Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Latest commit

 

History

History
48 lines (38 loc) · 907 Bytes

README.md

File metadata and controls

48 lines (38 loc) · 907 Bytes

zig-crystal-ast

Crystal language parser and AST implemented in Zig (WIP)

Crystal:

abstract class ASTNode
  # ...
end

class While < ASTNode
  property cond : ASTNode
  property body : ASTNode

  def initialize(@cond, body = nil)
    @body = Expressions.from body
  end
end

Zig:

pub const Node = union(enum) {
    // ...
    @"while": *While,
    yield: *Yield,
};

pub const While = struct {
    cond: Node,
    body: Node,

    pub fn allocate(allocator: std.mem.Allocator, cond: Node, body: ?Node) !*@This() {
        var instance = try allocator.create(@This());
        instance.* = .{
            .cond = cond,
            .body = try Expressions.from(allocator, body),
        };
        return instance;
    }

    pub fn node(allocator: std.mem.Allocator, cond: Node, body: ?Node) !Node {
        return Node { .@"while" = try allocate(allocator, cond, body) };
    }
};