parser: make internal tokens use ArrayList(Token)

This commit is contained in:
Luna 2019-08-25 23:59:01 -03:00
parent 8f7535e14e
commit 90543d1d56

View file

@ -15,16 +15,25 @@ const Node = ast.Node;
const Expr = ast.Expr; const Expr = ast.Expr;
const Stmt = ast.Stmt; const Stmt = ast.Stmt;
const TokenList = std.ArrayList(Token);
pub const Parser = struct { pub const Parser = struct {
allocator: *Allocator, allocator: *Allocator,
scanner: *Scanner, scanner: *Scanner,
tokens: TokenList,
tokens: []Token = undefined,
hadError: bool = false, hadError: bool = false,
pub fn init(allocator: *Allocator, scanner: *Scanner) Parser { pub fn init(allocator: *Allocator, scanner: *Scanner) Parser {
return Parser{ .allocator = allocator, .scanner = scanner }; return Parser{
.allocator = allocator,
.scanner = scanner,
.tokens = TokenList.init(allocator),
};
}
pub fn deinit(self: *@This()) void {
self.tokens.deinit();
} }
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) void { fn doError(self: *Parser, comptime fmt: []const u8, args: ...) void {
@ -36,11 +45,11 @@ pub const Parser = struct {
} }
fn peek(self: *Parser) Token { fn peek(self: *Parser) Token {
return self.tokens[self.tokens.len - 1]; return self.tokens.at(self.tokens.len - 1);
} }
fn previous(self: *Parser) Token { fn previous(self: *Parser) Token {
return self.tokens[self.tokens.len - 2]; return self.tokens.at(self.tokens.len - 2);
} }
fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void { fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void {
@ -73,12 +82,7 @@ pub const Parser = struct {
} }
} }
self.tokens = try self.allocator.realloc( try self.tokens.append(token);
self.tokens,
self.tokens.len + 1,
);
self.tokens[self.tokens.len - 1] = token;
std.debug.warn("skip to {}\n", token); std.debug.warn("skip to {}\n", token);
return token; return token;
} }
@ -306,7 +310,6 @@ pub const Parser = struct {
} }
pub fn parse(self: *Parser) !*ast.Node { pub fn parse(self: *Parser) !*ast.Node {
self.tokens = try self.allocator.alloc(Token, 0);
var root = try ast.mkRoot(self.allocator); var root = try ast.mkRoot(self.allocator);
var token_opt: ?Token = null; var token_opt: ?Token = null;