diff --git a/examples/hello.ry b/examples/hello.ry index 62bfab3..961e4aa 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 + (2 * b); + return a + b; } fn main() i32 { diff --git a/src/analysis.zig b/src/analysis.zig index 7363078..68792fe 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 Analyzer = struct { +pub const TypeSolver = struct { allocator: *std.mem.Allocator, // error handling @@ -18,8 +18,8 @@ pub const Analyzer = struct { err_ctx_buffer: []u8, - pub fn init(allocator: *std.mem.Allocator) !Analyzer { - return Analyzer{ + pub fn init(allocator: *std.mem.Allocator) !TypeSolver { + return TypeSolver{ .allocator = allocator, .err_ctx_buffer = try allocator.alloc(u8, 512), }; @@ -329,8 +329,8 @@ pub const Analyzer = struct { }, else => { - self.doError( - "Expected Struct/Enum as get target, got {}", + std.debug.warn( + "Expected Struct/Enum as get target, got {}\n", comp.SymbolUnderlyingTypeEnum(global_typ), ); @@ -340,23 +340,13 @@ pub const Analyzer = 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, - ); - - if (var_type == null) { - self.doError("Assign target variable not found"); - return CompileError.Invalid; - } + ).?.value; var value_type = try self.resolveExprType(ctx, assign.value); - try self.expectSymUnTypeEqual(var_type.?.value, value_type); - return var_type.?.value; + try self.expectSymUnTypeEqual(var_type, value_type); + return var_type; }, .Set => @panic("TODO analysis of Set exprs"), @@ -432,13 +422,10 @@ pub const Analyzer = struct { try self.expectSymUnTypeEnum(expr, .Bool); } - try ctx.bumpScope("loop"); - + // TODO bump-dump scope 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 4d6b7b3..f8136b6 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -18,6 +18,7 @@ pub const NodeType = enum { Struct, Enum, Block, + Stmt, }; pub const ParamDecl = struct { @@ -322,6 +323,8 @@ 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 36bd72a..181a3c2 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -99,6 +99,12 @@ 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 e28085b..c728c0a 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -13,7 +13,6 @@ pub const CompileError = error{ LLVMError, EmitError, TypeError, - Invalid, }; fn mkLLVMBool(val: bool) llvm.LLVMValueRef { @@ -259,10 +258,6 @@ 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 4ef5b4b..b2119d0 100644 --- a/src/entry.c +++ b/src/entry.c @@ -1,3 +1,3 @@ int main(void) { - return __rayoko_main(); + __rayoko_main(); } diff --git a/src/main.zig b/src/main.zig index 17e004e..a249060 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.Analyzer.init(allocator); + var solver = try analysis.TypeSolver.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 75bedfc..7fa359b 100644 --- a/src/parsers.zig +++ b/src/parsers.zig @@ -236,6 +236,12 @@ 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 }; @@ -628,8 +634,7 @@ pub const Parser = struct { while (self.peek().typ != .RightBrace) { var stmt = try self.parseStmt(); - printer.printStmt(0, stmt); - + printer.printNode(try self.mkStmt(stmt), 0); if (self.check(.Semicolon)) _ = try self.consumeSingle(.Semicolon); try stmts.append(stmt.*);