Compare commits

..

No commits in common. "5348e2b5c478804931a66c43cf5662a40feade30" and "34481c8ea8963dee3e3615ecff1eab8fb6856796" have entirely different histories.

8 changed files with 28 additions and 32 deletions

View file

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

View file

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

View file

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

View file

@ -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| { .Struct => |struc| {
print(ident, "(struct {} (\n", struc.name.lexeme); print(ident, "(struct {} (\n", struc.name.lexeme);
for (struc.fields.toSlice()) |field| { for (struc.fields.toSlice()) |field| {

View file

@ -13,7 +13,6 @@ pub const CompileError = error{
LLVMError, LLVMError,
EmitError, EmitError,
TypeError, TypeError,
Invalid,
}; };
fn mkLLVMBool(val: bool) llvm.LLVMValueRef { 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 => { else => {
std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*)); std.debug.warn("Got unexpected expr {}\n", ast.ExprType(expr.*));
return CompileError.EmitError; return CompileError.EmitError;

View file

@ -1,3 +1,3 @@
int main(void) { int main(void) {
return __rayoko_main(); __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"); std.debug.warn("parse tree\n");
printer.printNode(root, 0); printer.printNode(root, 0);
var solver = try analysis.Analyzer.init(allocator); var solver = try analysis.TypeSolver.init(allocator);
var ctx = try solver.pass(root); var ctx = try solver.pass(root);
std.debug.warn("symbol table\n"); std.debug.warn("symbol table\n");

View file

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