diff --git a/examples/hello.ry b/examples/hello.ry index 961e4aa..62bfab3 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -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 { diff --git a/src/analysis.zig b/src/analysis.zig index 68792fe..7363078 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -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 diff --git a/src/ast.zig b/src/ast.zig index f8136b6..4d6b7b3 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -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) }; diff --git a/src/ast_printer.zig b/src/ast_printer.zig index 181a3c2..36bd72a 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -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| { diff --git a/src/codegen.zig b/src/codegen.zig index c728c0a..e28085b 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -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; diff --git a/src/entry.c b/src/entry.c index b2119d0..4ef5b4b 100644 --- a/src/entry.c +++ b/src/entry.c @@ -1,3 +1,3 @@ int main(void) { - __rayoko_main(); + return __rayoko_main(); } diff --git a/src/main.zig b/src/main.zig index a249060..17e004e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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"); diff --git a/src/parsers.zig b/src/parsers.zig index 7fa359b..75bedfc 100644 --- a/src/parsers.zig +++ b/src/parsers.zig @@ -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.*);