Compare commits

..

No commits in common. "4f0c43865f427558cd036923048dc43fe202cfa4" and "1ab966b85309e713b934c8c40f96ed0479794ecf" have entirely different histories.

6 changed files with 20 additions and 92 deletions

View file

@ -6,11 +6,6 @@ a vlang parser in zig
because i can because i can
## variations
- `for` is split between `for` and `loop` because my fucking god i cant stand
having *four* different variations of `for`.
## how ## how
``` ```

View file

@ -20,14 +20,4 @@ fn main(a int) int {
a && b a && b
a || b a || b
loop {}
loop {
println('Ballse')
}
loop a > 2 {}
loop a > 2 {
println('skirts')
}
} }

View file

@ -97,17 +97,12 @@ pub const Expr = union(ExprType) {
Grouping: *Expr, Grouping: *Expr,
}; };
pub const Block = std.ArrayList(*Stmt); pub const IfBranch = std.ArrayList(*Stmt);
pub const IfStmt = struct { pub const IfStmt = struct {
condition: *Expr, condition: *Expr,
then_branch: Block, then_branch: IfBranch,
else_branch: ?Block, else_branch: ?IfBranch,
};
pub const LoopStmt = struct {
condition: ?*Expr,
then_branch: Block,
}; };
pub const Stmt = union(enum) { pub const Stmt = union(enum) {
@ -115,46 +110,28 @@ pub const Stmt = union(enum) {
Println: *Expr, Println: *Expr,
If: IfStmt, If: IfStmt,
Loop: LoopStmt,
pub fn mkPrintln(allocator: *std.mem.Allocator, expr: *Expr) !*Stmt { pub fn mkPrintln(allocator: *std.mem.Allocator, expr: *Expr) !*Stmt {
var stmt = try allocator.create(Stmt); var println = try allocator.create(Stmt);
stmt.* = Stmt{ .Println = expr }; println.* = Stmt{ .Println = expr };
return stmt; return println;
} }
pub fn mkIfStmt( pub fn mkIfStmt(
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
condition: *Expr, condition: *Expr,
then: Block, then: IfBranch,
else_branch: ?Block, else_branch: ?IfBranch,
) !*Stmt { ) !*Stmt {
var stmt = try allocator.create(Stmt); var println = try allocator.create(Stmt);
stmt.* = Stmt{ println.* = Stmt{
.If = IfStmt{ .If = IfStmt{
.condition = condition, .condition = condition,
.then_branch = then, .then_branch = then,
.else_branch = else_branch, .else_branch = else_branch,
}, },
}; };
return println;
return stmt;
}
pub fn mkLoop(
allocator: *std.mem.Allocator,
condition: ?*Expr,
then: Block,
) !*Stmt {
var stmt = try allocator.create(Stmt);
stmt.* = Stmt{
.Loop = LoopStmt{
.condition = condition,
.then_branch = then,
},
};
return stmt;
} }
}; };
@ -327,20 +304,6 @@ pub fn printStmt(ident: usize, stmt: *Stmt) void {
std.debug.warn(")\n"); std.debug.warn(")\n");
}, },
.Loop => |loop| {
std.debug.warn("(loop ");
if (loop.condition) |cond| {
printExpr(cond);
} else {
std.debug.warn("true");
}
std.debug.warn(" ");
printBlock(ident + 1, loop.then_branch, false);
std.debug.warn(")\n");
},
else => std.debug.warn("UnknownStmt-{}", @tagName(stmt.*)), else => std.debug.warn("UnknownStmt-{}", @tagName(stmt.*)),
} }
} }

View file

@ -449,7 +449,6 @@ pub const Parser = struct {
fn parseStmt(self: *@This()) anyerror!*Stmt { fn parseStmt(self: *@This()) anyerror!*Stmt {
return switch (self.peek().ttype) { return switch (self.peek().ttype) {
.If => try self.parseIfStmt(), .If => try self.parseIfStmt(),
.Loop => try self.parseLoop(),
.Println => try self.parsePrintln(), .Println => try self.parsePrintln(),
// TODO make newlines tokens and consume newline? // TODO make newlines tokens and consume newline?
@ -457,35 +456,35 @@ pub const Parser = struct {
}; };
} }
/// Copy of parseBlock for blocks in statements /// Copy of parseBlock for if branches
fn parseStmtBlock(self: *@This()) !ast.Block { fn parseIfBranch(self: *@This()) !ast.IfBranch {
var block = ast.Block.init(self.allocator); var branch = ast.IfBranch.init(self.allocator);
errdefer block.deinit(); errdefer branch.deinit();
_ = try self.consumeSingle(.LeftBrace); _ = try self.consumeSingle(.LeftBrace);
while (self.peek().ttype != .RightBrace) { while (self.peek().ttype != .RightBrace) {
var stmt = try self.parseDecl(); var stmt = try self.parseDecl();
ast.printNode(try self.mkStmt(stmt), 0); ast.printNode(try self.mkStmt(stmt), 0);
try block.append(stmt); try branch.append(stmt);
} }
_ = try self.consumeSingle(.RightBrace); _ = try self.consumeSingle(.RightBrace);
return block; return branch;
} }
fn parseIfStmt(self: *@This()) !*Stmt { fn parseIfStmt(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.If); _ = try self.consumeSingle(.If);
var condition = (try self.parseExpr()).Expr; var condition = (try self.parseExpr()).Expr;
const then_branch = try self.parseStmtBlock(); const then_branch = try self.parseIfBranch();
var else_branch: ?ast.Block = null; var else_branch: ?ast.IfBranch = null;
if (self.check(.Else)) { if (self.check(.Else)) {
_ = try self.consumeSingle(.Else); _ = try self.consumeSingle(.Else);
else_branch = try self.parseStmtBlock(); else_branch = try self.parseIfBranch();
} }
return try Stmt.mkIfStmt( return try Stmt.mkIfStmt(
@ -496,22 +495,6 @@ pub const Parser = struct {
); );
} }
fn parseLoop(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Loop);
var expr: ?*Expr = null;
var body: ast.Block = undefined;
// infinite loop
if (self.check(.LeftBrace)) {
body = try self.parseStmtBlock();
} else {
expr = (try self.parseExpr()).Expr;
body = try self.parseStmtBlock();
}
return try Stmt.mkLoop(self.allocator, expr, body);
}
fn parsePrintln(self: *@This()) !*Stmt { fn parsePrintln(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Println); _ = try self.consumeSingle(.Println);

View file

@ -50,7 +50,6 @@ const keywords = [_][]const u8{
"false", "false",
"None", "None",
"println", "println",
"loop",
}; };
const keyword_ttypes = [_]TokenType{ const keyword_ttypes = [_]TokenType{
@ -79,7 +78,6 @@ const keyword_ttypes = [_]TokenType{
.False, .False,
.None, .None,
.Println, .Println,
.Loop,
}; };
fn getKeyword(keyword: []const u8) ?TokenType { fn getKeyword(keyword: []const u8) ?TokenType {

View file

@ -55,7 +55,6 @@ pub const TokenType = enum {
Enum, Enum,
Fn, Fn,
For, For,
Loop,
Go, Go,
Goto, Goto,
If, If,