const std = @import("std"); const Ast = @import("./Ast.zig"); test "parser: empty file" { try testEqual( \\ , \\File "" \\ , ); } test "parser: \"Hello World\"" { try testEqual( \\Hello, world! , \\File "" \\`--BlockStmt \\ `--ContentStmt \\ `--Content \\ `--StringLiteral `Hello, world!` \\ , ); } test "parser: expression statements" { try testEqual( \\~ (-1 + 2) * 3 - -4 , \\File "" \\`--BlockStmt \\ `--ExprStmt \\ `--SubtractExpr \\ |--MultiplyExpr \\ | |--AddExpr \\ | | |--NegateExpr \\ | | | `--NumberLiteral `1` \\ | | `--NumberLiteral `2` \\ | `--NumberLiteral `3` \\ `--NegateExpr \\ `--NumberLiteral `4` \\ , ); } test "parser: temporary variables" { try testEqual( \\~ temp a = 1 + 2 + 3 \\~ temp b = a + 1 \\{b} , \\File "" \\`--BlockStmt \\ |--TempDecl \\ | |--Identifier `a` \\ | `--AddExpr \\ | |--AddExpr \\ | | |--NumberLiteral `1` \\ | | `--NumberLiteral `2` \\ | `--NumberLiteral `3` \\ |--TempDecl \\ | |--Identifier `b` \\ | `--AddExpr \\ | |--Identifier `a` \\ | `--NumberLiteral `1` \\ `--ContentStmt \\ `--Content \\ `--InlineLogicExpr \\ `--Identifier `b` \\ , ); } test "parser: temporary assignment" { try testEqual( \\~ temp a = 123 \\~ a = 321 , \\File "" \\`--BlockStmt \\ |--TempDecl \\ | |--Identifier `a` \\ | `--NumberLiteral `123` \\ `--AssignStmt \\ |--Identifier `a` \\ `--NumberLiteral `321` \\ , ); } fn testEqual(source_bytes: [:0]const u8, expected_ast: []const u8) !void { const gpa = std.testing.allocator; var arena_allocator = std.heap.ArenaAllocator.init(gpa); defer arena_allocator.deinit(); const arena = arena_allocator.allocator(); var allocating = std.io.Writer.Allocating.init(gpa); defer allocating.deinit(); const ast = try Ast.parse(gpa, arena, source_bytes, "", 0); try ast.render(gpa, &allocating.writer, .{ .use_color = false, }); return std.testing.expectEqualSlices(u8, expected_ast, allocating.written()); }