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 {
return a + b + (2 * b);
return a + b;
}
fn main() i32 {

View file

@ -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

View file

@ -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) };

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

View file

@ -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;

View file

@ -1,3 +1,3 @@
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");
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");

View file

@ -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.*);