vig/src/parser.zig

1105 lines
31 KiB
Zig
Raw Permalink Normal View History

2019-06-05 01:11:11 +00:00
const std = @import("std");
const scanners = @import("scanner.zig");
const main = @import("main.zig");
const ast = @import("ast.zig");
const tokens = @import("tokens.zig");
2019-06-06 01:06:12 +00:00
const err = @import("errors.zig");
const printer = @import("ast_printer.zig");
2019-06-05 01:11:11 +00:00
const Allocator = std.mem.Allocator;
const Scanner = scanners.Scanner;
const Token = tokens.Token;
const TokenType = tokens.TokenType;
const Result = main.Result;
2019-08-24 01:13:26 +00:00
const Node = ast.Node;
2019-08-24 01:13:26 +00:00
const Expr = ast.Expr;
2019-08-24 19:00:22 +00:00
const Stmt = ast.Stmt;
2019-06-06 01:06:12 +00:00
const TokenList = std.ArrayList(Token);
2019-08-27 00:31:01 +00:00
const FieldState = struct {
public: bool = false,
mutable: bool = false,
mutable_outside: bool = false,
};
2019-06-05 01:11:11 +00:00
pub const Parser = struct {
allocator: *Allocator,
scanner: *Scanner,
tokens: TokenList,
2019-06-05 01:11:11 +00:00
2019-08-23 14:57:49 +00:00
hadError: bool = false,
2019-06-05 01:11:11 +00:00
pub fn init(allocator: *Allocator, scanner: *Scanner) Parser {
return Parser{
.allocator = allocator,
.scanner = scanner,
.tokens = TokenList.init(allocator),
};
}
pub fn deinit(self: *@This()) void {
self.tokens.deinit();
2019-06-05 01:11:11 +00:00
}
2020-07-23 19:38:26 +00:00
fn doError(self: *Parser, comptime fmt: []const u8, args: anytype) void {
2019-08-23 14:57:49 +00:00
self.hadError = true;
2020-04-10 19:48:10 +00:00
std.debug.warn("parser error at line {}\n\t", .{self.scanner.line});
2019-06-05 01:11:11 +00:00
std.debug.warn(fmt, args);
2020-04-10 19:48:10 +00:00
std.debug.warn("\n", .{});
2019-06-05 01:11:11 +00:00
}
2019-06-06 01:06:12 +00:00
fn peek(self: *Parser) Token {
2020-04-10 19:48:10 +00:00
return self.tokens.items[self.tokens.items.len - 1];
2019-06-06 01:06:12 +00:00
}
fn previous(self: *Parser) Token {
2020-04-10 19:48:10 +00:00
return self.tokens.items[self.tokens.items.len - 2];
2019-06-06 01:06:12 +00:00
}
fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void {
if (token.ttype == .EOF) {
err.report(token.line, " at end", msg);
} else {
2020-04-10 19:48:10 +00:00
err.reportFmt(token.line, " at '{}': {}", .{ token.lexeme, msg });
2019-06-05 01:11:11 +00:00
}
2019-06-06 01:06:12 +00:00
return Result.CompileError;
2019-06-05 01:11:11 +00:00
}
2019-06-06 01:06:12 +00:00
fn isAtEnd(self: *Parser) bool {
return self.peek().ttype == .EOF;
}
fn check(self: *Parser, ttype: TokenType) bool {
if (self.isAtEnd()) return false;
return self.peek().ttype == ttype;
}
2019-07-01 18:39:48 +00:00
fn nextToken(self: *Parser) !Token {
var token: Token = undefined;
while (true) {
var next_token_opt = try self.scanner.nextToken();
if (next_token_opt) |token_nice| {
token = token_nice;
break;
2019-06-06 01:06:12 +00:00
}
}
try self.tokens.append(token);
2020-04-10 19:48:10 +00:00
std.debug.warn("skip to {}\n", .{token});
2019-07-01 18:39:48 +00:00
return token;
2019-06-06 01:06:12 +00:00
}
2019-08-26 18:29:59 +00:00
/// Consume the current token type, then walk to the next token.
/// Returns the consumed token.
2019-07-01 18:39:48 +00:00
fn consume(self: *Parser, ttype: TokenType, comptime msg: []const u8) !Token {
2019-08-26 16:15:08 +00:00
if (self.check(ttype)) {
var tok = self.peek();
_ = try self.nextToken();
return tok;
}
2019-06-06 01:06:12 +00:00
try self.tokenError(self.peek(), msg);
return Result.CompileError;
2019-06-05 01:11:11 +00:00
}
2019-08-26 18:29:59 +00:00
/// Consume the current token. Gives default error messages
2019-07-01 18:25:07 +00:00
fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
2019-07-01 18:39:48 +00:00
if (self.check(ttype)) {
var cur = self.peek();
_ = try self.nextToken();
return cur;
}
2019-07-01 18:25:07 +00:00
2019-08-26 18:29:59 +00:00
// TODO maybe this could be entirely comptime?
2019-07-01 18:25:07 +00:00
var buf_main: [1000]u8 = undefined;
2020-04-10 19:48:10 +00:00
var buf = try std.fmt.bufPrint(buf_main[0..], "expected {}, got {}", .{
2019-07-01 18:25:07 +00:00
ttype,
self.peek().ttype,
2020-04-10 19:48:10 +00:00
});
2019-07-01 18:25:07 +00:00
2019-08-26 18:29:59 +00:00
try self.tokenError(self.peek(), buf);
2019-07-01 18:25:07 +00:00
return Result.CompileError;
}
2019-08-26 18:29:59 +00:00
/// check() against multiple tokens
2020-04-10 19:48:10 +00:00
fn compareAnyOf(self: *@This(), ttypes: []const TokenType) bool {
2019-08-24 01:13:26 +00:00
for (ttypes) |ttype| {
2019-08-24 14:07:23 +00:00
if (self.check(ttype)) return true;
2019-08-24 01:13:26 +00:00
}
return false;
}
// TODO maybe move helper functions to ast_helper.zig?
fn mkFnDecl(
self: *Parser,
name: Token,
params: ast.ParamList,
return_type: Token,
block: ast.StmtList,
2019-08-26 23:31:15 +00:00
method: ?*ast.MethodData,
) !*ast.Node {
2019-07-01 18:25:07 +00:00
var node = try self.allocator.create(Node);
2019-07-01 18:55:19 +00:00
node.* = Node{
.FnDecl = ast.FnDecl{
.func_name = name,
.params = params,
.return_type = return_type,
2019-08-24 14:07:23 +00:00
.body = block,
2019-08-26 23:31:15 +00:00
.method = method,
2019-07-01 18:55:19 +00:00
},
};
return node;
2019-06-06 01:06:12 +00:00
}
fn mkConstDecl(self: *Parser, consts: ast.ConstList) !*ast.Node {
var node = try self.allocator.create(Node);
node.* = Node{ .ConstDecl = consts };
return node;
}
2019-07-01 18:55:19 +00:00
2019-08-24 19:00:22 +00:00
fn mkBlock(self: *Parser, stmts: ast.StmtList) !*ast.Node {
2019-08-24 01:13:26 +00:00
var node = try self.allocator.create(Node);
2019-08-24 19:00:22 +00:00
node.* = Node{ .Block = stmts };
2019-08-24 01:13:26 +00:00
return node;
}
2019-08-24 19:00:22 +00:00
fn mkStmt(self: *Parser, stmt: *Stmt) !*ast.Node {
var node = try self.allocator.create(Node);
node.* = Node{ .Stmt = stmt };
return node;
}
fn mkStmtExpr(self: *Parser, expr: *Expr) !*Stmt {
var stmt = try self.allocator.create(Stmt);
stmt.* = Stmt{ .Expr = expr };
return stmt;
}
2019-08-24 01:13:26 +00:00
fn mkGrouping(self: *Parser, expr: *Expr) !*ast.Expr {
var grouping = try self.allocator.create(Expr);
grouping.* = Expr{ .Grouping = expr };
return grouping;
}
fn mkUnary(self: *Parser, op: Token, right: *Expr) !*Expr {
2020-04-10 19:48:10 +00:00
std.debug.warn("Unary\n", .{});
2019-08-24 14:07:23 +00:00
2019-08-24 01:13:26 +00:00
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Unary = ast.UnaryExpr{
.op = op,
.right = right,
},
};
return expr;
}
fn mkBinary(self: *Parser, left: *Expr, op: Token, right: *Expr) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Binary = ast.BinaryExpr{
.left = left,
.op = op,
.right = right,
},
};
return expr;
}
2019-08-25 16:21:22 +00:00
fn mkLogical(self: *Parser, left: *Expr, op: Token, right: *Expr) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Logical = ast.LogicalExpr{
.left = left,
.op = op,
.right = right,
},
};
return expr;
}
2019-08-25 13:27:50 +00:00
fn mkAssign(self: *Parser, name: Token, value: *Expr) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Assign = ast.AssignExpr{
.name = name,
.value = value,
},
};
return expr;
}
fn mkVarDecl(self: *@This(), name: Token, value: *Expr, mutable: bool) !*Expr {
var vardecl = try self.allocator.create(Expr);
vardecl.* = Expr{
.VarDecl = ast.VarDecl{
.assign = ast.AssignExpr{
.name = name,
.value = value,
},
.mutable = mutable,
},
};
return vardecl;
}
2019-08-26 01:41:25 +00:00
fn mkCall(self: *@This(), callee: *Expr, paren: Token, args: ast.ExprList) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Call = ast.CallExpr{
.callee = callee,
.paren = paren,
.arguments = args,
},
};
return expr;
}
2019-08-26 14:59:32 +00:00
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;
}
2019-08-26 16:15:08 +00:00
fn mkGet(self: *@This(), struc: *Expr, name: Token) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Get = ast.GetExpr{
.struc = struc,
.name = name,
},
};
return expr;
}
2019-08-26 16:29:45 +00:00
fn mkSet(self: *@This(), struc: *Expr, field: Token, value: *Expr) !*Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Set = ast.SetExpr{
.struc = struc,
.field = field,
.value = value,
},
};
return expr;
}
2019-08-24 01:13:26 +00:00
fn mkBool(self: *Parser, val: bool) !*ast.Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Literal = ast.LiteralExpr{
.Bool = val,
},
};
return expr;
}
fn mkInteger(self: *Parser, val: []const u8) !*ast.Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Literal = ast.LiteralExpr{
.Integer = val,
},
};
return expr;
}
fn mkFloat(self: *Parser, val: []const u8) !*ast.Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Literal = ast.LiteralExpr{
.Float = val,
},
};
return expr;
}
fn mkString(self: *Parser, val: []const u8) !*ast.Expr {
var expr = try self.allocator.create(Expr);
expr.* = Expr{
.Literal = ast.LiteralExpr{
.String = val,
},
};
return expr;
}
2019-08-27 15:19:10 +00:00
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 {
var expr = try self.allocator.create(Expr);
expr.* = Expr{ .Variable = variable };
return expr;
}
2019-08-24 01:13:26 +00:00
pub fn parse(self: *Parser) !*ast.Node {
2019-08-26 13:49:43 +00:00
var root = try Node.mkRoot(self.allocator);
2019-08-24 01:13:26 +00:00
var token_opt: ?Token = null;
while (true) {
if (token_opt == null) {
token_opt = try self.nextToken();
} else {
token_opt = self.peek();
}
var token = token_opt.?;
if (token.ttype == .EOF) break;
var node = try self.parseTopDecl();
2019-08-26 12:54:01 +00:00
try root.Root.append(node);
2019-08-24 01:13:26 +00:00
}
if (self.hadError) {
return error.ParseError;
}
return root;
}
/// Copy a token with a different lexeme.
fn mkToken(self: *@This(), ttype: TokenType, lexeme: []const u8, line: usize) !Token {
const owned_lexeme = try std.mem.dupe(self.allocator, u8, lexeme);
return Token{
.ttype = ttype,
.lexeme = owned_lexeme,
.line = line,
};
}
2019-08-26 12:54:01 +00:00
fn parseFnDecl(self: *@This()) !*Node {
var param_list = ast.ParamList.init(self.allocator);
errdefer param_list.deinit();
2019-08-26 23:31:15 +00:00
var method: ?*ast.MethodData = null;
_ = try self.consumeSingle(.Fn);
if (self.check(.LeftParen)) {
2019-08-26 23:31:15 +00:00
method = try self.parsePreMethod();
}
2019-08-24 14:07:23 +00:00
const name = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.LeftParen);
2019-08-23 18:52:04 +00:00
while (self.peek().ttype != .RightParen) {
const param_name = try self.consumeSingle(.Identifier);
const param_type = try self.consumeSingle(.Identifier);
2019-08-23 18:52:04 +00:00
try param_list.append(ast.ParamDecl{
.name = param_name,
.typ = param_type,
});
}
2019-08-23 18:52:04 +00:00
_ = try self.consumeSingle(.RightParen);
2019-08-23 18:52:04 +00:00
// the return type is default void if a type
// is not provided
var return_type: Token = undefined;
if (self.check(.Identifier)) {
return_type = try self.consumeSingle(.Identifier);
} else {
return_type = try self.mkToken(.Identifier, "void", name.line);
}
var block_node = try self.parseBlock();
2019-08-26 23:31:15 +00:00
return try self.mkFnDecl(name, param_list, return_type, block_node.Block, method);
}
/// parse the (v [mut] T) part of the method (defined here
/// as a premethod)
fn parsePreMethod(self: *@This()) !?*ast.MethodData {
_ = try self.consumeSingle(.LeftParen);
var mutable_ref: bool = false;
const variable = try self.consumeSingle(.Identifier);
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
mutable_ref = true;
}
const typ = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.RightParen);
// create method data and assign the values we got into it
var method = try self.allocator.create(ast.MethodData);
method.* = ast.MethodData{
.variable = variable,
.typ = typ,
.mutable = mutable_ref,
};
return method;
}
2019-08-23 18:52:04 +00:00
2019-08-26 12:54:01 +00:00
fn parseConstDecl(self: *@This()) !*Node {
var consts = ast.ConstList.init(self.allocator);
errdefer consts.deinit();
_ = try self.consumeSingle(.Const);
_ = try self.consumeSingle(.LeftParen);
while (self.peek().ttype != .RightParen) {
const const_name = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.Equal);
// const declarations dont have type, a future type system must
// check the output type of the expression and assign it to the
// const later on.
2019-08-24 19:00:22 +00:00
var expr = try self.parseExpr();
try consts.append(ast.SingleConst{
.name = const_name,
.expr = expr,
2019-08-24 19:00:22 +00:00
});
}
_ = try self.consumeSingle(.RightParen);
return self.mkConstDecl(consts);
}
2019-08-26 13:49:43 +00:00
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);
2019-08-27 00:31:01 +00:00
var field_state = FieldState{};
2019-08-26 13:49:43 +00:00
while (!self.check(.RightBrace)) {
try self.parseFieldModifiers(&field_state);
2019-08-27 00:31:01 +00:00
2019-08-26 13:49:43 +00:00
const field_name = try self.consumeSingle(.Identifier);
const field_type = try self.consumeSingle(.Identifier);
2019-08-27 00:31:01 +00:00
// we could create a FieldState on the heap and copy our current
// field state into a StructField.state, but copying via this makes
// things so much nicer.
2019-08-26 13:49:43 +00:00
try fields.append(ast.StructField{
.name = field_name,
.typ = field_type,
2019-08-27 00:31:01 +00:00
.mutable = field_state.mutable,
.public = field_state.public,
.mutable_outside = field_state.mutable_outside,
2019-08-26 13:49:43 +00:00
});
}
_ = try self.consumeSingle(.RightBrace);
return Node.mkStructDecl(self.allocator, name, fields);
}
fn parseFieldModifiers(self: *@This(), field_state: *FieldState) !void {
// there are five access modifiers:
// - none (private immutable)
// - mut (private mutable)
// - pub (public immutable)
// - pub mut (public mutable only in module)
// - pub mut mut (public mutable everywhere)
// this function takes care of that by changing the current FieldState
// to what the modifiers dictate.
switch (self.peek().ttype) {
.Mut => {
// There are no oher modifiers that start with mut, so we
// can just go the way of marking it as mutable
_ = try self.consumeSingle(.Mut);
_ = try self.consumeSingle(.Colon);
field_state.mutable = true;
},
// 'pub', 'pub mut', and 'pub mut mut' are all handled here
.Pub => {
_ = try self.consumeSingle(.Pub);
field_state.public = true;
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
field_state.mutable = true;
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
field_state.mutable_outside = true;
}
}
_ = try self.consumeSingle(.Colon);
},
// if it isn't mut or pub we're likely in an identifier, just
// ignore it.
else => return,
}
}
2019-08-27 15:31:02 +00:00
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);
}
2019-08-26 12:54:01 +00:00
fn parseTopDecl(self: *@This()) !*Node {
return switch (self.peek().ttype) {
2019-08-24 01:13:26 +00:00
.Fn => try self.parseFnDecl(),
.Const => try self.parseConstDecl(),
2019-08-26 13:49:43 +00:00
.Struct => try self.parseStructDecl(),
2019-08-27 15:31:02 +00:00
.Enum => try self.parseEnumDecl(),
2019-08-23 18:52:04 +00:00
2019-08-23 14:57:49 +00:00
else => |ttype| blk: {
2020-04-10 19:48:10 +00:00
self.doError("expected Fn, Const, Struct, got {}\n", .{ttype});
2019-08-26 12:54:01 +00:00
return Result.CompileError;
2019-08-23 14:57:49 +00:00
},
};
}
2019-08-26 17:59:14 +00:00
fn parseBlockInternal(self: *@This(), comptime T: type) !T {
var stmts = T.init(self.allocator);
2019-08-24 19:00:22 +00:00
errdefer stmts.deinit();
2019-06-06 01:06:12 +00:00
2019-08-24 14:07:23 +00:00
_ = try self.consumeSingle(.LeftBrace);
2019-08-24 14:07:23 +00:00
while (self.peek().ttype != .RightBrace) {
2019-08-26 17:59:14 +00:00
var stmt = try self.parseStmt();
printer.printNode(try self.mkStmt(stmt), 0);
2019-08-24 20:46:42 +00:00
try stmts.append(stmt);
2019-08-24 14:07:23 +00:00
}
2019-08-24 19:00:22 +00:00
2019-08-24 01:13:26 +00:00
_ = try self.consumeSingle(.RightBrace);
2019-08-26 17:59:14 +00:00
return stmts;
2019-08-24 20:46:42 +00:00
}
fn parseStmt(self: *@This()) anyerror!*Stmt {
return switch (self.peek().ttype) {
2019-08-25 16:02:40 +00:00
.If => try self.parseIfStmt(),
2019-08-25 20:14:25 +00:00
.Loop => try self.parseLoop(),
2019-08-29 17:30:33 +00:00
.For => try self.parseForStmt(),
2019-08-24 19:00:22 +00:00
.Println => try self.parsePrintln(),
2019-08-26 02:16:13 +00:00
.Return => try self.parseReturn(),
2019-08-24 19:00:22 +00:00
else => try self.parseStmtExpr(),
};
}
2019-08-26 17:59:14 +00:00
/// Parse a list of statements.
fn parseBlock(self: *@This()) !*Node {
var stmts = try self.parseBlockInternal(ast.StmtList);
return try self.mkBlock(stmts);
}
2019-08-25 16:02:40 +00:00
2019-08-26 17:59:14 +00:00
/// parse blocks inside statements
fn parseStmtBlock(self: *@This()) !ast.Block {
var block = try self.parseBlockInternal(ast.Block);
return block;
2019-08-25 16:02:40 +00:00
}
fn parseIfStmt(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.If);
var condition = try self.parseExpr();
2019-08-25 16:02:40 +00:00
const then_branch = try self.parseStmtBlock();
2019-08-25 16:02:40 +00:00
var else_branch: ?ast.Block = null;
2019-08-25 16:02:40 +00:00
if (self.check(.Else)) {
_ = try self.consumeSingle(.Else);
else_branch = try self.parseStmtBlock();
2019-08-25 16:02:40 +00:00
}
return try Stmt.mkIfStmt(
self.allocator,
condition,
then_branch,
else_branch,
);
}
2019-08-29 17:30:33 +00:00
fn parseForStmt(self: *@This()) !*Stmt {
// There are two types of for in vig's V subset:
// - for x in y
// - for idx, x in y
_ = try self.consumeSingle(.For);
var index_var: ?Token = null;
var value_var: Token = undefined;
const subject_1 = try self.consumeSingle(.Identifier);
if (self.check(.Comma)) {
_ = try self.consumeSingle(.Comma);
const subject_2 = try self.consumeSingle(.Identifier);
index_var = subject_1;
value_var = subject_2;
} else {
value_var = subject_1;
}
_ = try self.consumeSingle(.In);
// MUST be identifier
var array = try self.consumeSingle(.Identifier);
var block = try self.parseStmtBlock();
return try Stmt.mkFor(
self.allocator,
index_var,
value_var,
array,
block,
);
}
2019-08-25 20:14:25 +00:00
fn parseLoop(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Loop);
var expr: ?*Expr = null;
var body: ast.Block = undefined;
2019-08-26 18:19:31 +00:00
// 'loop {' = infinite loop
2019-08-25 20:14:25 +00:00
if (self.check(.LeftBrace)) {
body = try self.parseStmtBlock();
} else {
expr = try self.parseExpr();
2019-08-25 20:14:25 +00:00
body = try self.parseStmtBlock();
}
return try Stmt.mkLoop(self.allocator, expr, body);
}
2019-08-26 02:16:13 +00:00
fn parseReturn(self: *@This()) !*Stmt {
const tok = try self.consumeSingle(.Return);
const expr = try self.parseExpr();
2019-08-26 02:16:13 +00:00
return try Stmt.mkReturn(self.allocator, tok, expr);
}
2019-08-24 19:00:22 +00:00
fn parsePrintln(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Println);
2019-08-24 19:56:58 +00:00
_ = try self.consumeSingle(.LeftParen);
var expr = try self.parseExpr();
2019-08-24 19:56:58 +00:00
_ = try self.consumeSingle(.RightParen);
2019-08-26 02:16:13 +00:00
return try Stmt.mkPrintln(self.allocator, expr);
2019-08-24 19:00:22 +00:00
}
fn parseStmtExpr(self: *@This()) !*Stmt {
var expr = try self.parseExpr();
2019-08-24 19:00:22 +00:00
return try self.mkStmtExpr(expr);
2019-08-24 01:13:26 +00:00
}
fn parseExpr(self: *@This()) anyerror!*Expr {
return try self.parseAssignment();
2019-08-24 01:13:26 +00:00
}
2019-08-25 13:27:50 +00:00
fn parseAssignment(self: *@This()) anyerror!*Expr {
2019-08-26 18:19:31 +00:00
// there can be two assignments coming out of this function:
// - a mutable/immutable variable declaration with :=
// - an assignment to a variable with =, +=, -=
// one is a statement, other is an expression. since the normal result
// of this is an Expr, we wrap variable assignments in an Expr as well.
var mutable: bool = false;
if (self.check(.Mut)) {
_ = try self.consumeSingle(.Mut);
mutable = true;
}
2019-08-25 16:21:22 +00:00
var expr = try self.parseOr();
if (self.compareAnyOf(&[_]TokenType{
.ColonEqual, .Equal, .PlusEqual, .MinusEqual, .StarEqual,
2019-08-27 14:58:32 +00:00
.SlashEqual,
})) {
2019-08-26 18:19:31 +00:00
return try self.finishAssignment(expr, mutable);
}
2019-08-25 13:27:50 +00:00
2019-08-26 18:19:31 +00:00
return expr;
}
2019-08-26 16:29:45 +00:00
2019-08-26 18:19:31 +00:00
fn finishAssignment(self: *@This(), expr: *Expr, mutable: bool) !*Expr {
var op = self.peek();
_ = try self.nextToken();
var value = try self.parseAssignment();
// TODO convert binary's op field from Token to
// something else, maybe enum'd
const new_op_ttype: TokenType = switch (op.ttype) {
.ColonEqual => TokenType.ColonEqual,
.Equal => .Equal,
.PlusEqual => .Plus,
.MinusEqual => .Minus,
.StarEqual => .Star,
2019-08-27 14:58:32 +00:00
.SlashEqual => .Slash,
else => unreachable,
};
2019-08-27 14:58:32 +00:00
// we create new_lexeme so that
// the AST printer properly prints
// x += 1
// as
// (set x (+ x 1))
// and not
// (set x (+= x 1))
const new_lexeme: []const u8 = switch (op.ttype) {
.ColonEqual => ":=",
.Equal => "=",
2019-08-27 14:58:32 +00:00
.PlusEqual => "+",
.MinusEqual => "-",
.StarEqual => "*",
2019-08-27 14:58:32 +00:00
.SlashEqual => "/",
else => unreachable,
};
2019-08-26 18:19:31 +00:00
switch (expr.*) {
.Variable => {
switch (op.ttype) {
.ColonEqual => return try self.mkVarDecl(expr.Variable, value, mutable),
.Equal => return try self.mkAssign(expr.Variable, value),
.PlusEqual, .MinusEqual, .StarEqual, .SlashEqual => {
var new_op = try self.mkToken(new_op_ttype, new_lexeme, op.line);
return try self.mkAssign(
expr.Variable,
try self.mkBinary(expr, new_op, value),
);
},
2019-08-26 18:19:31 +00:00
else => unreachable,
}
},
2019-08-26 16:29:45 +00:00
2019-08-26 18:19:31 +00:00
.Get => |get| {
switch (op.ttype) {
.ColonEqual => {
2020-04-10 19:48:10 +00:00
self.doError("can not initialize struct field", .{});
return Result.CompileError;
},
.Equal => return try self.mkSet(get.struc, get.name, value),
.PlusEqual, .MinusEqual, .StarEqual, .SlashEqual => {
var new_op = try self.mkToken(new_op_ttype, new_lexeme, op.line);
return try self.mkSet(
get.struc,
get.name,
try self.mkBinary(expr, new_op, value),
);
},
else => unreachable,
2019-08-26 18:19:31 +00:00
}
},
else => |expr_typ| {
2020-04-10 19:48:10 +00:00
self.doError("Invalid assignment target {}", .{expr_typ});
2019-08-26 18:19:31 +00:00
return Result.CompileError;
},
}
2019-08-25 13:27:50 +00:00
}
2019-08-25 16:21:22 +00:00
fn parseOr(self: *@This()) !*Expr {
var expr = try self.parseAnd();
while (self.check(.Or)) {
var op = self.peek();
_ = try self.nextToken();
var right = try self.parseAnd();
expr = try self.mkLogical(expr, op, right);
}
return expr;
}
fn parseAnd(self: *@This()) !*Expr {
var expr = try self.parseEquality();
while (self.check(.And)) {
var op = self.peek();
_ = try self.nextToken();
var right = try self.parseEquality();
expr = try self.mkLogical(expr, op, right);
}
return expr;
}
2019-08-24 01:13:26 +00:00
fn parseEquality(self: *@This()) !*Expr {
var expr = try self.parseComparison();
2019-08-25 16:21:22 +00:00
while (self.check(.EqualEqual)) {
2019-08-24 01:13:26 +00:00
var op = self.peek();
_ = try self.nextToken();
2019-08-24 01:13:26 +00:00
var right = try self.parseComparison();
expr = try self.mkBinary(expr, op, right);
2019-08-23 14:57:49 +00:00
}
2019-08-24 01:13:26 +00:00
return expr;
}
fn parseComparison(self: *@This()) !*Expr {
var expr = try self.parseAddition();
while (self.compareAnyOf(&[_]TokenType{
.Greater,
.GreaterEqual,
.Less,
.LessEqual,
})) {
var op = self.peek();
_ = try self.nextToken();
2019-08-24 01:13:26 +00:00
var right = try self.parseAddition();
expr = try self.mkBinary(expr, op, right);
2019-06-06 01:06:12 +00:00
}
2019-08-24 01:13:26 +00:00
return expr;
}
fn parseAddition(self: *@This()) !*Expr {
var expr = try self.parseMultiplication();
while (self.compareAnyOf(&[_]TokenType{
.Minus, .Plus,
})) {
2019-08-24 14:07:23 +00:00
var op = self.peek();
_ = try self.nextToken();
2019-08-24 01:13:26 +00:00
var right = try self.parseMultiplication();
2019-08-24 14:07:23 +00:00
expr = try self.mkBinary(expr, op, right);
2019-08-24 01:13:26 +00:00
}
return expr;
}
fn parseMultiplication(self: *@This()) !*Expr {
var expr = try self.parseUnary();
while (self.compareAnyOf(&[_]TokenType{
.Star, .Slash,
})) {
var op = self.peek();
_ = try self.nextToken();
2019-08-24 01:13:26 +00:00
var right = try self.parseUnary();
expr = try self.mkBinary(expr, op, right);
2019-08-24 01:13:26 +00:00
}
return expr;
}
fn parseUnary(self: *@This()) anyerror!*Expr {
if (self.compareAnyOf(&[_]TokenType{ .Bang, .Minus })) {
var op = self.previous();
var right = try self.parseUnary();
return try self.mkUnary(op, right);
}
2019-08-26 01:41:25 +00:00
var expr = try self.parseCall();
return expr;
}
2019-08-26 18:29:59 +00:00
/// Parse either:
/// - A function call
/// - A struct initialization (Point.{...})
/// - A struct Get expression (p.x)
2019-08-26 01:41:25 +00:00
fn parseCall(self: *@This()) !*Expr {
2019-08-24 14:07:23 +00:00
var expr = try self.parsePrimary();
2019-08-26 01:41:25 +00:00
while (true) {
if (self.check(.LeftParen)) {
_ = try self.consumeSingle(.LeftParen);
expr = try self.finishCall(expr);
2019-08-26 14:59:32 +00:00
} else if (self.check(.Dot)) {
_ = try self.consumeSingle(.Dot);
2019-08-26 16:15:08 +00:00
if (self.check(.LeftBrace)) {
_ = try self.consumeSingle(.LeftBrace);
expr = try self.finishStructVal(expr);
} else {
2019-08-26 18:29:59 +00:00
var name = try self.consume(
.Identifier,
"Expect property name after '.'",
);
2019-08-26 16:15:08 +00:00
expr = try self.mkGet(expr, name);
}
2019-08-26 01:41:25 +00:00
} else {
break;
}
}
2019-08-24 14:07:23 +00:00
return expr;
2019-08-24 01:13:26 +00:00
}
2019-08-26 01:41:25 +00:00
fn finishCall(self: *@This(), callee: *Expr) !*Expr {
var args = ast.ExprList.init(self.allocator);
errdefer args.deinit();
if (!self.check(.RightParen)) {
// emulating do-while really badly
var arg = try self.parseExpr();
2019-08-26 01:41:25 +00:00
try args.append(arg);
while (self.check(.Comma)) {
_ = try self.consumeSingle(.Comma);
arg = try self.parseExpr();
2019-08-26 01:41:25 +00:00
try args.append(arg);
}
}
var paren = try self.consume(.RightParen, "Expected ')' after arguments");
return self.mkCall(callee, paren, args);
}
2019-08-26 14:59:32 +00:00
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.
2019-12-07 22:45:12 +00:00
if (@as(ast.ExprType, expr.*) != .Variable) {
2020-04-10 19:48:10 +00:00
self.doError("Expected variable for struct type, got {}", .{@as(ast.ExprType, expr.*)});
2019-08-26 14:59:32 +00:00
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();
2019-08-26 14:59:32 +00:00
try inits.append(ast.StructInit{
.field = field_name,
.expr = field_value,
});
}
_ = try self.consumeSingle(.RightBrace);
return try self.mkStructExpr(expr.Variable, inits);
}
2019-08-24 01:13:26 +00:00
fn parsePrimary(self: *@This()) !*Expr {
const curtype = self.peek().ttype;
const lexeme = self.peek().lexeme;
2019-08-24 14:07:23 +00:00
var expr = switch (curtype) {
.False => try self.mkBool(false),
.True => try self.mkBool(true),
2019-08-24 01:13:26 +00:00
2019-08-24 14:07:23 +00:00
.Integer => try self.mkInteger(lexeme),
.Float => try self.mkFloat(lexeme),
.String => try self.mkString(lexeme),
.Identifier => try self.mkVariable(self.peek()),
2019-08-24 01:13:26 +00:00
2019-08-27 15:19:10 +00:00
// 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 => {
2019-08-24 14:07:23 +00:00
_ = try self.nextToken();
var expr = try self.parseExpr();
2019-08-24 01:13:26 +00:00
_ = try self.consume(.RightParen, "Expected ')' after expression");
2019-08-24 19:51:37 +00:00
// for groupings, we don't want to skip tokens as we already
// consumed RightParen.
return try self.mkGrouping(expr);
2019-08-24 01:13:26 +00:00
},
else => blk: {
2020-04-10 19:48:10 +00:00
self.doError("expected literal, got {}", .{curtype});
2019-08-24 01:13:26 +00:00
return Result.CompileError;
},
};
2019-08-24 14:07:23 +00:00
_ = try self.nextToken();
return expr;
2019-06-05 01:11:11 +00:00
}
};