Compare commits

...

6 Commits

Author SHA1 Message Date
Luna 5348e2b5c4 remove Stmt as an AST node
it was only used for printing purposes, even though we can use the
existing printStmt() function.
2019-10-05 10:37:28 -03:00
Luna 9d1c9cab7f analysis: fix ret type 2019-10-05 10:33:37 -03:00
Luna d69a64dbfb analysis: add bump-dump scope to loop analysis 2019-10-05 10:30:01 -03:00
Luna 881833b187 s/TypeSolver/Analyzer 2019-10-05 10:27:38 -03:00
Luna 5c58ac0238 analysis: add contextual checks for Assign expr 2019-10-05 10:26:47 -03:00
Luna 809dad1095 codegen: add Grouping expr support
- entry: return value of rayoko main
2019-10-05 10:08:27 -03:00
8 changed files with 32 additions and 28 deletions

View File

@ -26,7 +26,7 @@ fn multwo(num: i32, double_flag: bool) i32 {
}
fn add(a: i32, b: i32) i32 {
return a + b;
return a + b + (2 * b);
}
fn main() i32 {

View File

@ -8,7 +8,7 @@ const Token = @import("tokens.zig").Token;
const SymbolUnderlyingType = comp.SymbolUnderlyingType;
pub const TypeSolver = struct {
pub const Analyzer = struct {
allocator: *std.mem.Allocator,
// error handling
@ -18,8 +18,8 @@ pub const TypeSolver = struct {
err_ctx_buffer: []u8,
pub fn init(allocator: *std.mem.Allocator) !TypeSolver {
return TypeSolver{
pub fn init(allocator: *std.mem.Allocator) !Analyzer {
return Analyzer{
.allocator = allocator,
.err_ctx_buffer = try allocator.alloc(u8, 512),
};
@ -329,8 +329,8 @@ pub const TypeSolver = struct {
},
else => {
std.debug.warn(
"Expected Struct/Enum as get target, got {}\n",
self.doError(
"Expected Struct/Enum as get target, got {}",
comp.SymbolUnderlyingTypeEnum(global_typ),
);
@ -340,13 +340,23 @@ pub const TypeSolver = struct {
},
.Assign => |assign| {
if (ctx.current_scope == null) {
self.doError("Can't assign without a scope");
return CompileError.Invalid;
}
var var_type = ctx.current_scope.?.env.get(
assign.name.lexeme,
).?.value;
);
if (var_type == null) {
self.doError("Assign target variable not found");
return CompileError.Invalid;
}
var value_type = try self.resolveExprType(ctx, assign.value);
try self.expectSymUnTypeEqual(var_type, value_type);
return var_type;
try self.expectSymUnTypeEqual(var_type.?.value, value_type);
return var_type.?.value;
},
.Set => @panic("TODO analysis of Set exprs"),
@ -422,10 +432,13 @@ pub const TypeSolver = struct {
try self.expectSymUnTypeEnum(expr, .Bool);
}
// TODO bump-dump scope
try ctx.bumpScope("loop");
for (loop.then_branch.toSlice()) |then_stmt| {
try self.stmtPass(ctx, then_stmt);
}
ctx.dumpScope();
},
// For (creates 1 scope) receives arrays, which we dont have yet

View File

@ -18,7 +18,6 @@ pub const NodeType = enum {
Struct,
Enum,
Block,
Stmt,
};
pub const ParamDecl = struct {
@ -323,8 +322,6 @@ pub const Node = union(NodeType) {
Block: StmtList,
Stmt: *Stmt,
pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
var node = try allocator.create(Node);
node.* = Node{ .Root = NodeList.init(allocator) };

View File

@ -99,12 +99,6 @@ pub fn printNode(node: *const Node, ident: usize) void {
}
},
.Stmt => |stmt| {
printIdent(ident);
printStmt(ident, stmt);
std.debug.warn("\n");
},
.Struct => |struc| {
print(ident, "(struct {} (\n", struc.name.lexeme);
for (struc.fields.toSlice()) |field| {

View File

@ -13,6 +13,7 @@ pub const CompileError = error{
LLVMError,
EmitError,
TypeError,
Invalid,
};
fn mkLLVMBool(val: bool) llvm.LLVMValueRef {
@ -258,6 +259,10 @@ pub const Codegen = struct {
};
},
.Grouping => |expr_ptr| blk: {
break :blk try self.emitExpr(builder, expr_ptr);
},
else => {
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
return CompileError.EmitError;

View File

@ -1,3 +1,3 @@
int main(void) {
__rayoko_main();
return __rayoko_main();
}

View File

@ -50,7 +50,7 @@ pub fn run(allocator: *std.mem.Allocator, slice: []const u8) !Result {
std.debug.warn("parse tree\n");
printer.printNode(root, 0);
var solver = try analysis.TypeSolver.init(allocator);
var solver = try analysis.Analyzer.init(allocator);
var ctx = try solver.pass(root);
std.debug.warn("symbol table\n");

View File

@ -236,12 +236,6 @@ pub const Parser = struct {
return node;
}
fn mkStmt(self: *Parser, stmt: *Stmt) !*ast.Node {
var node = try self.allocator.create(Node);
node.* = Node{ .Stmt = stmt };
return node;
}
fn mkStmtExpr(self: *Parser, expr: *Expr) !*Stmt {
var stmt = try self.allocator.create(Stmt);
stmt.* = Stmt{ .Expr = expr };
@ -634,7 +628,8 @@ pub const Parser = struct {
while (self.peek().typ != .RightBrace) {
var stmt = try self.parseStmt();
printer.printNode(try self.mkStmt(stmt), 0);
printer.printStmt(0, stmt);
if (self.check(.Semicolon))
_ = try self.consumeSingle(.Semicolon);
try stmts.append(stmt.*);