summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/mathTest.nya3
-rw-r--r--examples/testif.nya9
-rw-r--r--src/codegen.zig11
-rw-r--r--src/main.zig3
-rw-r--r--src/parser.zig5
-rw-r--r--src/symtable.zig2
-rw-r--r--src/tokenize.zig39
7 files changed, 57 insertions, 15 deletions
diff --git a/examples/mathTest.nya b/examples/mathTest.nya
index 14e7f0a..7b36cc8 100644
--- a/examples/mathTest.nya
+++ b/examples/mathTest.nya
@@ -8,8 +8,9 @@ fn test() -> i32 {
fn main() -> i32 {
const: i32 num = 5 + 3 * 7;
const: i32 num2 = test();
- if (num2 == test())
+ if (num2 == test()) {
puts("Hello World!");
+ }
if (num2 == 0)
puts("Shouldn't print");
diff --git a/examples/testif.nya b/examples/testif.nya
new file mode 100644
index 0000000..c18f34e
--- /dev/null
+++ b/examples/testif.nya
@@ -0,0 +1,9 @@
+fn main() -> i32 {
+ if (6 == 7) {
+ return 1;
+ }
+ if (5 == 5) {
+ return 0;
+ }
+ return 2;
+}
diff --git a/src/codegen.zig b/src/codegen.zig
index 9fb09cb..49a07fe 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -104,7 +104,7 @@ pub const Generator = struct {
const ptr = try self.genAlloc(toLLVMtype(nodeVar.expr.typ orelse try nodeVar.expr.inferType(self.allocator, table), table, nodeVar.expr), nodeVar.ident.ident);
const value = try self.genExpr(nodeVar.expr);
_ = core.LLVMBuildStore(self.builder, value, ptr);
- std.debug.print("\t\t\tGenerated Value {s}\n", .{nodeVar.ident.ident});
+ // std.debug.print("\t\t\tGenerated Value {s}\n", .{nodeVar.ident.ident});
try self.references.put(symbol.id, ptr);
}
@@ -215,7 +215,7 @@ pub const Generator = struct {
}
fn genStmt(self: *Generator, stmt: parse.NodeStmt) !void {
- std.debug.print("======\n\tStmt: {any}\n======\n", .{stmt.kind});
+ // std.debug.print("======\n\tStmt: {any}\n======\n", .{stmt.kind});
try switch (stmt.kind) {
.exit => |expr| self.genExit(expr),
.function => self.genFunc(stmt),
@@ -223,11 +223,10 @@ pub const Generator = struct {
.defVar => self.genVar(stmt),
.assignVar => self.genAssign(stmt),
.ifstmt => self.genIf(stmt),
+ .block => |blk| self.genBlock(blk),
.expr => |expression| {
_ = try self.genExpr(expression);
},
-
- else => {},
};
}
@@ -259,11 +258,11 @@ pub const Generator = struct {
"",
);
},
- .intLit => |int| core.LLVMConstInt(core.LLVMInt32TypeInContext(self.context), @intCast(int.intLit), 1),
+ .intLit => |int| core.LLVMConstInt(core.LLVMInt32Type(), @intCast(int.intLit), 1),
.binaryOp => |exp| blk: {
const lhs = try self.genExpr(exp.lhs.*);
const rhs = try self.genExpr(exp.rhs.*);
- std.debug.print("\n\n\tLHS: {any}\n\tRHS: {any}\n\tOP: {any}\n\n", .{ lhs, rhs, exp.op });
+ // std.debug.print("\n\n\tLHS: {any}\n\tRHS: {any}\n\tOP: {any}\n\n", .{ exp.lhs.*.kind, exp.rhs.*.kind, exp.op });
break :blk switch (exp.op) {
.plus => core.LLVMBuildAdd(self.builder, lhs, rhs, "add"),
diff --git a/src/main.zig b/src/main.zig
index b51b170..be9a2d8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -13,7 +13,7 @@ pub fn main() !void {
var allocator = arena.allocator();
if (std.os.argv.len < 2) {
std.debug.print(
- \\info: Usage: calico [input file]
+ \\info: Usage: calico [input file] [output name (defaults to "out")]
\\
, .{});
return;
@@ -47,6 +47,7 @@ pub fn main() !void {
var tokenizer = tok.Tokenizer.init(allocator, all);
defer tokenizer.deinit();
const tokens = try tokenizer.tokenize();
+ // for (tokens) |tokn| std.debug.print("{s} ", .{tokn.toString()});
// Parse
// var arena = std.heap.ArenaAllocator.init(allocator);
diff --git a/src/parser.zig b/src/parser.zig
index 66d02a2..b1581bb 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -234,8 +234,7 @@ pub const Parser = struct {
};
break :blk ExprKind{ .stringLit = (try self.tokens.consume(.stringLit)).? };
},
- else => |expr| break :blk errorblk: {
- std.debug.print("Invalid Expression: {any}\n", .{expr});
+ else => break :blk errorblk: {
break :errorblk ParsingError.InvalidExpression;
},
};
@@ -656,7 +655,7 @@ test "Parser" {
},
.symtable = symbTable,
.typ = TypeIdent{ .list = false, .ident = "i32" },
- .isConst = true,
+ .isConst = false,
},
},
},
diff --git a/src/symtable.zig b/src/symtable.zig
index 3017670..fdbc536 100644
--- a/src/symtable.zig
+++ b/src/symtable.zig
@@ -204,7 +204,7 @@ pub const Populator = struct {
try value.expr.inferType(self.allocator, table),
false,
);
- std.debug.print("Value: {s}\nSymbol: {any}\n", .{ value.ident.ident, symbol });
+ // std.debug.print("Value: {s}\nSymbol: {any}\n", .{ value.ident.ident, symbol });
if (!try table.insert(value.ident.ident, symbol)) return error.FailedToInsert;
},
.block => {
diff --git a/src/tokenize.zig b/src/tokenize.zig
index 2c536db..038a4d3 100644
--- a/src/tokenize.zig
+++ b/src/tokenize.zig
@@ -94,9 +94,9 @@ pub const Token = union(TokenType) {
':' => .colon,
'[' => .openBracket,
']' => .closeBracket,
- else => {
- // std.debug.print("{c}: ", .{char});
- return TokenizeError.UnknownToken;
+ else => errblk: {
+ std.debug.print("{c}: ", .{char});
+ break :errblk TokenizeError.UnknownToken;
},
};
}
@@ -111,6 +111,39 @@ pub const Token = union(TokenType) {
if (eql(u8, str, "import")) return .import;
return Token{ .ident = str };
}
+
+ pub fn toString(self: Token) []const u8 {
+ return switch (self) {
+ .ident => "IDENT",
+ .stringLit => "STRINGlIT",
+ .intLit => "INTlIT",
+ .charLit => "CHARlIT",
+ .ifstmt => "IFSTMT",
+ .constant => "CONSTANT",
+ .variable => "VARIABLE",
+ .exit => "EXIT",
+ .fun => "FUN",
+ .import => "IMPORT",
+ .plus => "PLUS",
+ .minus => "MINUS",
+ .star => "STAR",
+ .slash => "SLASH",
+ .semiCol => "SEMIcOL",
+ .equal => "EQUAL",
+ .eqleql => "EQLEQL",
+ .lessthan => "LESSTHAN",
+ .greaterthan => "GREATERTHAN",
+ .openBrace => "OPENbRACE",
+ .closeBrace => "CLOSEbRACE",
+ .openParen => "OPENpAREN",
+ .closeParen => "CLOSEpAREN",
+ .openBracket => "OPENbRACKET",
+ .closeBracket => "CLOSEbRACKET",
+ .arrow => "ARROW",
+ .colon => "COLON",
+ .comma => "COMMA",
+ };
+ }
};
pub fn checkType(tok: Token, comptime typ: TokenType) bool {