Skip to content

Commit

Permalink
Rename opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Nov 12, 2022
1 parent d4bcbad commit 5cfbf51
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
10 changes: 5 additions & 5 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,19 @@ func (c *compiler) NilNode(_ *ast.NilNode) {

func (c *compiler) IdentifierNode(node *ast.IdentifierNode) {
if c.mapEnv {
c.emit(OpFetchEnvFast, c.addConstant(node.Value))
c.emit(OpEnvFast, c.addConstant(node.Value))
} else if len(node.FieldIndex) > 0 {
c.emit(OpFetchEnvField, c.addConstant(&runtime.Field{
c.emit(OpEnvField, c.addConstant(&runtime.Field{
Index: node.FieldIndex,
Path: []string{node.Value},
}))
} else if node.Method {
c.emit(OpMethodEnv, c.addConstant(&runtime.Method{
c.emit(OpEnvMethod, c.addConstant(&runtime.Method{
Name: node.Value,
Index: node.MethodIndex,
}))
} else {
c.emit(OpFetchEnv, c.addConstant(node.Value))
c.emit(OpEnvConst, c.addConstant(node.Value))
}
if node.Deref {
c.emit(OpDeref)
Expand Down Expand Up @@ -454,7 +454,7 @@ func (c *compiler) MemberNode(node *ast.MemberNode) {
}
index = append(ident.FieldIndex, index...)
path = append([]string{ident.Value}, path...)
c.emitLocation(ident.Location(), OpFetchEnvField, c.addConstant(
c.emitLocation(ident.Location(), OpEnvField, c.addConstant(
&runtime.Field{Index: index, Path: path},
))
goto deref
Expand Down
8 changes: 4 additions & 4 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestCompile(t *testing.T) {
},
},
Bytecode: []vm.Opcode{
vm.OpFetchEnvField,
vm.OpEnvField,
},
Arguments: []int{0},
},
Expand All @@ -178,7 +178,7 @@ func TestCompile(t *testing.T) {
},
},
Bytecode: []vm.Opcode{
vm.OpFetchEnvField,
vm.OpEnvField,
vm.OpJumpIfNil,
vm.OpFetchField,
},
Expand All @@ -199,7 +199,7 @@ func TestCompile(t *testing.T) {
},
},
Bytecode: []vm.Opcode{
vm.OpFetchEnvField,
vm.OpEnvField,
vm.OpJumpIfNil,
vm.OpFetchField,
},
Expand All @@ -221,7 +221,7 @@ func TestCompile(t *testing.T) {
},
},
Bytecode: []vm.Opcode{
vm.OpFetchEnvField,
vm.OpEnvField,
vm.OpPush,
vm.OpFetch,
vm.OpFetchField,
Expand Down
8 changes: 4 additions & 4 deletions vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const (
OpPushInt
OpPop
OpRot
OpEnvConst
OpEnvField
OpEnvFast
OpEnvMethod
OpFetch
OpFetchField
OpFetchEnv
OpFetchEnvField
OpFetchEnvFast
OpMethod
OpMethodEnv
OpTrue
OpFalse
OpNil
Expand Down
24 changes: 12 additions & 12 deletions vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,27 @@ func (program *Program) Disassemble() string {
case OpRot:
code("OpRot")

case OpEnvConst:
constant("OpEnvConst")

case OpEnvField:
constant("OpEnvField")

case OpEnvFast:
constant("OpEnvFast")

case OpEnvMethod:
constant("OpEnvMethod")

case OpFetch:
code("OpFetch")

case OpFetchField:
constant("OpFetchField")

case OpFetchEnv:
constant("OpFetchEnv")

case OpFetchEnvField:
constant("OpFetchEnvField")

case OpFetchEnvFast:
constant("OpFetchEnvFast")

case OpMethod:
constant("OpMethod")

case OpMethodEnv:
constant("OpMethodEnv")

case OpTrue:
code("OpTrue")

Expand Down
24 changes: 12 additions & 12 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
vm.push(b)
vm.push(a)

case OpEnvConst:
vm.push(runtime.Fetch(env, program.Constants[arg]))

case OpEnvField:
vm.push(runtime.FetchField(env, program.Constants[arg].(*runtime.Field)))

case OpEnvFast:
vm.push(env.(map[string]interface{})[program.Constants[arg].(string)])

case OpEnvMethod:
vm.push(runtime.FetchMethod(env, program.Constants[arg].(*runtime.Method)))

case OpFetch:
b := vm.pop()
a := vm.pop()
Expand All @@ -109,22 +121,10 @@ func (vm *VM) Run(program *Program, env interface{}) (out interface{}, err error
a := vm.pop()
vm.push(runtime.FetchField(a, program.Constants[arg].(*runtime.Field)))

case OpFetchEnv:
vm.push(runtime.Fetch(env, program.Constants[arg]))

case OpFetchEnvField:
vm.push(runtime.FetchField(env, program.Constants[arg].(*runtime.Field)))

case OpFetchEnvFast:
vm.push(env.(map[string]interface{})[program.Constants[arg].(string)])

case OpMethod:
a := vm.pop()
vm.push(runtime.FetchMethod(a, program.Constants[arg].(*runtime.Method)))

case OpMethodEnv:
vm.push(runtime.FetchMethod(env, program.Constants[arg].(*runtime.Method)))

case OpTrue:
vm.push(true)

Expand Down

0 comments on commit 5cfbf51

Please sign in to comment.