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:
|
pub mut mut:
|
||||||
f int
|
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 NodeList = std.ArrayList(*Node);
|
||||||
pub const StmtList = std.ArrayList(*Stmt);
|
pub const StmtList = std.ArrayList(*Stmt);
|
||||||
pub const ExprList = std.ArrayList(*Expr);
|
pub const ExprList = std.ArrayList(*Expr);
|
||||||
|
pub const TokenList = std.ArrayList(Token);
|
||||||
pub const ParamList = std.ArrayList(ParamDecl);
|
pub const ParamList = std.ArrayList(ParamDecl);
|
||||||
pub const ConstList = std.ArrayList(SingleConst);
|
pub const ConstList = std.ArrayList(SingleConst);
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ pub const NodeType = enum {
|
||||||
FnDecl,
|
FnDecl,
|
||||||
ConstDecl,
|
ConstDecl,
|
||||||
Struct,
|
Struct,
|
||||||
|
Enum,
|
||||||
Block,
|
Block,
|
||||||
Stmt,
|
Stmt,
|
||||||
};
|
};
|
||||||
|
@ -239,11 +241,17 @@ pub const Struct = struct {
|
||||||
fields: FieldList,
|
fields: FieldList,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const Enum = struct {
|
||||||
|
name: Token,
|
||||||
|
fields: TokenList,
|
||||||
|
};
|
||||||
|
|
||||||
pub const Node = union(NodeType) {
|
pub const Node = union(NodeType) {
|
||||||
Root: NodeList,
|
Root: NodeList,
|
||||||
FnDecl: FnDecl,
|
FnDecl: FnDecl,
|
||||||
ConstDecl: ConstList,
|
ConstDecl: ConstList,
|
||||||
Struct: Struct,
|
Struct: Struct,
|
||||||
|
Enum: Enum,
|
||||||
|
|
||||||
Block: StmtList,
|
Block: StmtList,
|
||||||
|
|
||||||
|
@ -266,4 +274,16 @@ pub const Node = union(NodeType) {
|
||||||
|
|
||||||
return node;
|
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");
|
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 => {
|
.Root => {
|
||||||
for (node.Root.toSlice()) |child| {
|
for (node.Root.toSlice()) |child| {
|
||||||
printNode(child, ident + 1);
|
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 {
|
fn parseTopDecl(self: *@This()) !*Node {
|
||||||
return switch (self.peek().ttype) {
|
return switch (self.peek().ttype) {
|
||||||
.Fn => try self.parseFnDecl(),
|
.Fn => try self.parseFnDecl(),
|
||||||
.Const => try self.parseConstDecl(),
|
.Const => try self.parseConstDecl(),
|
||||||
.Struct => try self.parseStructDecl(),
|
.Struct => try self.parseStructDecl(),
|
||||||
|
.Enum => try self.parseEnumDecl(),
|
||||||
|
|
||||||
else => |ttype| blk: {
|
else => |ttype| blk: {
|
||||||
self.doError("expected Fn, Const, Struct, got {}\n", ttype);
|
self.doError("expected Fn, Const, Struct, got {}\n", ttype);
|
||||||
|
|
Loading…
Reference in a new issue