Skip to content

Commit

Permalink
Unflatten builtins opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jan 30, 2023
1 parent 9caceee commit f311f78
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
7 changes: 3 additions & 4 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ var (
type Function struct {
Name string
Func func(args ...interface{}) (interface{}, error)
Opcode byte
Opcode int
Types []reflect.Type
Validate func(args []reflect.Type) (reflect.Type, error)
}

const (
Opcode byte = iota + 100
Len
Len = iota + 1
Abs
Int
Float
)

var Builtins = map[byte]*Function{
var Builtins = map[int]*Function{
Len: {
Name: "len",
Opcode: Len,
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func (c *compiler) CallNode(node *ast.CallNode) {
}
if node.Func != nil {
if node.Func.Opcode > 0 {
c.emit(node.Func.Opcode)
c.emit(OpBuiltin, node.Func.Opcode)
return
}
switch len(node.Arguments) {
Expand Down
3 changes: 2 additions & 1 deletion vm/opcodes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package vm

type Opcode = byte
type Opcode byte

const (
OpPush Opcode = iota
Expand Down Expand Up @@ -54,6 +54,7 @@ const (
OpCallN
OpCallFast
OpCallTyped
OpBuiltin
OpArray
OpMap
OpLen
Expand Down
11 changes: 6 additions & 5 deletions vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ func (program *Program) Disassemble() string {
}
out += fmt.Sprintf("%v\t%v\t%v\t%v\n", pp, label, arg, c)
}

if op > builtin.Opcode {
f, ok := builtin.Builtins[op]
builtIn := func(label string) {
f, ok := builtin.Builtins[arg]
if !ok {
panic(fmt.Sprintf("unknown builtin %v", op))
panic(fmt.Sprintf("unknown builtin %v", arg))
}
out += fmt.Sprintf("%v\t%v\t%v\n", pp, "OpBuiltin", f.Name)
continue
}

switch op {
Expand Down Expand Up @@ -224,6 +222,9 @@ func (program *Program) Disassemble() string {
case OpCallTyped:
argument("OpCallTyped")

case OpBuiltin:
builtIn("OpBuiltin")

case OpArray:
code("OpArray")

Expand Down
4 changes: 2 additions & 2 deletions vm/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
func TestProgram_Disassemble(t *testing.T) {
for op := vm.OpPush; op < vm.OpEnd; op++ {
program := vm.Program{
Constants: []interface{}{true},
Constants: []interface{}{1, 2},
Bytecode: []vm.Opcode{op},
Arguments: []int{0},
Arguments: []int{1},
}
d := program.Disassemble()
if strings.Contains(d, "\t0x") {
Expand Down
22 changes: 14 additions & 8 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,23 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)
case OpEnd:
vm.scopes = vm.scopes[:len(vm.scopes)-1]

case builtin.Len:
vm.push(runtime.Len(vm.pop()))
case OpBuiltin:
switch arg {
case builtin.Len:
vm.push(runtime.Len(vm.pop()))

case builtin.Abs:
vm.push(runtime.Abs(vm.pop()))
case builtin.Abs:
vm.push(runtime.Abs(vm.pop()))

case builtin.Int:
vm.push(runtime.ToInt(vm.pop()))
case builtin.Int:
vm.push(runtime.ToInt(vm.pop()))

case builtin.Float:
vm.push(runtime.ToFloat64(vm.pop()))

case builtin.Float:
vm.push(runtime.ToFloat64(vm.pop()))
default:
panic(fmt.Sprintf("unknown builtin %v", arg))
}

default:
panic(fmt.Sprintf("unknown bytecode %#x", op))
Expand Down

0 comments on commit f311f78

Please sign in to comment.