add array parsing

This commit is contained in:
Luna 2019-08-27 12:19:10 -03:00
parent 4479e78356
commit 6ab7cdbe20
4 changed files with 35 additions and 1 deletions

View file

@ -61,6 +61,8 @@ 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() {}

View file

@ -64,6 +64,7 @@ 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 {

View file

@ -146,6 +146,10 @@ 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),
} }
}, },

View file

@ -350,6 +350,17 @@ 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 };
@ -1000,7 +1011,23 @@ 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()),
.LeftParen => blk: { // type checking for arrays happens at later stages
.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");