diff options
Diffstat (limited to 'pkg/lang/parser/parser_test.go')
| -rw-r--r-- | pkg/lang/parser/parser_test.go | 200 |
1 files changed, 199 insertions, 1 deletions
diff --git a/pkg/lang/parser/parser_test.go b/pkg/lang/parser/parser_test.go index b756b1b..e02ab5a 100644 --- a/pkg/lang/parser/parser_test.go +++ b/pkg/lang/parser/parser_test.go @@ -106,7 +106,7 @@ func TestLeftAssocBinary4Expr(t *testing.T) { require.Equal(t, 1, len(program.Stmts)) require.Equal(t, ast.Stmt{ - Kind: 11, + Kind: ast.StmtKindExpr, Value: ast.StmtExpr{ Value: ast.Expr{ Kind: ast.ExprKindBinary, @@ -420,6 +420,204 @@ func TestIfStmt(t *testing.T) { }, program.Stmts[0]) } +func TestForCondStmt(t *testing.T) { + src := sourceify( + `for x <= 5 {`, + ` x = x + 1`, + `}`, + ) + + p := cheatWithScanner(t, src) + program, err := p.Parse() + require.NoError(t, err) + + require.Equal(t, 1, len(program.Stmts)) + require.Equal(t, ast.Stmt{ + At: source.NewLoc(0, 0), + Kind: ast.StmtKindForCond, + Value: ast.StmtForCond{ + Cond: ast.Expr{ + At: source.NewLoc(0, 4), + Kind: ast.ExprKindBinary, + Value: ast.ExprBinary{ + Left: ast.Expr{ + At: source.NewLoc(0, 4), + Kind: ast.ExprKindIdent, + Value: ast.ExprIdent{ + Value: ast.IdentNode{ + At: source.NewLoc(0, 4), + Value: "x", + }, + }, + }, + Op: ast.BinOpLte, + Right: ast.Expr{ + At: source.NewLoc(0, 9), + Kind: ast.ExprKindIntLit, + Value: ast.ExprIntLit{ + Value: 5, + }, + }, + }, + }, + Do: ast.BlockNode{ + At: source.NewLoc(0, 11), + Stmts: []ast.Stmt{ + { + At: source.NewLoc(0, 12), + Kind: ast.StmtKindEmpty, + Value: ast.StmtEmpty{}, + }, + { + At: source.NewLoc(1, 1), + Kind: ast.StmtKindExpr, + Value: ast.StmtExpr{ + Value: ast.Expr{ + At: source.NewLoc(1, 1), + Kind: ast.ExprKindBinary, + Value: ast.ExprBinary{ + Left: ast.Expr{ + At: source.NewLoc(1, 1), + Kind: ast.ExprKindIdent, + Value: ast.ExprIdent{ + Value: ast.IdentNode{ + At: source.NewLoc(1, 1), + Value: "x", + }, + }, + }, + Op: ast.BinOpAssign, + Right: ast.Expr{ + At: source.NewLoc(1, 5), + Kind: ast.ExprKindBinary, + Value: ast.ExprBinary{ + Left: ast.Expr{ + At: source.NewLoc(1, 5), + Kind: ast.ExprKindIdent, + Value: ast.ExprIdent{ + Value: ast.IdentNode{ + At: source.NewLoc(1, 5), + Value: "x", + }, + }, + }, + Op: ast.BinOpPlus, + Right: ast.Expr{ + At: source.NewLoc(1, 9), + Kind: ast.ExprKindIntLit, + Value: ast.ExprIntLit{ + Value: 1, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, program.Stmts[0]) +} + +func TestForInStmt(t *testing.T) { + src := sourceify( + `for x in [1, 2, 3] {`, + ` say(x)`, + `}`, + ) + + p := cheatWithScanner(t, src) + program, err := p.Parse() + require.NoError(t, err) + + require.Equal(t, 1, len(program.Stmts)) + require.Equal(t, ast.Stmt{ + At: source.NewLoc(0, 0), + Kind: ast.StmtKindForIn, + Value: ast.StmtForIn{ + Name: ast.IdentNode{ + At: source.NewLoc(0, 4), + Value: "x", + }, + Collection: ast.Expr{ + At: source.NewLoc(0, 9), + Kind: ast.ExprKindArrayLit, + Value: ast.ExprArrayLit{ + Values: []ast.Expr{ + { + At: source.NewLoc(0, 10), + Kind: ast.ExprKindIntLit, + Value: ast.ExprIntLit{ + Value: 1, + }, + }, + { + At: source.NewLoc(0, 13), + Kind: ast.ExprKindIntLit, + Value: ast.ExprIntLit{ + Value: 2, + }, + }, + { + At: source.NewLoc(0, 16), + Kind: ast.ExprKindIntLit, + Value: ast.ExprIntLit{ + Value: 3, + }, + }, + }, + }, + }, + Do: ast.BlockNode{ + At: source.NewLoc(0, 19), + Stmts: []ast.Stmt{ + { + At: source.NewLoc(0, 20), + Kind: ast.StmtKindEmpty, + Value: ast.StmtEmpty{}, + }, + { + At: source.NewLoc(1, 1), + Kind: ast.StmtKindExpr, + Value: ast.StmtExpr{ + Value: ast.Expr{ + At: source.NewLoc(1, 1), + Kind: ast.ExprKindCall, + Value: ast.ExprCall{ + Callee: ast.Expr{ + At: source.NewLoc(1, 1), + Kind: ast.ExprKindIdent, + Value: ast.ExprIdent{ + Value: ast.IdentNode{ + At: source.NewLoc(1, 1), + Value: "say", + }, + }, + }, + Args: []ast.Expr{ + { + At: source.NewLoc(1, 5), + Kind: ast.ExprKindIdent, + Value: ast.ExprIdent{ + Value: ast.IdentNode{ + At: source.NewLoc(1, 5), + Value: "x", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, program.Stmts[0]) +} + func sourceify(lines ...string) string { return strings.Join(lines, "\n") } |
