fix for latest zig
This commit is contained in:
parent
927c0f6a1c
commit
180444a5d6
|
@ -2,9 +2,10 @@ const std = @import("std");
|
||||||
const tokens = @import("tokens.zig");
|
const tokens = @import("tokens.zig");
|
||||||
const Token = tokens.Token;
|
const Token = tokens.Token;
|
||||||
|
|
||||||
pub const NodeList = std.ArrayList(*Node);
|
pub const NodeList = std.ArrayList(Node);
|
||||||
pub const StmtList = std.ArrayList(*Stmt);
|
pub const StmtList = std.ArrayList(Stmt);
|
||||||
pub const ExprList = std.ArrayList(*Expr);
|
pub const Block = std.ArrayList(Stmt);
|
||||||
|
pub const ExprList = std.ArrayList(Expr);
|
||||||
pub const TokenList = std.ArrayList(Token);
|
pub const TokenList = std.ArrayList(Token);
|
||||||
pub const ParamList = std.ArrayList(ParamDecl);
|
pub const ParamList = std.ArrayList(ParamDecl);
|
||||||
pub const ConstList = std.ArrayList(SingleConst);
|
pub const ConstList = std.ArrayList(SingleConst);
|
||||||
|
@ -145,8 +146,6 @@ pub const Expr = union(ExprType) {
|
||||||
Set: SetExpr,
|
Set: SetExpr,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Block = std.ArrayList(*Stmt);
|
|
||||||
|
|
||||||
pub const IfStmt = struct {
|
pub const IfStmt = struct {
|
||||||
condition: *Expr,
|
condition: *Expr,
|
||||||
then_branch: Block,
|
then_branch: Block,
|
||||||
|
|
|
@ -23,7 +23,7 @@ fn printBlock(ident: usize, block: var, endNewline: bool) void {
|
||||||
|
|
||||||
for (block.toSlice()) |stmt| {
|
for (block.toSlice()) |stmt| {
|
||||||
printIdent(ident);
|
printIdent(ident);
|
||||||
printStmt(ident, stmt);
|
printStmt(ident, &stmt);
|
||||||
std.debug.warn("\n");
|
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.*) {
|
switch (node.*) {
|
||||||
.FnDecl => |decl| {
|
.FnDecl => |decl| {
|
||||||
const name = decl.func_name.lexeme;
|
const name = decl.func_name.lexeme;
|
||||||
|
@ -97,7 +97,7 @@ pub fn printNode(node: *Node, ident: usize) void {
|
||||||
|
|
||||||
.Root => {
|
.Root => {
|
||||||
for (node.Root.toSlice()) |child| {
|
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);
|
std.debug.warn("({}", name);
|
||||||
|
|
||||||
for (exprs) |expr| {
|
for (exprs) |expr| {
|
||||||
std.debug.warn(" ");
|
std.debug.warn(" ");
|
||||||
printExpr(expr);
|
printExpr(&expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.warn(")");
|
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.*) {
|
switch (expr.*) {
|
||||||
.Binary => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
|
.Binary => |binary| printBinOp(binary),
|
||||||
.Logical => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
|
.Logical => |logical| printBinOp(logical),
|
||||||
.Unary => |unary| parenthetize(unary.op.lexeme, &[_]*Expr{unary.right}),
|
|
||||||
.Grouping => |expr_ptr| parenthetize("group", &[_]*Expr{expr_ptr}),
|
.Unary => |unary| printSingleOp(unary.op.lexeme, unary.right),
|
||||||
|
.Grouping => |expr_ptr| printSingleOp("group", expr_ptr),
|
||||||
|
|
||||||
.Literal => |literal| {
|
.Literal => |literal| {
|
||||||
switch (literal) {
|
switch (literal) {
|
||||||
|
@ -194,7 +214,7 @@ pub fn printExpr(expr: *Expr) void {
|
||||||
|
|
||||||
for (call.arguments.toSlice()) |arg| {
|
for (call.arguments.toSlice()) |arg| {
|
||||||
std.debug.warn(" ");
|
std.debug.warn(" ");
|
||||||
printExpr(arg);
|
printExpr(&arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.warn(")");
|
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.*) {
|
switch (stmt.*) {
|
||||||
.Println => |expr| parenthetize("println", &[_]*Expr{expr}),
|
.Println => |expr| printSingleOp("println", expr),
|
||||||
.Expr => |expr| printExpr(expr),
|
.Expr => |expr| printExpr(expr),
|
||||||
|
|
||||||
.If => |ifstmt| {
|
.If => |ifstmt| {
|
||||||
|
|
|
@ -385,7 +385,7 @@ pub const Parser = struct {
|
||||||
if (token.typ == .EOF) break;
|
if (token.typ == .EOF) break;
|
||||||
|
|
||||||
var node = try self.parseTopDecl();
|
var node = try self.parseTopDecl();
|
||||||
try root.Root.append(node);
|
try root.Root.append(node.*);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self.hadError) {
|
if (self.hadError) {
|
||||||
|
@ -625,7 +625,7 @@ pub const Parser = struct {
|
||||||
while (self.peek().typ != .RightBrace) {
|
while (self.peek().typ != .RightBrace) {
|
||||||
var stmt = try self.parseStmt();
|
var stmt = try self.parseStmt();
|
||||||
printer.printNode(try self.mkStmt(stmt), 0);
|
printer.printNode(try self.mkStmt(stmt), 0);
|
||||||
try stmts.append(stmt);
|
try stmts.append(stmt.*);
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = try self.consumeSingle(.RightBrace);
|
_ = try self.consumeSingle(.RightBrace);
|
||||||
|
@ -1012,13 +1012,13 @@ pub const Parser = struct {
|
||||||
|
|
||||||
// emulating do-while really badly
|
// emulating do-while really badly
|
||||||
var arg = try self.parseExpr();
|
var arg = try self.parseExpr();
|
||||||
try args.append(arg);
|
try args.append(arg.*);
|
||||||
|
|
||||||
while (self.check(.Comma)) {
|
while (self.check(.Comma)) {
|
||||||
_ = try self.consumeSingle(.Comma);
|
_ = try self.consumeSingle(.Comma);
|
||||||
|
|
||||||
arg = try self.parseExpr();
|
arg = try self.parseExpr();
|
||||||
try args.append(arg);
|
try args.append(arg.*);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1077,7 +1077,7 @@ pub const Parser = struct {
|
||||||
errdefer exprs.deinit();
|
errdefer exprs.deinit();
|
||||||
|
|
||||||
while (!self.check(.RightSquare)) {
|
while (!self.check(.RightSquare)) {
|
||||||
try exprs.append(try self.parseExpr());
|
try exprs.append((try self.parseExpr()).*);
|
||||||
if (self.check(.Comma)) _ = try self.consumeSingle(.Comma);
|
if (self.check(.Comma)) _ = try self.consumeSingle(.Comma);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue