Skip to content

Commit

Permalink
Fix(assembler): increase parser lookahead (#108)
Browse files Browse the repository at this point in the history
bug fix: increase parser lookahead
  • Loading branch information
rodrigo-pino authored Oct 11, 2023
1 parent 9c2c6e0 commit 0868a92
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/assembler/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions pkg/assembler/grammar_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assembler

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0868a92

Please sign in to comment.