add basic ast, parser files

This commit is contained in:
Luna 2019-06-04 22:11:11 -03:00
parent c19f6df834
commit 71cef32fb0
3 changed files with 105 additions and 5 deletions

19
src/ast.zig Normal file
View file

@ -0,0 +1,19 @@
pub const AstNodeType = enum {
Program,
};
pub const AstNode = union(AstNodeType) {
Program: []AstNode,
};
pub fn printNode(stdout: var, node: AstNode) anyerror!void {
switch (node) {
.Program => |children| try printNodes(stdout, children),
}
}
fn printNodes(stdout: var, nodes: []AstNode) anyerror!void {
for (nodes) |node| {
try printNode(stdout, node);
}
}

View file

@ -1 +1,72 @@
pub const Parser = struct {};
const std = @import("std");
const scanners = @import("scanner.zig");
const main = @import("main.zig");
const ast = @import("ast.zig");
const tokens = @import("tokens.zig");
const Allocator = std.mem.Allocator;
const Scanner = scanners.Scanner;
const AstNode = ast.AstNode;
const Token = tokens.Token;
const TokenType = tokens.TokenType;
const Result = main.Result;
pub const Parser = struct {
allocator: *Allocator,
scanner: *Scanner,
current: Token = undefined,
root: AstNode = undefined,
pub fn init(allocator: *Allocator, scanner: *Scanner) Parser {
return Parser{ .allocator = allocator, .scanner = scanner };
}
fn doError(self: *Parser, comptime fmt: []const u8, args: ...) !void {
std.debug.warn("parser error at line {}\n\t", self.scanner.line);
std.debug.warn(fmt, args);
std.debug.warn("\n");
return Result.CompileError;
}
fn advance(self: *Parser) !void {
var tok_opt = try self.scanner.nextToken();
if (tok_opt) |tok| {
self.current = tok;
}
}
fn accept(self: *Parser, ttype: TokenType) !bool {
if (self.current.ttype == ttype) {
try self.advance();
return true;
} else {
return false;
}
}
fn expect(self: *Parser, ttype: TokenType) !void {
if (!try self.accept(ttype)) {
try self.doError("expected {x}, got {}", ttype, self.current.ttype);
}
}
fn program(self: *Parser) !void {
try self.advance();
try self.advance();
try self.advance();
try self.advance();
try self.expect(.EOF);
}
pub fn parse(self: *Parser) !AstNode {
self.root = AstNode{
.Program = try self.allocator.alloc(AstNode, 0),
};
try self.program();
return self.root;
}
};

View file

@ -1,21 +1,22 @@
const std = @import("std");
const scanners = @import("scanner.zig");
const parsers = @import("parser.zig");
const main = @import("main.zig");
const ast = @import("ast.zig");
const Allocator = std.mem.Allocator;
const Result = main.Result;
const Parser = parsers.Parser;
pub const Runner = struct {
allocator: *Allocator,
stdout: main.StdOut,
pub fn init(allocator: *Allocator, stdout: var) Runner {
pub fn init(allocator: *Allocator, stdout: main.StdOut) Runner {
return Runner{ .allocator = allocator, .stdout = stdout };
}
pub fn execute(self: *Runner, code: []u8) !void {
var scanner = scanners.Scanner.init(self.allocator, code);
fn testScanner(self: *Runner, scanner: *scanners.Scanner) !void {
while (true) {
var tok_opt = scanner.nextToken() catch |err| {
try self.stdout.print(
@ -32,7 +33,16 @@ pub const Runner = struct {
try self.stdout.print("{x}\n", tok);
}
}
}
pub fn execute(self: *Runner, code: []u8) !void {
var scanner = scanners.Scanner.init(self.allocator, code);
try self.testScanner(&scanner);
scanner = scanners.Scanner.init(self.allocator, code);
var parser = Parser.init(self.allocator, &scanner);
var tree = try parser.parse();
try ast.printNode(self.stdout, tree);
return Result.Ok;
}
};