lots o fixes
Some checks failed
Build / build (push) Successful in 9m50s
Publish Image / publish (push) Failing after 49s
Test / build (push) Successful in 10m55s

This commit is contained in:
2024-04-01 18:18:45 -04:00
parent fe33fda0ab
commit 862119e90e
44 changed files with 326 additions and 170 deletions

View 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)
}