Compare commits
No commits in common. "f9e8543b7c84910fa44f2f8447eeefe32030c7ab" and "1ac1e5426455dafeb3230b05673564b80c12e633" have entirely different histories.
f9e8543b7c
...
1ac1e54264
6 changed files with 6 additions and 84 deletions
|
@ -37,6 +37,7 @@ negatively charged towards
|
||||||
|
|
||||||
## wip
|
## wip
|
||||||
|
|
||||||
|
- no `for` yet
|
||||||
- no `map` yet
|
- no `map` yet
|
||||||
- no `in` yet (probably will be dropped)
|
- no `in` yet (probably will be dropped)
|
||||||
- no `module`, `import` yet
|
- no `module`, `import` yet
|
||||||
|
|
|
@ -17,8 +17,6 @@ fn main(a int) int {
|
||||||
mut a := 1+2
|
mut a := 1+2
|
||||||
a = 2
|
a = 2
|
||||||
|
|
||||||
a = 1 && 0
|
|
||||||
|
|
||||||
if a {
|
if a {
|
||||||
println(30)
|
println(30)
|
||||||
} else {
|
} else {
|
||||||
|
@ -65,9 +63,6 @@ fn main(a int) int {
|
||||||
str.len /= 1
|
str.len /= 1
|
||||||
|
|
||||||
awoo := [1, 2, a(), b + 2, c(31) * d]
|
awoo := [1, 2, a(), b + 2, c(31) * d]
|
||||||
|
|
||||||
for a in b {}
|
|
||||||
for idx, a in b {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (v Typ) voidfunc() {}
|
fn (v Typ) voidfunc() {}
|
||||||
|
|
22
src/ast.zig
22
src/ast.zig
|
@ -158,20 +158,12 @@ pub const LoopStmt = struct {
|
||||||
then_branch: Block,
|
then_branch: Block,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ForStmt = struct {
|
|
||||||
index: ?Token,
|
|
||||||
value: Token,
|
|
||||||
array: Token,
|
|
||||||
block: Block,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const Stmt = union(enum) {
|
pub const Stmt = union(enum) {
|
||||||
Expr: *Expr,
|
Expr: *Expr,
|
||||||
Println: *Expr,
|
Println: *Expr,
|
||||||
|
|
||||||
If: IfStmt,
|
If: IfStmt,
|
||||||
Loop: LoopStmt,
|
Loop: LoopStmt,
|
||||||
For: ForStmt,
|
|
||||||
|
|
||||||
Return: ReturnStmt,
|
Return: ReturnStmt,
|
||||||
|
|
||||||
|
@ -220,20 +212,6 @@ pub const Stmt = union(enum) {
|
||||||
return stmt;
|
return stmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mkFor(allocator: *std.mem.Allocator, index: ?Token, value: Token, array: Token, block: Block) !*Stmt {
|
|
||||||
var stmt = try allocator.create(Stmt);
|
|
||||||
stmt.* = Stmt{
|
|
||||||
.For = ForStmt{
|
|
||||||
.index = index,
|
|
||||||
.value = value,
|
|
||||||
.array = array,
|
|
||||||
.block = block,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return stmt;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mkReturn(allocator: *std.mem.Allocator, tok: Token, value: *Expr) !*Stmt {
|
pub fn mkReturn(allocator: *std.mem.Allocator, tok: Token, value: *Expr) !*Stmt {
|
||||||
var stmt = try allocator.create(Stmt);
|
var stmt = try allocator.create(Stmt);
|
||||||
stmt.* = Stmt{
|
stmt.* = Stmt{
|
||||||
|
|
|
@ -262,21 +262,6 @@ pub fn printStmt(ident: usize, stmt: *Stmt) void {
|
||||||
std.debug.warn(")\n");
|
std.debug.warn(")\n");
|
||||||
},
|
},
|
||||||
|
|
||||||
.For => |forstmt| {
|
|
||||||
std.debug.warn("(for ");
|
|
||||||
|
|
||||||
if (forstmt.index) |index| {
|
|
||||||
std.debug.warn("({} {}) ", index.lexeme, forstmt.value.lexeme);
|
|
||||||
} else {
|
|
||||||
std.debug.warn("{} ", forstmt.value.lexeme);
|
|
||||||
}
|
|
||||||
|
|
||||||
std.debug.warn("{} ", forstmt.array.lexeme);
|
|
||||||
|
|
||||||
printBlock(ident + 1, forstmt.block, false);
|
|
||||||
std.debug.warn(")\n");
|
|
||||||
},
|
|
||||||
|
|
||||||
.Return => |ret| {
|
.Return => |ret| {
|
||||||
std.debug.warn("(return ");
|
std.debug.warn("(return ");
|
||||||
printExpr(ret.value);
|
printExpr(ret.value);
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub const Result = error{
|
||||||
pub const StdOut = *std.io.OutStream(std.fs.File.WriteError);
|
pub const StdOut = *std.io.OutStream(std.fs.File.WriteError);
|
||||||
|
|
||||||
fn run(allocator: *Allocator, data: []u8) !void {
|
fn run(allocator: *Allocator, data: []u8) !void {
|
||||||
var stdout_file = std.io.getStdOut();
|
var stdout_file = try std.io.getStdOut();
|
||||||
const stdout = &stdout_file.outStream().stream;
|
const stdout = &stdout_file.outStream().stream;
|
||||||
|
|
||||||
var runner = runners.Runner.init(allocator, stdout);
|
var runner = runners.Runner.init(allocator, stdout);
|
||||||
|
@ -39,7 +39,7 @@ fn runFile(allocator: *Allocator, path: []const u8) !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runPrompt(allocator: *Allocator) !void {
|
fn runPrompt(allocator: *Allocator) !void {
|
||||||
var stdout_file = std.io.getStdOut();
|
var stdout_file = try std.io.getStdOut();
|
||||||
const stdout = &stdout_file.outStream().stream;
|
const stdout = &stdout_file.outStream().stream;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -71,7 +71,7 @@ pub fn main() anyerror!void {
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
var allocator = &arena.allocator;
|
var allocator = &arena.allocator;
|
||||||
|
|
||||||
var stdout_file = std.io.getStdOut();
|
var stdout_file = try std.io.getStdOut();
|
||||||
var stdout = &stdout_file.outStream().stream;
|
var stdout = &stdout_file.outStream().stream;
|
||||||
|
|
||||||
var args_it = std.process.args();
|
var args_it = std.process.args();
|
||||||
|
|
|
@ -636,7 +636,6 @@ pub const Parser = struct {
|
||||||
return switch (self.peek().ttype) {
|
return switch (self.peek().ttype) {
|
||||||
.If => try self.parseIfStmt(),
|
.If => try self.parseIfStmt(),
|
||||||
.Loop => try self.parseLoop(),
|
.Loop => try self.parseLoop(),
|
||||||
.For => try self.parseForStmt(),
|
|
||||||
.Println => try self.parsePrintln(),
|
.Println => try self.parsePrintln(),
|
||||||
.Return => try self.parseReturn(),
|
.Return => try self.parseReturn(),
|
||||||
else => try self.parseStmtExpr(),
|
else => try self.parseStmtExpr(),
|
||||||
|
@ -676,42 +675,6 @@ pub const Parser = struct {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseForStmt(self: *@This()) !*Stmt {
|
|
||||||
// There are two types of for in vig's V subset:
|
|
||||||
// - for x in y
|
|
||||||
// - for idx, x in y
|
|
||||||
_ = try self.consumeSingle(.For);
|
|
||||||
|
|
||||||
var index_var: ?Token = null;
|
|
||||||
var value_var: Token = undefined;
|
|
||||||
|
|
||||||
const subject_1 = try self.consumeSingle(.Identifier);
|
|
||||||
|
|
||||||
if (self.check(.Comma)) {
|
|
||||||
_ = try self.consumeSingle(.Comma);
|
|
||||||
|
|
||||||
const subject_2 = try self.consumeSingle(.Identifier);
|
|
||||||
index_var = subject_1;
|
|
||||||
value_var = subject_2;
|
|
||||||
} else {
|
|
||||||
value_var = subject_1;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = try self.consumeSingle(.In);
|
|
||||||
|
|
||||||
// MUST be identifier
|
|
||||||
var array = try self.consumeSingle(.Identifier);
|
|
||||||
var block = try self.parseStmtBlock();
|
|
||||||
|
|
||||||
return try Stmt.mkFor(
|
|
||||||
self.allocator,
|
|
||||||
index_var,
|
|
||||||
value_var,
|
|
||||||
array,
|
|
||||||
block,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parseLoop(self: *@This()) !*Stmt {
|
fn parseLoop(self: *@This()) !*Stmt {
|
||||||
_ = try self.consumeSingle(.Loop);
|
_ = try self.consumeSingle(.Loop);
|
||||||
var expr: ?*Expr = null;
|
var expr: ?*Expr = null;
|
||||||
|
@ -1029,8 +992,8 @@ pub const Parser = struct {
|
||||||
fn finishStructVal(self: *@This(), expr: *Expr) !*Expr {
|
fn finishStructVal(self: *@This(), expr: *Expr) !*Expr {
|
||||||
// <expr>{a: 10 b: 10}
|
// <expr>{a: 10 b: 10}
|
||||||
// for this to work properly, <expr> must be Variable, since its a type.
|
// for this to work properly, <expr> must be Variable, since its a type.
|
||||||
if (@as(ast.ExprType, expr.*) != .Variable) {
|
if (ast.ExprType(expr.*) != .Variable) {
|
||||||
self.doError("Expected variable for struct type, got {}", @as(ast.ExprType, expr.*));
|
self.doError("Expected variable for struct type, got {}", ast.ExprType(expr.*));
|
||||||
return Result.CompileError;
|
return Result.CompileError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue