Skip to content

Commit

Permalink
Add extra grammar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-pino committed Sep 8, 2023
1 parent 8595730 commit 1dd8e44
Showing 1 changed file with 167 additions and 13 deletions.
180 changes: 167 additions & 13 deletions pkg/assembler/grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package assembler
import (
"testing"

"github.com/alecthomas/participle/v2"
"github.com/stretchr/testify/require"
)

var testParser *participle.Parser[CasmProgram] = safeParserBuild()

func TestAssertEqualWithRegister(t *testing.T) {
func TestAssertEqualWithRegisterGrammar(t *testing.T) {
code := "[fp + 3] = [ap + 4];"

casmAst, err := parseCode(code)
Expand Down Expand Up @@ -48,18 +45,175 @@ func TestAssertEqualWithRegister(t *testing.T) {
)
}

func TestAssertEqualWithApPlusGrammar(t *testing.T) {
code := "[fp + 3] = [ap + 4], ap++;"

casmAst, err := parseCode(code)
require.NoError(t, err)

require.Equal(
t,
&CasmProgram{
[]Instruction{
{
Core: &CoreInstruction{
AssertEq: &AssertEq{
Dst: &Deref{
Name: "fp",
Offset: &Offset{
Sign: "+",
Value: ptrOf(3),
},
},
Value: &Expression{
Deref: &Deref{
Name: "ap",
Offset: &Offset{
Sign: "+",
Value: ptrOf(4),
},
},
},
},
},
ApPlusOne: true,
},
},
},
casmAst,
)
}

func TestAssertEqualWithImmediateGrammar(t *testing.T) {
code := "[fp + 1] = 5;"

casmAst, err := parseCode(code)
require.NoError(t, err)

require.Equal(
t,
&CasmProgram{
[]Instruction{
{
Core: &CoreInstruction{
AssertEq: &AssertEq{
Dst: &Deref{
Name: "fp",
Offset: &Offset{
Sign: "+",
Value: ptrOf(1),
},
},
Value: &Expression{
Immediate: ptrOf("5"),
},
},
},
ApPlusOne: false,
},
},
},
casmAst,
)
}

func TestAssertEqualWithMathOperationGrammar(t *testing.T) {
code := "[ap] = [fp + 7] + 5;"

casmAst, err := parseCode(code)
require.NoError(t, err)

require.Equal(
t,
&CasmProgram{
[]Instruction{
{
Core: &CoreInstruction{
AssertEq: &AssertEq{
Dst: &Deref{
Name: "ap",
Offset: nil,
},
Value: &Expression{
MathOperation: &MathOperation{
Lhs: &Deref{
Name: "fp",
Offset: &Offset{
Sign: "+",
Value: ptrOf(7),
},
},
Operator: "+",
Rhs: &DerefOrImm{
Immediate: ptrOf("5"),
},
},
},
},
},
ApPlusOne: false,
},
},
},
casmAst,
)
}

func TestCallAbsGrammar(t *testing.T) {
code := "call abs 123;"

casmAst, err := parseCode(code)
require.NoError(t, err)

require.Equal(
t,
&CasmProgram{
[]Instruction{
{
Core: &CoreInstruction{
Call: &Call{
CallType: "abs",
Value: &DerefOrImm{
Immediate: ptrOf("123"),
},
},
},
ApPlusOne: false,
},
},
},
casmAst,
)
}

func TestRetGrammar(t *testing.T) {
code := "ret;"

casmAst, err := parseCode(code)
require.NoError(t, err)

require.Equal(
t,
&CasmProgram{
[]Instruction{
{
Core: &CoreInstruction{
Ret: &Ret{
Ret: "",
},
},
ApPlusOne: false,
},
},
},
casmAst,
)
}

func ptrOf[T any](n T) *T {
return &n
}

func parseCode(code string) (*CasmProgram, error) {
return testParser.ParseString("", code)
}

func safeParserBuild() *participle.Parser[CasmProgram] {
parser, err := participle.Build[CasmProgram]()
if err != nil {
panic(err)
}
return parser
return parser.ParseString("", code)
}

0 comments on commit 1dd8e44

Please sign in to comment.