Compare commits
No commits in common. "6a52297ccef17f894fa3efc414b11faa8a99638d" and "c2499f96c90786c97ca324509ba659f61d8b08dd" have entirely different histories.
6a52297cce
...
c2499f96c9
16
README.md
16
README.md
|
@ -1,16 +1,10 @@
|
|||
# vig
|
||||
# spoodle
|
||||
|
||||
a [v] parser in zig
|
||||
|
||||
[v]: https://vlang.io
|
||||
|
||||
(will likely be done as a full compiler, who knows, leave a like and subscribe
|
||||
for more epic adventures)
|
||||
a vlang parser in zig
|
||||
|
||||
## why
|
||||
|
||||
because i want to learn parsers and what best to do it with a language i'm
|
||||
negatively charged towards
|
||||
because i can
|
||||
|
||||
## variations
|
||||
|
||||
|
@ -23,7 +17,7 @@ negatively charged towards
|
|||
## how
|
||||
|
||||
```
|
||||
git clone https://gitdab.com/luna/vig.git
|
||||
cd vig
|
||||
git clone https://gitdab.com/luna/spoodle.git
|
||||
cd spoodle
|
||||
zig build install --prefix ~/.local/
|
||||
```
|
||||
|
|
|
@ -2,7 +2,7 @@ const Builder = @import("std").build.Builder;
|
|||
|
||||
pub fn build(b: *Builder) void {
|
||||
const mode = b.standardReleaseOptions();
|
||||
const exe = b.addExecutable("vig", "src/main.zig");
|
||||
const exe = b.addExecutable("spoodle", "src/main.zig");
|
||||
exe.setBuildMode(mode);
|
||||
|
||||
const run_cmd = exe.run();
|
||||
|
|
|
@ -5,11 +5,6 @@ const (
|
|||
Businesses = 4
|
||||
)
|
||||
|
||||
struct Point {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
fn main(a int) int {
|
||||
1 + 2 + 3 + 4
|
||||
1 + 1 * 1
|
||||
|
@ -46,7 +41,10 @@ fn main(a int) int {
|
|||
y: 20
|
||||
}
|
||||
|
||||
p.x = 69
|
||||
|
||||
println(egg.scramble(3).with(cheddar))
|
||||
//println(p.x)
|
||||
}
|
||||
|
||||
struct Point {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
|
16
src/ast.zig
16
src/ast.zig
|
@ -80,8 +80,6 @@ pub const ExprType = enum {
|
|||
Struct,
|
||||
|
||||
Grouping,
|
||||
Get,
|
||||
Set,
|
||||
};
|
||||
|
||||
pub const VarDecl = struct {
|
||||
|
@ -107,17 +105,6 @@ pub const StructExpr = struct {
|
|||
inits: StructInitList,
|
||||
};
|
||||
|
||||
pub const GetExpr = struct {
|
||||
struc: *Expr,
|
||||
name: Token,
|
||||
};
|
||||
|
||||
pub const SetExpr = struct {
|
||||
struc: *Expr,
|
||||
field: Token,
|
||||
value: *Expr,
|
||||
};
|
||||
|
||||
pub const Expr = union(ExprType) {
|
||||
Assign: AssignExpr,
|
||||
VarDecl: VarDecl,
|
||||
|
@ -131,9 +118,6 @@ pub const Expr = union(ExprType) {
|
|||
Variable: Token,
|
||||
Grouping: *Expr,
|
||||
Call: CallExpr,
|
||||
|
||||
Get: GetExpr,
|
||||
Set: SetExpr,
|
||||
};
|
||||
|
||||
pub const Block = std.ArrayList(*Stmt);
|
||||
|
|
|
@ -4,8 +4,6 @@ const Token = tokens.Token;
|
|||
|
||||
usingnamespace @import("ast.zig");
|
||||
|
||||
const warn = std.debug.warn;
|
||||
|
||||
fn printIdent(ident: usize) void {
|
||||
var i: usize = 0;
|
||||
while (i < ident) : (i += 1) {
|
||||
|
@ -179,20 +177,6 @@ pub fn printExpr(expr: *Expr) void {
|
|||
std.debug.warn("))");
|
||||
},
|
||||
|
||||
.Get => |get| {
|
||||
warn("(");
|
||||
printExpr(get.struc);
|
||||
warn(".{})", get.name.lexeme);
|
||||
},
|
||||
|
||||
.Set => |set| {
|
||||
warn("(set ");
|
||||
printExpr(set.struc);
|
||||
warn(" {} ", set.field.lexeme);
|
||||
printExpr(set.value);
|
||||
warn(")");
|
||||
},
|
||||
|
||||
else => std.debug.warn("UnknownExpr-{}", @tagName(expr.*)),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,11 +89,7 @@ pub const Parser = struct {
|
|||
}
|
||||
|
||||
fn consume(self: *Parser, ttype: TokenType, comptime msg: []const u8) !Token {
|
||||
if (self.check(ttype)) {
|
||||
var tok = self.peek();
|
||||
_ = try self.nextToken();
|
||||
return tok;
|
||||
}
|
||||
if (self.check(ttype)) return try self.nextToken();
|
||||
|
||||
try self.tokenError(self.peek(), msg);
|
||||
return Result.CompileError;
|
||||
|
@ -275,31 +271,6 @@ pub const Parser = struct {
|
|||
return expr;
|
||||
}
|
||||
|
||||
fn mkGet(self: *@This(), struc: *Expr, name: Token) !*Expr {
|
||||
var expr = try self.allocator.create(Expr);
|
||||
expr.* = Expr{
|
||||
.Get = ast.GetExpr{
|
||||
.struc = struc,
|
||||
.name = name,
|
||||
},
|
||||
};
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
fn mkSet(self: *@This(), struc: *Expr, field: Token, value: *Expr) !*Expr {
|
||||
var expr = try self.allocator.create(Expr);
|
||||
expr.* = Expr{
|
||||
.Set = ast.SetExpr{
|
||||
.struc = struc,
|
||||
.field = field,
|
||||
.value = value,
|
||||
},
|
||||
};
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
fn mkBool(self: *Parser, val: bool) !*ast.Expr {
|
||||
var expr = try self.allocator.create(Expr);
|
||||
expr.* = Expr{
|
||||
|
@ -611,28 +582,15 @@ pub const Parser = struct {
|
|||
_ = try self.nextToken();
|
||||
value = try self.parseAssignment();
|
||||
|
||||
switch (expr.*) {
|
||||
.Variable => {
|
||||
switch (op.ttype) {
|
||||
.ColonEqual => return try self.mkVarDecl(expr.Variable, value, mutable),
|
||||
.Equal => return try self.mkAssign(expr.Variable, value),
|
||||
else => unreachable,
|
||||
}
|
||||
},
|
||||
if (ast.ExprType(expr.*) != .Variable) {
|
||||
self.doError("Invalid assignment target");
|
||||
return Result.CompileError;
|
||||
}
|
||||
|
||||
.Get => |get| {
|
||||
if (op.ttype == .ColonEqual) {
|
||||
self.doError("can not initialize struct field");
|
||||
return Result.CompileError;
|
||||
}
|
||||
|
||||
return try self.mkSet(get.struc, get.name, value);
|
||||
},
|
||||
|
||||
else => |expr_typ| {
|
||||
self.doError("Invalid assignment target {}", expr_typ);
|
||||
return Result.CompileError;
|
||||
},
|
||||
switch (op.ttype) {
|
||||
.ColonEqual => return try self.mkVarDecl(expr.Variable, value, mutable),
|
||||
.Equal => return try self.mkAssign(expr.Variable, value),
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -757,18 +715,17 @@ pub const Parser = struct {
|
|||
printer.printExpr(expr);
|
||||
|
||||
if (self.check(.LeftParen)) {
|
||||
if (ast.ExprType(expr.*) != .Variable) {
|
||||
self.doError("cannot call non-variable {}", ast.ExprType(expr.*));
|
||||
return Result.CompileError;
|
||||
}
|
||||
|
||||
_ = try self.consumeSingle(.LeftParen);
|
||||
expr = try self.finishCall(expr);
|
||||
} else if (self.check(.Dot)) {
|
||||
_ = try self.consumeSingle(.Dot);
|
||||
|
||||
if (self.check(.LeftBrace)) {
|
||||
_ = try self.consumeSingle(.LeftBrace);
|
||||
expr = try self.finishStructVal(expr);
|
||||
} else {
|
||||
var name = try self.consume(.Identifier, "Expect property name after '.'");
|
||||
expr = try self.mkGet(expr, name);
|
||||
}
|
||||
_ = try self.consumeSingle(.LeftBrace);
|
||||
expr = try self.finishStructVal(expr);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -795,7 +752,6 @@ pub const Parser = struct {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO shouldnt consume() return the current token, not nextToken?
|
||||
var paren = try self.consume(.RightParen, "Expected ')' after arguments");
|
||||
|
||||
return self.mkCall(callee, paren, args);
|
||||
|
|
Loading…
Reference in New Issue