VM!
This commit is contained in:
@@ -12,6 +12,7 @@ type Opcode byte
|
||||
|
||||
const (
|
||||
OpConstant Opcode = iota
|
||||
OpAdd
|
||||
)
|
||||
|
||||
type Definition struct {
|
||||
@@ -21,6 +22,7 @@ type Definition struct {
|
||||
|
||||
var definitions = map[Opcode]*Definition{
|
||||
OpConstant: {"OpConstant", []int{2}},
|
||||
OpAdd: {"OpAdd", []int{}},
|
||||
}
|
||||
|
||||
func Lookup(op byte) (*Definition, error) {
|
||||
@@ -88,6 +90,8 @@ func (ins Instructions) fmtInstruction(def *Definition, operands []int) string {
|
||||
}
|
||||
|
||||
switch operandCount {
|
||||
case 0:
|
||||
return def.Name
|
||||
case 1:
|
||||
return fmt.Sprintf("%s %d", def.Name, operands[0])
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ func TestMake(t *testing.T) {
|
||||
expected []byte
|
||||
}{
|
||||
{OpConstant, []int{65534}, []byte{byte(OpConstant), 255, 254}},
|
||||
{OpAdd, []int{}, []byte{byte(OpAdd)}},
|
||||
}
|
||||
|
||||
for _, tt := range test {
|
||||
@@ -28,14 +29,14 @@ func TestMake(t *testing.T) {
|
||||
|
||||
func TestInstructions(t *testing.T) {
|
||||
instructions := []Instructions{
|
||||
Make(OpConstant, 1),
|
||||
Make(OpAdd),
|
||||
Make(OpConstant, 2),
|
||||
Make(OpConstant, 65535),
|
||||
}
|
||||
|
||||
expected := `0000 OpConstant 1
|
||||
0003 OpConstant 2
|
||||
0006 OpConstant 65535
|
||||
expected := `0000 OpAdd
|
||||
0001 OpConstant 2
|
||||
0004 OpConstant 65535
|
||||
`
|
||||
|
||||
concatted := Instructions{}
|
||||
|
||||
Reference in New Issue
Block a user