functions with bindings
This commit is contained in:
13
code/code.go
13
code/code.go
@@ -35,6 +35,8 @@ const (
|
||||
OpCall
|
||||
OpReturnValue
|
||||
OpReturn
|
||||
OpGetLocal
|
||||
OpSetLocal
|
||||
)
|
||||
|
||||
type Definition struct {
|
||||
@@ -67,6 +69,8 @@ var definitions = map[Opcode]*Definition{
|
||||
OpCall: {"OpCall", []int{}},
|
||||
OpReturnValue: {"OpReturnValue", []int{}},
|
||||
OpReturn: {"OpReturn", []int{}},
|
||||
OpGetLocal: {"OpGetLocal", []int{1}},
|
||||
OpSetLocal: {"OpSetLocal", []int{1}},
|
||||
}
|
||||
|
||||
func Lookup(op byte) (*Definition, error) {
|
||||
@@ -98,6 +102,8 @@ func Make(op Opcode, operands ...int) []byte {
|
||||
switch width {
|
||||
case 2:
|
||||
binary.BigEndian.PutUint16(instruction[offset:], uint16(o))
|
||||
case 1:
|
||||
instruction[offset] = byte(o)
|
||||
}
|
||||
offset += width
|
||||
}
|
||||
@@ -151,6 +157,9 @@ func ReadOperands(def *Definition, ins Instructions) ([]int, int) {
|
||||
switch width {
|
||||
case 2:
|
||||
operands[i] = int(ReadUint16(ins[offset:]))
|
||||
case 1:
|
||||
operands[i] = int(ReadUint8(ins[offset:]))
|
||||
|
||||
}
|
||||
|
||||
offset += width
|
||||
@@ -162,3 +171,7 @@ func ReadOperands(def *Definition, ins Instructions) ([]int, int) {
|
||||
func ReadUint16(ins Instructions) uint16 {
|
||||
return binary.BigEndian.Uint16(ins)
|
||||
}
|
||||
|
||||
func ReadUint8(ins Instructions) uint8 {
|
||||
return ins[0]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user