diff --git a/pkg/assembler/assembler.go b/pkg/assembler/assembler.go index b567ba672..985227272 100644 --- a/pkg/assembler/assembler.go +++ b/pkg/assembler/assembler.go @@ -9,7 +9,7 @@ var parser *participle.Parser[CasmProgram] = participle.MustBuild[CasmProgram]( // mandatory lookahead to disambiguate between productions: // expr -> [reg + n] + [reg + m] and // expr -> [reg + n] - participle.UseLookahead(5), + participle.UseLookahead(7), ) func CasmToBytecode(code string) ([]*f.Element, error) { diff --git a/pkg/assembler/grammar_test.go b/pkg/assembler/grammar_test.go index 70e2a1d27..a8d939a30 100644 --- a/pkg/assembler/grammar_test.go +++ b/pkg/assembler/grammar_test.go @@ -1,6 +1,7 @@ package assembler import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -198,6 +199,52 @@ func TestRetGrammar(t *testing.T) { ) } +func TestJumpGrammar(t *testing.T) { + for _, jmpType := range []string{"abs", "rel"} { + t.Logf("jmpType: %s", jmpType) + + code := fmt.Sprintf("jmp %s [ap + 1] + [fp - 7];", jmpType) + + casmAst, err := parseCode(code) + require.NoError(t, err) + + require.Equal( + t, + &CasmProgram{ + []InstructionNode{ + { + Jump: &Jump{ + JumpType: jmpType, + Value: &Expression{ + MathOperation: &MathOperation{ + Lhs: &Deref{ + Name: "ap", + Offset: &Offset{ + Sign: "+", + Value: ptrOf(1), + }, + }, + Rhs: &DerefOrImm{ + Deref: &Deref{ + Name: "fp", + Offset: &Offset{ + Sign: "-", + Value: ptrOf(7), + }, + }, + }, + Operator: "+", + }, + }, + }, + }, + }, + }, + casmAst, + ) + } +} + func ptrOf[T any](n T) *T { return &n }