Compare commits
No commits in common. "c2499f96c90786c97ca324509ba659f61d8b08dd" and "45ae160b76e614909632baa3dfd55d7d686a6df1" have entirely different histories.
c2499f96c9
...
45ae160b76
5 changed files with 18 additions and 171 deletions
|
@ -11,9 +11,6 @@ because i can
|
||||||
- `for` is split between `for` and `loop` because my fucking god i cant stand
|
- `for` is split between `for` and `loop` because my fucking god i cant stand
|
||||||
having *four* different variations of `for`.
|
having *four* different variations of `for`.
|
||||||
|
|
||||||
- struct initialization is with `Struct.{}`, not `Struct{}`, to remove parsing
|
|
||||||
ambiguities
|
|
||||||
|
|
||||||
## how
|
## how
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -18,7 +18,6 @@ fn main(a int) int {
|
||||||
println(50)
|
println(50)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
a && b
|
a && b
|
||||||
a || b
|
a || b
|
||||||
|
|
||||||
|
@ -36,15 +35,14 @@ fn main(a int) int {
|
||||||
|
|
||||||
return 23
|
return 23
|
||||||
|
|
||||||
p := Point.{
|
//p := Point{
|
||||||
x: 10
|
// x: 10
|
||||||
y: 20
|
// y: 20
|
||||||
}
|
//}
|
||||||
|
|
||||||
//println(p.x)
|
//println(p.x)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
//struct Point {
|
||||||
x int
|
// x int
|
||||||
y int
|
// y int
|
||||||
}
|
//}
|
||||||
|
|
55
src/ast.zig
55
src/ast.zig
|
@ -12,7 +12,6 @@ pub const NodeType = enum {
|
||||||
Root,
|
Root,
|
||||||
FnDecl,
|
FnDecl,
|
||||||
ConstDecl,
|
ConstDecl,
|
||||||
Struct,
|
|
||||||
Block,
|
Block,
|
||||||
Expr,
|
Expr,
|
||||||
Stmt,
|
Stmt,
|
||||||
|
@ -77,8 +76,6 @@ pub const ExprType = enum {
|
||||||
Literal,
|
Literal,
|
||||||
Variable,
|
Variable,
|
||||||
Call,
|
Call,
|
||||||
Struct,
|
|
||||||
|
|
||||||
Grouping,
|
Grouping,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -93,18 +90,6 @@ pub const CallExpr = struct {
|
||||||
arguments: ExprList,
|
arguments: ExprList,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const StructInit = struct {
|
|
||||||
field: Token,
|
|
||||||
expr: *Expr,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const StructInitList = std.ArrayList(StructInit);
|
|
||||||
|
|
||||||
pub const StructExpr = struct {
|
|
||||||
name: Token,
|
|
||||||
inits: StructInitList,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Expr = union(ExprType) {
|
pub const Expr = union(ExprType) {
|
||||||
Assign: AssignExpr,
|
Assign: AssignExpr,
|
||||||
VarDecl: VarDecl,
|
VarDecl: VarDecl,
|
||||||
|
@ -113,7 +98,6 @@ pub const Expr = union(ExprType) {
|
||||||
Unary: UnaryExpr,
|
Unary: UnaryExpr,
|
||||||
Logical: LogicalExpr,
|
Logical: LogicalExpr,
|
||||||
Literal: LiteralExpr,
|
Literal: LiteralExpr,
|
||||||
Struct: StructExpr,
|
|
||||||
|
|
||||||
Variable: Token,
|
Variable: Token,
|
||||||
Grouping: *Expr,
|
Grouping: *Expr,
|
||||||
|
@ -200,46 +184,19 @@ pub const Stmt = union(enum) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const FieldList = std.ArrayList(StructField);
|
|
||||||
|
|
||||||
pub const StructField = struct {
|
|
||||||
name: Token,
|
|
||||||
typ: Token,
|
|
||||||
mutable: bool = true,
|
|
||||||
public: bool = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Struct = struct {
|
|
||||||
name: Token,
|
|
||||||
fields: FieldList,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Node = union(NodeType) {
|
pub const Node = union(NodeType) {
|
||||||
Root: NodeList,
|
Root: NodeList,
|
||||||
FnDecl: FnDecl,
|
FnDecl: FnDecl,
|
||||||
ConstDecl: ConstList,
|
ConstDecl: ConstList,
|
||||||
Struct: Struct,
|
|
||||||
|
|
||||||
Block: StmtList,
|
Block: StmtList,
|
||||||
|
|
||||||
Expr: *Expr,
|
Expr: *Expr,
|
||||||
Stmt: *Stmt,
|
Stmt: *Stmt,
|
||||||
|
|
||||||
pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
|
|
||||||
var node = try allocator.create(Node);
|
|
||||||
node.* = Node{ .Root = NodeList.init(allocator) };
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mkStructDecl(allocator: *std.mem.Allocator, name: Token, fields: FieldList) !*Node {
|
|
||||||
var node = try allocator.create(Node);
|
|
||||||
node.* = Node{
|
|
||||||
.Struct = Struct{
|
|
||||||
.name = name,
|
|
||||||
.fields = fields,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
|
||||||
|
var node = try allocator.create(Node);
|
||||||
|
node.* = Node{ .Root = NodeList.init(allocator) };
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
|
@ -74,31 +74,12 @@ pub fn printNode(node: *Node, ident: usize) void {
|
||||||
std.debug.warn("\n");
|
std.debug.warn("\n");
|
||||||
},
|
},
|
||||||
|
|
||||||
.Stmt => |stmt| {
|
.Stmt => |stmt| blk: {
|
||||||
printIdent(ident);
|
printIdent(ident);
|
||||||
printStmt(ident, stmt);
|
printStmt(ident, stmt);
|
||||||
std.debug.warn("\n");
|
std.debug.warn("\n");
|
||||||
},
|
},
|
||||||
|
|
||||||
.Struct => |struc| {
|
|
||||||
print(ident, "(struct {} (\n", struc.name.lexeme);
|
|
||||||
for (struc.fields.toSlice()) |field| {
|
|
||||||
printIdent(ident + 1);
|
|
||||||
if (field.mutable) {
|
|
||||||
std.debug.warn("(mut ");
|
|
||||||
} else {
|
|
||||||
std.debug.warn("(");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (field.public) {
|
|
||||||
std.debug.warn("pub ");
|
|
||||||
}
|
|
||||||
|
|
||||||
std.debug.warn("{} {})\n", field.name.lexeme, field.typ.lexeme);
|
|
||||||
}
|
|
||||||
print(ident, "))\n");
|
|
||||||
},
|
|
||||||
|
|
||||||
else => {
|
else => {
|
||||||
print(ident, "unknown node: {}\n", node);
|
print(ident, "unknown node: {}\n", node);
|
||||||
},
|
},
|
||||||
|
@ -165,18 +146,6 @@ pub fn printExpr(expr: *Expr) void {
|
||||||
std.debug.warn(")");
|
std.debug.warn(")");
|
||||||
},
|
},
|
||||||
|
|
||||||
.Struct => |val| {
|
|
||||||
std.debug.warn("({} (", val.name.lexeme);
|
|
||||||
|
|
||||||
for (val.inits.toSlice()) |init| {
|
|
||||||
std.debug.warn(" ({} ", init.field.lexeme);
|
|
||||||
printExpr(init.expr);
|
|
||||||
std.debug.warn(")");
|
|
||||||
}
|
|
||||||
|
|
||||||
std.debug.warn("))");
|
|
||||||
},
|
|
||||||
|
|
||||||
else => std.debug.warn("UnknownExpr-{}", @tagName(expr.*)),
|
else => std.debug.warn("UnknownExpr-{}", @tagName(expr.*)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -259,18 +259,6 @@ pub const Parser = struct {
|
||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mkStructExpr(self: *@This(), name: Token, args: ast.StructInitList) !*Expr {
|
|
||||||
var expr = try self.allocator.create(Expr);
|
|
||||||
expr.* = Expr{
|
|
||||||
.Struct = ast.StructExpr{
|
|
||||||
.name = name,
|
|
||||||
.inits = args,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mkBool(self: *Parser, val: bool) !*ast.Expr {
|
fn mkBool(self: *Parser, val: bool) !*ast.Expr {
|
||||||
var expr = try self.allocator.create(Expr);
|
var expr = try self.allocator.create(Expr);
|
||||||
expr.* = Expr{
|
expr.* = Expr{
|
||||||
|
@ -323,7 +311,7 @@ pub const Parser = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(self: *Parser) !*ast.Node {
|
pub fn parse(self: *Parser) !*ast.Node {
|
||||||
var root = try Node.mkRoot(self.allocator);
|
var root = try ast.mkRoot(self.allocator);
|
||||||
|
|
||||||
var token_opt: ?Token = null;
|
var token_opt: ?Token = null;
|
||||||
|
|
||||||
|
@ -401,37 +389,11 @@ pub const Parser = struct {
|
||||||
return self.mkConstDecl(consts);
|
return self.mkConstDecl(consts);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseStructDecl(self: *@This()) !*Node {
|
|
||||||
var fields = ast.FieldList.init(self.allocator);
|
|
||||||
errdefer fields.deinit();
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.Struct);
|
|
||||||
|
|
||||||
var name = try self.consumeSingle(.Identifier);
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.LeftBrace);
|
|
||||||
|
|
||||||
while (!self.check(.RightBrace)) {
|
|
||||||
// TODO mut and pub
|
|
||||||
const field_name = try self.consumeSingle(.Identifier);
|
|
||||||
const field_type = try self.consumeSingle(.Identifier);
|
|
||||||
|
|
||||||
try fields.append(ast.StructField{
|
|
||||||
.name = field_name,
|
|
||||||
.typ = field_type,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.RightBrace);
|
|
||||||
|
|
||||||
return Node.mkStructDecl(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(),
|
// TODO .Struct => try self.parseStructDecl(),
|
||||||
|
|
||||||
else => |ttype| blk: {
|
else => |ttype| blk: {
|
||||||
self.doError("(basic) expected fn/const, got {}\n", ttype);
|
self.doError("(basic) expected fn/const, got {}\n", ttype);
|
||||||
|
@ -711,21 +673,14 @@ pub const Parser = struct {
|
||||||
var expr = try self.parsePrimary();
|
var expr = try self.parsePrimary();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
std.debug.warn("maybe fncall / struct: {}\n", self.peek().ttype);
|
|
||||||
printer.printExpr(expr);
|
|
||||||
|
|
||||||
if (self.check(.LeftParen)) {
|
if (self.check(.LeftParen)) {
|
||||||
if (ast.ExprType(expr.*) != .Variable) {
|
if (ast.ExprType(expr.*) != .Variable) {
|
||||||
self.doError("cannot call non-variable {}", ast.ExprType(expr.*));
|
self.doError("cannot call non-variable{}", ast.ExprType(expr.*));
|
||||||
return Result.CompileError;
|
return Result.CompileError;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = try self.consumeSingle(.LeftParen);
|
_ = try self.consumeSingle(.LeftParen);
|
||||||
expr = try self.finishCall(expr);
|
expr = try self.finishCall(expr);
|
||||||
} else if (self.check(.Dot)) {
|
|
||||||
_ = try self.consumeSingle(.Dot);
|
|
||||||
_ = try self.consumeSingle(.LeftBrace);
|
|
||||||
expr = try self.finishStructVal(expr);
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -757,35 +712,6 @@ pub const Parser = struct {
|
||||||
return self.mkCall(callee, paren, args);
|
return self.mkCall(callee, paren, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finishStructVal(self: *@This(), expr: *Expr) !*Expr {
|
|
||||||
// <expr>{a: 10 b: 10}
|
|
||||||
// for this to work properly, <expr> must be Variable, since its a type.
|
|
||||||
if (ast.ExprType(expr.*) != .Variable) {
|
|
||||||
self.doError("Expected variable for struct type, got {}", ast.ExprType(expr.*));
|
|
||||||
return Result.CompileError;
|
|
||||||
}
|
|
||||||
|
|
||||||
var inits = ast.StructInitList.init(self.allocator);
|
|
||||||
errdefer inits.deinit();
|
|
||||||
|
|
||||||
while (!self.check(.RightBrace)) {
|
|
||||||
const field_name = try self.consumeSingle(.Identifier);
|
|
||||||
// TODO check .Comma for the quick initialization {val,val,val}
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.Colon);
|
|
||||||
const field_value = (try self.parseExpr()).Expr;
|
|
||||||
|
|
||||||
try inits.append(ast.StructInit{
|
|
||||||
.field = field_name,
|
|
||||||
.expr = field_value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.RightBrace);
|
|
||||||
|
|
||||||
return try self.mkStructExpr(expr.Variable, inits);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parsePrimary(self: *@This()) !*Expr {
|
fn parsePrimary(self: *@This()) !*Expr {
|
||||||
const curtype = self.peek().ttype;
|
const curtype = self.peek().ttype;
|
||||||
const lexeme = self.peek().lexeme;
|
const lexeme = self.peek().lexeme;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue