Compare commits
No commits in common. "36361b3d1346dd51251e7f2cf4ffc1b961d6af94" and "4479e783567cdbd50c3b07c77b9291e219f6b395" have entirely different histories.
36361b3d13
...
4479e78356
5 changed files with 3 additions and 95 deletions
|
@ -35,10 +35,12 @@ negatively charged towards
|
||||||
## wip
|
## wip
|
||||||
|
|
||||||
- no `for` yet
|
- no `for` yet
|
||||||
|
- no arrays yet
|
||||||
- no `map` yet
|
- no `map` yet
|
||||||
- no `in` yet (probably will be dropped)
|
- no `in` yet (probably will be dropped)
|
||||||
- no `module`, `import` yet
|
- no `module`, `import` yet
|
||||||
- no interfaces yet
|
- no interfaces yet
|
||||||
|
- no enums yet
|
||||||
- no generics yet
|
- no generics yet
|
||||||
- no attributes yet (`[live]`, `[skip]`)
|
- no attributes yet (`[live]`, `[skip]`)
|
||||||
- no `defer` yet
|
- no `defer` yet
|
||||||
|
|
|
@ -61,8 +61,6 @@ fn main(a int) int {
|
||||||
str.len -= 1
|
str.len -= 1
|
||||||
str.len *= 1
|
str.len *= 1
|
||||||
str.len /= 1
|
str.len /= 1
|
||||||
|
|
||||||
awoo := [1, 2, a(), b + 2, c(31) * d]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (v Typ) voidfunc() {}
|
fn (v Typ) voidfunc() {}
|
||||||
|
@ -79,9 +77,3 @@ pub mut:
|
||||||
pub mut mut:
|
pub mut mut:
|
||||||
f int
|
f int
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Color {
|
|
||||||
red green blue
|
|
||||||
cyan
|
|
||||||
alpha
|
|
||||||
}
|
|
||||||
|
|
21
src/ast.zig
21
src/ast.zig
|
@ -5,7 +5,6 @@ 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);
|
||||||
|
|
||||||
|
@ -14,7 +13,6 @@ pub const NodeType = enum {
|
||||||
FnDecl,
|
FnDecl,
|
||||||
ConstDecl,
|
ConstDecl,
|
||||||
Struct,
|
Struct,
|
||||||
Enum,
|
|
||||||
Block,
|
Block,
|
||||||
Stmt,
|
Stmt,
|
||||||
};
|
};
|
||||||
|
@ -66,7 +64,6 @@ pub const LiteralExpr = union(enum) {
|
||||||
Integer: []const u8,
|
Integer: []const u8,
|
||||||
Float: []const u8,
|
Float: []const u8,
|
||||||
String: []const u8,
|
String: []const u8,
|
||||||
Array: ExprList,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const AssignExpr = struct {
|
pub const AssignExpr = struct {
|
||||||
|
@ -241,17 +238,11 @@ 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,
|
||||||
|
|
||||||
|
@ -274,16 +265,4 @@ 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,20 +81,6 @@ 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);
|
||||||
|
@ -160,10 +146,6 @@ pub fn printExpr(expr: *Expr) void {
|
||||||
.Integer => |val| std.debug.warn("{}", val),
|
.Integer => |val| std.debug.warn("{}", val),
|
||||||
.Float => |val| std.debug.warn("{}", val),
|
.Float => |val| std.debug.warn("{}", val),
|
||||||
.String => |val| std.debug.warn("'{}'", val),
|
.String => |val| std.debug.warn("'{}'", val),
|
||||||
.Array => |exprs| {
|
|
||||||
parenthetize("array", exprs.toSlice());
|
|
||||||
},
|
|
||||||
else => |typ| std.debug.warn("UnknownLiteral-{}", typ),
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -350,17 +350,6 @@ pub const Parser = struct {
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkArray(self: *Parser, exprs: ast.ExprList) !*ast.Expr {
|
|
||||||
var expr = try self.allocator.create(Expr);
|
|
||||||
expr.* = Expr{
|
|
||||||
.Literal = ast.LiteralExpr{
|
|
||||||
.Array = exprs,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mkVariable(self: *Parser, variable: Token) !*ast.Expr {
|
fn mkVariable(self: *Parser, variable: Token) !*ast.Expr {
|
||||||
var expr = try self.allocator.create(Expr);
|
var expr = try self.allocator.create(Expr);
|
||||||
expr.* = Expr{ .Variable = variable };
|
expr.* = Expr{ .Variable = variable };
|
||||||
|
@ -582,31 +571,11 @@ 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);
|
||||||
|
@ -1031,23 +1000,7 @@ pub const Parser = struct {
|
||||||
.String => try self.mkString(lexeme),
|
.String => try self.mkString(lexeme),
|
||||||
.Identifier => try self.mkVariable(self.peek()),
|
.Identifier => try self.mkVariable(self.peek()),
|
||||||
|
|
||||||
// type checking for arrays happens at later stages
|
.LeftParen => blk: {
|
||||||
.LeftSquare => {
|
|
||||||
_ = try self.consumeSingle(.LeftSquare);
|
|
||||||
|
|
||||||
var exprs = ast.ExprList.init(self.allocator);
|
|
||||||
errdefer exprs.deinit();
|
|
||||||
|
|
||||||
while (!self.check(.RightSquare)) {
|
|
||||||
try exprs.append(try self.parseExpr());
|
|
||||||
if (self.check(.Comma)) _ = try self.consumeSingle(.Comma);
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.RightSquare);
|
|
||||||
return try self.mkArray(exprs);
|
|
||||||
},
|
|
||||||
|
|
||||||
.LeftParen => {
|
|
||||||
_ = try self.nextToken();
|
_ = try self.nextToken();
|
||||||
var expr = try self.parseExpr();
|
var expr = try self.parseExpr();
|
||||||
_ = try self.consume(.RightParen, "Expected ')' after expression");
|
_ = try self.consume(.RightParen, "Expected ')' after expression");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue