diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-08-16 00:21:00 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2024-08-16 00:21:00 -0500 |
| commit | f1a2e03047c31ca57ca2d79f94f0ae179f0110e2 (patch) | |
| tree | 1a2ac762632373550d621f9ed85ff5e0b925b00f /src/parser.zig | |
| parent | 760a9246618862b56bafe3dd5d95a77fdd668a6c (diff) | |
| download | calico-f1a2e03047c31ca57ca2d79f94f0ae179f0110e2.tar.gz | |
Added type checking to assignments. Types required for varbl now.
Diffstat (limited to 'src/parser.zig')
| -rw-r--r-- | src/parser.zig | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/parser.zig b/src/parser.zig index d4bbfc0..f2dba0a 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -17,6 +17,13 @@ const ParsingError = error{ ExpectedToken, OutOfMemory, TokenIteratorOnly, + TypeRequiredForVarbl, +}; + +const TypeError = error{ + OutOfMemory, + IncorrectType, + UnknownIdentifier, }; fn errcast(err: anytype) ParsingError { @@ -56,6 +63,17 @@ pub const NodeExpr = struct { } return try childrenArray.toOwnedSlice(); } + pub fn inferType(self: NodeExpr, allocator: std.mem.Allocator, table: *symb.SymbolTable) TypeError!TypeIdent { + const expectedType = try switch (self.kind) { + .call => |call| if (table.getValue(call.ident.ident)) |symbol| try symbol.typ.Function.output.toTypeIdent(allocator) else TypeError.UnknownIdentifier, + .ident => |id| if (table.getValue(id.ident)) |symbol| try symbol.typ.toTypeIdent(allocator) else TypeError.UnknownIdentifier, + .intLit => TypeIdent{ .ident = "i32", .list = false }, + .stringLit => TypeIdent{ .ident = "[u8]", .list = true }, + }; + if (self.typ) |typ| { + return if (std.mem.eql(u8, typ.ident, expectedType.ident)) expectedType else TypeError.IncorrectType; + } else return expectedType; + } }; pub fn map(comptime T: type, comptime F: type, slice: []const F, func: fn (F) T) []const T { @@ -182,10 +200,7 @@ pub const Parser = struct { }, }; } - typ = TypeIdent{ - .ident = "i32", - .list = false, - }; + typ = null; break :blk ExprKind{ .ident = ident }; }, .stringLit => { @@ -315,7 +330,6 @@ pub const Parser = struct { } fn parseAssign(self: *Parser) ParsingError!NodeStmt { - std.debug.print("{any}\n", .{self.tokens.peek().?}); const ident = (try self.tokens.consume(.ident)).?; _ = try self.tokens.consume(.equal); const expr = try self.parseExpr(); @@ -348,14 +362,13 @@ pub const Parser = struct { fn parseVariable(self: *Parser) ParsingError!NodeStmt { _ = try self.tokens.consume(.variable); var typ: ?TypeIdent = null; - if (self.tokens.consume(.colon)) |_| { - typ = .{ - .ident = (try self.tokens.consume(.ident)).?.ident, - .list = false, - }; - } else |err| { - if (err != tok.TokenizeError.ExpectedToken) return errcast(.{err}); - } + _ = self.tokens.consume(.colon) catch { + return ParsingError.TypeRequiredForVarbl; + }; + typ = .{ + .ident = (try self.tokens.consume(.ident)).?.ident, + .list = false, + }; const ident = (try self.tokens.consume(.ident)).?; _ = try self.tokens.consume(.equal); const expr = try self.parseExpr(); |
