basic fndecl ast node working

This commit is contained in:
Luna 2019-07-01 15:39:48 -03:00
parent e2438f143b
commit 4f7478d7b2

View file

@ -17,7 +17,6 @@ pub const Parser = struct {
scanner: *Scanner,
tokens: []Token = undefined,
current: usize = 0,
pub fn init(allocator: *Allocator, scanner: *Scanner) Parser {
return Parser{ .allocator = allocator, .scanner = scanner };
@ -32,11 +31,11 @@ pub const Parser = struct {
}
fn peek(self: *Parser) Token {
return self.tokens[self.current];
return self.tokens[self.tokens.len - 1];
}
fn previous(self: *Parser) Token {
return self.tokens[self.current - 1];
return self.tokens[self.tokens.len - 2];
}
fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void {
@ -53,45 +52,47 @@ pub const Parser = struct {
return self.peek().ttype == .EOF;
}
fn advance(self: *Parser) Token {
if (!self.isAtEnd()) self.current += 1;
return self.previous();
}
fn check(self: *Parser, ttype: TokenType) bool {
if (self.isAtEnd()) return false;
return self.peek().ttype == ttype;
}
fn match(self: *Parser, ttypes: []TokenType) bool {
for (ttypes) |ttype| {
if (self.check(ttype)) {
_ = self.advance();
return true;
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;
}
}
return false;
self.tokens = try self.allocator.realloc(
self.tokens,
self.tokens.len + 1,
);
self.tokens[self.tokens.len - 1] = token;
return token;
}
fn matchSingle(self: *Parser, ttype: TokenType) bool {
if (self.check(ttype)) {
_ = self.advance();
return true;
}
return false;
}
fn consume(self: *Parser, ttype: TokenType, comptime msg: []const u8) Result!Token {
if (self.check(ttype)) return self.advance();
fn consume(self: *Parser, ttype: TokenType, comptime msg: []const u8) !Token {
if (self.check(ttype)) return try self.nextToken();
try self.tokenError(self.peek(), msg);
return Result.CompileError;
}
fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
if (self.check(ttype)) return self.advance();
std.debug.warn("consume {}, has {}\n", ttype, self.peek().ttype);
if (self.check(ttype)) {
var cur = self.peek();
_ = try self.nextToken();
std.debug.warn("now has {}\n", self.peek());
return cur;
}
var buf_main: [1000]u8 = undefined;
var buf = try std.fmt.bufPrint(
@ -112,9 +113,10 @@ pub const Parser = struct {
}
fn functionDecl(self: *Parser) !*ast.Node {
// get the name
var name = try self.consume(.Identifier, "expected function name");
_ = try self.consumeSingle(.Fn);
var name = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.LeftParen);
return try self.mkFnDecl(name);
}
@ -132,24 +134,16 @@ pub const Parser = struct {
pub fn parse(self: *Parser) !*ast.Node {
self.tokens = try self.allocator.alloc(Token, 0);
var i: usize = 0;
var root = try ast.mkRoot(self.allocator);
while (true) {
var tok_opt = try self.scanner.nextToken();
if (tok_opt) |token| {
self.tokens = try self.allocator.realloc(self.tokens, i + 1);
self.tokens[i] = token;
i += 1;
var token = try self.nextToken();
if (token.ttype == .EOF) break;
var node = try self.processToken(token);
std.debug.warn("{}\n", node.*);
try root.Root.append(node);
} else {
continue;
}
}
return root;