al/src/main.zig
2025-12-16 23:30:02 +01:00

45 lines
1.5 KiB
Zig

const std = @import("std");
const al = @import("al");
pub fn nodeName(node: *al.Parser.Node) void {
switch (node.@"type") {
.start => std.debug.print("{d} [label=\"start\",fillcolor=yellow, color=black, shape=box]", .{node.id}),
.add => std.debug.print("{d} [label=\"+\"]", .{node.id}),
.sub => std.debug.print("{d} [label=\"-\"]", .{node.id}),
.mul => std.debug.print("{d} [label=\"*\"]", .{node.id}),
.div => std.debug.print("{d} [label=\"/\"]", .{node.id}),
.integer => std.debug.print("{d} [label=\"{d}\"]", .{node.id, node.data.integer}),
.float => std.debug.print("{d} [label=\"{d}\"]", .{node.id, node.data.float}),
else => {},
}
std.debug.print("\n", .{});
}
pub fn printGraph(node: *al.Parser.Node) void {
for (node.inputs.items) |n| {
nodeName(n);
std.debug.print("{d}->{d}\n", .{node.id, n.id});
printGraph(n);
}
}
pub fn main() !void {
var gpa = std.heap.DebugAllocator(.{}).init;
defer {
//_ = gpa.detectLeaks();
}
const allocator = gpa.allocator();
var lexer = try al.Lexer.init(allocator, @constCast("2+3+4"));
var parser = al.Parser.init(allocator, &lexer);
defer parser.deinit();
const graph = try parser.buildGraph();
if (graph) |g| {
defer g.deinit(&parser);
std.debug.print("digraph G {{\n", .{});
nodeName(g);
printGraph(g);
std.debug.print("}}\n", .{});
}
}