Everything is a closure
This commit is contained in:
@@ -38,6 +38,7 @@ const (
|
||||
OpGetLocal
|
||||
OpSetLocal
|
||||
OpGetBuiltin
|
||||
OpClosure
|
||||
)
|
||||
|
||||
type Definition struct {
|
||||
@@ -73,6 +74,7 @@ var definitions = map[Opcode]*Definition{
|
||||
OpGetLocal: {"OpGetLocal", []int{1}},
|
||||
OpSetLocal: {"OpSetLocal", []int{1}},
|
||||
OpGetBuiltin: {"OpGetBuiltin", []int{1}},
|
||||
OpClosure: {"OpClosure", []int{2, 1}},
|
||||
}
|
||||
|
||||
func Lookup(op byte) (*Definition, error) {
|
||||
@@ -146,6 +148,9 @@ func (ins Instructions) fmtInstruction(def *Definition, operands []int) string {
|
||||
return def.Name
|
||||
case 1:
|
||||
return fmt.Sprintf("%s %d", def.Name, operands[0])
|
||||
case 2:
|
||||
return fmt.Sprintf("%s %d %d", def.Name, operands[0], operands[1])
|
||||
|
||||
}
|
||||
|
||||
return fmt.Sprintf("ERROR: unhandled operandCount for %s\n", def.Name)
|
||||
|
||||
@@ -11,6 +11,7 @@ func TestMake(t *testing.T) {
|
||||
{OpConstant, []int{65534}, []byte{byte(OpConstant), 255, 254}},
|
||||
{OpAdd, []int{}, []byte{byte(OpAdd)}},
|
||||
{OpGetLocal, []int{255}, []byte{byte(OpGetLocal), 255}},
|
||||
{OpClosure, []int{65534, 255}, []byte{byte(OpClosure), 255, 254, 255}},
|
||||
}
|
||||
|
||||
for _, tt := range test {
|
||||
@@ -34,12 +35,14 @@ func TestInstructions(t *testing.T) {
|
||||
Make(OpGetLocal, 1),
|
||||
Make(OpConstant, 2),
|
||||
Make(OpConstant, 65535),
|
||||
Make(OpClosure, 65535, 255),
|
||||
}
|
||||
|
||||
expected := `0000 OpAdd
|
||||
0001 OpGetLocal 1
|
||||
0003 OpConstant 2
|
||||
0006 OpConstant 65535
|
||||
0009 OpClosure 65535 255
|
||||
`
|
||||
|
||||
concatted := Instructions{}
|
||||
@@ -60,6 +63,7 @@ func TestReadOperands(t *testing.T) {
|
||||
}{
|
||||
{OpConstant, []int{65535}, 2},
|
||||
{OpGetLocal, []int{255}, 1},
|
||||
{OpClosure, []int{65535, 255}, 3},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in New Issue
Block a user