110 lines
3.3 KiB
Zig
110 lines
3.3 KiB
Zig
const std = @import("std");
|
|
const Ast = @import("./Ast.zig");
|
|
|
|
test "parser: empty file" {
|
|
try testEqual(
|
|
\\
|
|
,
|
|
\\File "<STDIN>"
|
|
\\
|
|
,
|
|
);
|
|
}
|
|
|
|
test "parser: \"Hello World\"" {
|
|
try testEqual(
|
|
\\Hello, world!
|
|
,
|
|
\\File "<STDIN>"
|
|
\\`--BlockStmt <line:1, line:1>
|
|
\\ `--ContentStmt <line:1, col:1:14>
|
|
\\ `--Content <col:1, col:14>
|
|
\\ `--StringLiteral `Hello, world!` <col:1, col:14>
|
|
\\
|
|
,
|
|
);
|
|
}
|
|
|
|
test "parser: expression statements" {
|
|
try testEqual(
|
|
\\~ (-1 + 2) * 3 - -4
|
|
,
|
|
\\File "<STDIN>"
|
|
\\`--BlockStmt <line:1, line:1>
|
|
\\ `--ExprStmt <line:1, col:3:20>
|
|
\\ `--SubtractExpr <col:4, col:20>
|
|
\\ |--MultiplyExpr <col:4, col:15>
|
|
\\ | |--AddExpr <col:4, col:10>
|
|
\\ | | |--NegateExpr <col:4, col:6>
|
|
\\ | | | `--NumberLiteral `1` <col:5, col:6>
|
|
\\ | | `--NumberLiteral `2` <col:9, col:10>
|
|
\\ | `--NumberLiteral `3` <col:14, col:15>
|
|
\\ `--NegateExpr <col:18, col:20>
|
|
\\ `--NumberLiteral `4` <col:19, col:20>
|
|
\\
|
|
,
|
|
);
|
|
}
|
|
|
|
test "parser: temporary variables" {
|
|
try testEqual(
|
|
\\~ temp a = 1 + 2 + 3
|
|
\\~ temp b = a + 1
|
|
\\{b}
|
|
,
|
|
\\File "<STDIN>"
|
|
\\`--BlockStmt <line:1, line:3>
|
|
\\ |--TempDecl <line:1, col:3:21>
|
|
\\ | |--Identifier `a` <col:8, col:9>
|
|
\\ | `--AddExpr <col:12, col:21>
|
|
\\ | |--AddExpr <col:12, col:17>
|
|
\\ | | |--NumberLiteral `1` <col:12, col:13>
|
|
\\ | | `--NumberLiteral `2` <col:16, col:17>
|
|
\\ | `--NumberLiteral `3` <col:20, col:21>
|
|
\\ |--TempDecl <line:2, col:3:17>
|
|
\\ | |--Identifier `b` <col:8, col:9>
|
|
\\ | `--AddExpr <col:12, col:17>
|
|
\\ | |--Identifier `a` <col:12, col:13>
|
|
\\ | `--NumberLiteral `1` <col:16, col:17>
|
|
\\ `--ContentStmt <line:3, col:1:4>
|
|
\\ `--Content <col:1, col:4>
|
|
\\ `--InlineLogicExpr <col:1, col:4>
|
|
\\ `--Identifier `b` <col:2, col:3>
|
|
\\
|
|
,
|
|
);
|
|
}
|
|
|
|
test "parser: temporary assignment" {
|
|
try testEqual(
|
|
\\~ temp a = 123
|
|
\\~ a = 321
|
|
,
|
|
\\File "<STDIN>"
|
|
\\`--BlockStmt <line:1, line:2>
|
|
\\ |--TempDecl <line:1, col:3:15>
|
|
\\ | |--Identifier `a` <col:8, col:9>
|
|
\\ | `--NumberLiteral `123` <col:12, col:15>
|
|
\\ `--AssignStmt <line:2, col:3:10>
|
|
\\ |--Identifier `a` <col:3, col:4>
|
|
\\ `--NumberLiteral `321` <col:7, col:10>
|
|
\\
|
|
,
|
|
);
|
|
}
|
|
|
|
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, "<STDIN>", 0);
|
|
try ast.render(gpa, &allocating.writer, .{
|
|
.use_color = false,
|
|
});
|
|
return std.testing.expectEqualSlices(u8, expected_ast, allocating.written());
|
|
}
|