basic fndecl ast node working
This commit is contained in:
parent
e2438f143b
commit
4f7478d7b2
1 changed files with 35 additions and 41 deletions
|
@ -17,7 +17,6 @@ pub const Parser = struct {
|
||||||
scanner: *Scanner,
|
scanner: *Scanner,
|
||||||
|
|
||||||
tokens: []Token = undefined,
|
tokens: []Token = undefined,
|
||||||
current: usize = 0,
|
|
||||||
|
|
||||||
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 };
|
||||||
|
@ -32,11 +31,11 @@ pub const Parser = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn peek(self: *Parser) Token {
|
fn peek(self: *Parser) Token {
|
||||||
return self.tokens[self.current];
|
return self.tokens[self.tokens.len - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn previous(self: *Parser) Token {
|
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 {
|
fn tokenError(self: *Parser, token: Token, msg: []const u8) Result!void {
|
||||||
|
@ -53,45 +52,47 @@ pub const Parser = struct {
|
||||||
return self.peek().ttype == .EOF;
|
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 {
|
fn check(self: *Parser, ttype: TokenType) bool {
|
||||||
if (self.isAtEnd()) return false;
|
if (self.isAtEnd()) return false;
|
||||||
return self.peek().ttype == ttype;
|
return self.peek().ttype == ttype;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn match(self: *Parser, ttypes: []TokenType) bool {
|
fn nextToken(self: *Parser) !Token {
|
||||||
for (ttypes) |ttype| {
|
var token: Token = undefined;
|
||||||
if (self.check(ttype)) {
|
|
||||||
_ = self.advance();
|
while (true) {
|
||||||
return 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 {
|
fn consume(self: *Parser, ttype: TokenType, comptime msg: []const u8) !Token {
|
||||||
if (self.check(ttype)) {
|
if (self.check(ttype)) return try self.nextToken();
|
||||||
_ = 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();
|
|
||||||
|
|
||||||
try self.tokenError(self.peek(), msg);
|
try self.tokenError(self.peek(), msg);
|
||||||
return Result.CompileError;
|
return Result.CompileError;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
|
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_main: [1000]u8 = undefined;
|
||||||
var buf = try std.fmt.bufPrint(
|
var buf = try std.fmt.bufPrint(
|
||||||
|
@ -112,9 +113,10 @@ pub const Parser = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn functionDecl(self: *Parser) !*ast.Node {
|
fn functionDecl(self: *Parser) !*ast.Node {
|
||||||
// get the name
|
_ = try self.consumeSingle(.Fn);
|
||||||
var name = try self.consume(.Identifier, "expected function name");
|
var name = try self.consumeSingle(.Identifier);
|
||||||
_ = try self.consumeSingle(.LeftParen);
|
_ = try self.consumeSingle(.LeftParen);
|
||||||
|
|
||||||
return try self.mkFnDecl(name);
|
return try self.mkFnDecl(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,24 +134,16 @@ 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);
|
self.tokens = try self.allocator.alloc(Token, 0);
|
||||||
var i: usize = 0;
|
|
||||||
var root = try ast.mkRoot(self.allocator);
|
var root = try ast.mkRoot(self.allocator);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
var tok_opt = try self.scanner.nextToken();
|
var token = try self.nextToken();
|
||||||
|
|
||||||
if (tok_opt) |token| {
|
if (token.ttype == .EOF) break;
|
||||||
self.tokens = try self.allocator.realloc(self.tokens, i + 1);
|
|
||||||
self.tokens[i] = token;
|
|
||||||
i += 1;
|
|
||||||
|
|
||||||
if (token.ttype == .EOF) break;
|
var node = try self.processToken(token);
|
||||||
|
std.debug.warn("{}\n", node.*);
|
||||||
var node = try self.processToken(token);
|
try root.Root.append(node);
|
||||||
try root.Root.append(node);
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
|
|
Loading…
Reference in a new issue