fix for latest zig

This commit is contained in:
Luna 2019-09-18 16:53:44 -03:00
parent 927c0f6a1c
commit 180444a5d6
3 changed files with 42 additions and 23 deletions

View file

@ -2,9 +2,10 @@ const std = @import("std");
const tokens = @import("tokens.zig");
const Token = tokens.Token;
pub const NodeList = std.ArrayList(*Node);
pub const StmtList = std.ArrayList(*Stmt);
pub const ExprList = std.ArrayList(*Expr);
pub const NodeList = std.ArrayList(Node);
pub const StmtList = std.ArrayList(Stmt);
pub const Block = std.ArrayList(Stmt);
pub const ExprList = std.ArrayList(Expr);
pub const TokenList = std.ArrayList(Token);
pub const ParamList = std.ArrayList(ParamDecl);
pub const ConstList = std.ArrayList(SingleConst);
@ -145,8 +146,6 @@ pub const Expr = union(ExprType) {
Set: SetExpr,
};
pub const Block = std.ArrayList(*Stmt);
pub const IfStmt = struct {
condition: *Expr,
then_branch: Block,

View file

@ -23,7 +23,7 @@ fn printBlock(ident: usize, block: var, endNewline: bool) void {
for (block.toSlice()) |stmt| {
printIdent(ident);
printStmt(ident, stmt);
printStmt(ident, &stmt);
std.debug.warn("\n");
}
@ -34,7 +34,7 @@ fn printBlock(ident: usize, block: var, endNewline: bool) void {
}
}
pub fn printNode(node: *Node, ident: usize) void {
pub fn printNode(node: *const Node, ident: usize) void {
switch (node.*) {
.FnDecl => |decl| {
const name = decl.func_name.lexeme;
@ -97,7 +97,7 @@ pub fn printNode(node: *Node, ident: usize) void {
.Root => {
for (node.Root.toSlice()) |child| {
printNode(child, ident + 1);
printNode(&child, ident + 1);
}
},
@ -136,23 +136,43 @@ pub fn printNode(node: *Node, ident: usize) void {
}
}
fn parenthetize(name: []const u8, exprs: []*Expr) void {
fn parenthetize(name: []const u8, exprs: []const Expr) void {
std.debug.warn("({}", name);
for (exprs) |expr| {
std.debug.warn(" ");
printExpr(expr);
printExpr(&expr);
}
std.debug.warn(")");
}
pub fn printExpr(expr: *Expr) void {
fn printTwoExprs(expr_a: *const Expr, expr_b: *const Expr) void {
std.debug.warn(" ");
printExpr(expr_a);
std.debug.warn(" ");
printExpr(expr_b);
}
fn printBinOp(inner: var) void {
std.debug.warn("({}", inner.op.lexeme);
printTwoExprs(inner.left, inner.right);
std.debug.warn(")");
}
fn printSingleOp(tok: []const u8, applied: *const Expr) void {
std.debug.warn("({}", tok);
printExpr(applied);
std.debug.warn(")");
}
pub fn printExpr(expr: *const Expr) void {
switch (expr.*) {
.Binary => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
.Logical => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
.Unary => |unary| parenthetize(unary.op.lexeme, &[_]*Expr{unary.right}),
.Grouping => |expr_ptr| parenthetize("group", &[_]*Expr{expr_ptr}),
.Binary => |binary| printBinOp(binary),
.Logical => |logical| printBinOp(logical),
.Unary => |unary| printSingleOp(unary.op.lexeme, unary.right),
.Grouping => |expr_ptr| printSingleOp("group", expr_ptr),
.Literal => |literal| {
switch (literal) {
@ -194,7 +214,7 @@ pub fn printExpr(expr: *Expr) void {
for (call.arguments.toSlice()) |arg| {
std.debug.warn(" ");
printExpr(arg);
printExpr(&arg);
}
std.debug.warn(")");
@ -230,9 +250,9 @@ pub fn printExpr(expr: *Expr) void {
}
}
pub fn printStmt(ident: usize, stmt: *Stmt) void {
pub fn printStmt(ident: usize, stmt: *const Stmt) void {
switch (stmt.*) {
.Println => |expr| parenthetize("println", &[_]*Expr{expr}),
.Println => |expr| printSingleOp("println", expr),
.Expr => |expr| printExpr(expr),
.If => |ifstmt| {

View file

@ -385,7 +385,7 @@ pub const Parser = struct {
if (token.typ == .EOF) break;
var node = try self.parseTopDecl();
try root.Root.append(node);
try root.Root.append(node.*);
}
if (self.hadError) {
@ -625,7 +625,7 @@ pub const Parser = struct {
while (self.peek().typ != .RightBrace) {
var stmt = try self.parseStmt();
printer.printNode(try self.mkStmt(stmt), 0);
try stmts.append(stmt);
try stmts.append(stmt.*);
}
_ = try self.consumeSingle(.RightBrace);
@ -1012,13 +1012,13 @@ pub const Parser = struct {
// emulating do-while really badly
var arg = try self.parseExpr();
try args.append(arg);
try args.append(arg.*);
while (self.check(.Comma)) {
_ = try self.consumeSingle(.Comma);
arg = try self.parseExpr();
try args.append(arg);
try args.append(arg.*);
}
}
@ -1077,7 +1077,7 @@ pub const Parser = struct {
errdefer exprs.deinit();
while (!self.check(.RightSquare)) {
try exprs.append(try self.parseExpr());
try exprs.append((try self.parseExpr()).*);
if (self.check(.Comma)) _ = try self.consumeSingle(.Comma);
}