add enum support
This commit is contained in:
parent
622f0e8d81
commit
9781a28df7
4 changed files with 60 additions and 0 deletions
|
@ -79,3 +79,9 @@ pub mut:
|
|||
pub mut mut:
|
||||
f int
|
||||
}
|
||||
|
||||
enum Color {
|
||||
red green blue
|
||||
cyan
|
||||
alpha
|
||||
}
|
||||
|
|
20
src/ast.zig
20
src/ast.zig
|
@ -5,6 +5,7 @@ const Token = tokens.Token;
|
|||
pub const NodeList = std.ArrayList(*Node);
|
||||
pub const StmtList = std.ArrayList(*Stmt);
|
||||
pub const ExprList = std.ArrayList(*Expr);
|
||||
pub const TokenList = std.ArrayList(Token);
|
||||
pub const ParamList = std.ArrayList(ParamDecl);
|
||||
pub const ConstList = std.ArrayList(SingleConst);
|
||||
|
||||
|
@ -13,6 +14,7 @@ pub const NodeType = enum {
|
|||
FnDecl,
|
||||
ConstDecl,
|
||||
Struct,
|
||||
Enum,
|
||||
Block,
|
||||
Stmt,
|
||||
};
|
||||
|
@ -239,11 +241,17 @@ pub const Struct = struct {
|
|||
fields: FieldList,
|
||||
};
|
||||
|
||||
pub const Enum = struct {
|
||||
name: Token,
|
||||
fields: TokenList,
|
||||
};
|
||||
|
||||
pub const Node = union(NodeType) {
|
||||
Root: NodeList,
|
||||
FnDecl: FnDecl,
|
||||
ConstDecl: ConstList,
|
||||
Struct: Struct,
|
||||
Enum: Enum,
|
||||
|
||||
Block: StmtList,
|
||||
|
||||
|
@ -266,4 +274,16 @@ pub const Node = union(NodeType) {
|
|||
|
||||
return node;
|
||||
}
|
||||
|
||||
pub fn mkEnumDecl(allocator: *std.mem.Allocator, name: Token, fields: TokenList) !*Node {
|
||||
var node = try allocator.create(Node);
|
||||
node.* = Node{
|
||||
.Enum = Enum{
|
||||
.name = name,
|
||||
.fields = fields,
|
||||
},
|
||||
};
|
||||
|
||||
return node;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -81,6 +81,20 @@ pub fn printNode(node: *Node, ident: usize) void {
|
|||
print(ident, "))\n");
|
||||
},
|
||||
|
||||
.Enum => |decl| {
|
||||
print(ident, "(enum {} (\n", decl.name.lexeme);
|
||||
|
||||
for (decl.fields.toSlice()) |field| {
|
||||
print(
|
||||
ident + 1,
|
||||
"{}\n",
|
||||
field.lexeme,
|
||||
);
|
||||
}
|
||||
|
||||
print(ident, "))\n");
|
||||
},
|
||||
|
||||
.Root => {
|
||||
for (node.Root.toSlice()) |child| {
|
||||
printNode(child, ident + 1);
|
||||
|
|
|
@ -582,11 +582,31 @@ pub const Parser = struct {
|
|||
}
|
||||
}
|
||||
|
||||
fn parseEnumDecl(self: *@This()) !*Node {
|
||||
_ = try self.consumeSingle(.Enum);
|
||||
|
||||
var fields = ast.TokenList.init(self.allocator);
|
||||
errdefer fields.deinit();
|
||||
|
||||
const name = try self.consumeSingle(.Identifier);
|
||||
|
||||
_ = try self.consumeSingle(.LeftBrace);
|
||||
|
||||
while (!self.check(.RightBrace)) {
|
||||
try fields.append(try self.consumeSingle(.Identifier));
|
||||
}
|
||||
|
||||
_ = try self.consumeSingle(.RightBrace);
|
||||
|
||||
return try Node.mkEnumDecl(self.allocator, name, fields);
|
||||
}
|
||||
|
||||
fn parseTopDecl(self: *@This()) !*Node {
|
||||
return switch (self.peek().ttype) {
|
||||
.Fn => try self.parseFnDecl(),
|
||||
.Const => try self.parseConstDecl(),
|
||||
.Struct => try self.parseStructDecl(),
|
||||
.Enum => try self.parseEnumDecl(),
|
||||
|
||||
else => |ttype| blk: {
|
||||
self.doError("expected Fn, Const, Struct, got {}\n", ttype);
|
||||
|
|
Loading…
Reference in a new issue