lots o fixes
This commit is contained in:
55
internal/compiler/bytecode_test.go
Normal file
55
internal/compiler/bytecode_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package compiler
|
||||
|
||||
import (
|
||||
"monkey/internal/lexer"
|
||||
"monkey/internal/parser"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var hexRe = regexp.MustCompile(`0x[0-9a-f]+`)
|
||||
|
||||
func compile(input string) (*Bytecode, error) {
|
||||
l := lexer.New(input)
|
||||
p := parser.New("<text", l)
|
||||
c := New()
|
||||
if err := c.Compile(p.ParseProgram()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.Bytecode(), nil
|
||||
}
|
||||
|
||||
func TestEncodeDecode(t *testing.T) {
|
||||
input := `
|
||||
fib := fn(x) {
|
||||
if (x == 0) {
|
||||
return 0
|
||||
}
|
||||
if (x == 1) {
|
||||
return 1
|
||||
}
|
||||
return fib(x-1) + fib(x-2)
|
||||
}
|
||||
|
||||
fib(35)
|
||||
`
|
||||
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
bc, err := compile(input)
|
||||
require.NoError(err)
|
||||
|
||||
encoded, err := bc.Encode()
|
||||
require.NoError(err)
|
||||
|
||||
decoded := Decode(encoded)
|
||||
|
||||
expected := hexRe.ReplaceAllString(bc.String(), "0x1234")
|
||||
actual := hexRe.ReplaceAllString(decoded.String(), "0x1234")
|
||||
|
||||
assert.EqualValues(expected, actual)
|
||||
}
|
||||
Reference in New Issue
Block a user