Skip to content

Commit

Permalink
fix: upcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
kamva9697 committed Dec 17, 2023
1 parent 2edda48 commit 6a0ace8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const Object = struct {
Function,
Error,

pub fn Type(comptime ty: ObjectType) type {
pub inline fn Type(comptime ty: ObjectType) type {
return switch (ty) {
.Integer => Integer,
.Boolean => Boolean,
Expand All @@ -32,7 +32,7 @@ pub const Object = struct {
.Error => Error,
};
}
pub fn toString(ty: ObjectType) []const u8 {
pub inline fn toString(ty: ObjectType) []const u8 {
return switch (ty) {
.Integer => "Integer",
.Boolean => "Boolean",
Expand All @@ -48,7 +48,7 @@ pub const Object = struct {
return @fieldParentPtr(ty.Type(), "base", base);
}

pub fn create(comptime T: type, alloc: Allocator, value: T) !*T {
pub inline fn create(comptime T: type, alloc: Allocator, value: T) !*T {
const obj = try alloc.create(T);
obj.* = value;
return obj;
Expand All @@ -57,22 +57,22 @@ pub const Object = struct {
pub fn Inspect(self: *Object, alloc: Allocator, writer: anytype) !void {
switch (self.ty) {
.Integer => {
const int = cast(self, .Integer).?;
const int = cast(self, .Integer);
try writer.print("{d}", .{int.value});
},
.Boolean => {
const _bool = cast(self, .Boolean).?;
const _bool = cast(self, .Boolean);
try writer.print("{any}", .{_bool.value});
},
.ReturnValue => {
const rv = cast(self, .ReturnValue).?;
const rv = cast(self, .ReturnValue);
try rv.value.Inspect(alloc, writer);
},
.Null => {
try writer.print("null", .{});
},
.Function => {
const func = cast(self, .Function).?;
const func = cast(self, .Function);

var params = std.ArrayList(u8).init(alloc);
const paramsWriter = params.writer();
Expand All @@ -88,7 +88,7 @@ pub const Object = struct {
try writer.writeAll("\n");
},
.Error => {
const err = cast(self, .Error).?;
const err = cast(self, .Error);
try writer.print("Error: {s}", .{err.message});
},
}
Expand Down
12 changes: 6 additions & 6 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub const Parser = struct {
return p;
}

pub fn nextToken(self: *Parser) void {
pub inline fn nextToken(self: *Parser) void {
self.curToken = self.peekToken;
self.peekToken = self.lex.nextToken();
}
Expand All @@ -120,7 +120,7 @@ pub const Parser = struct {
return treePtr.toNode();
}

pub fn parseStatement(self: *Parser) !?*Node {
pub inline fn parseStatement(self: *Parser) !?*Node {
return switch (self.curToken.Type) {
.LET => try self.parseLetStatement(),
.RETURN => try self.parseReturnStatement(),
Expand Down Expand Up @@ -442,10 +442,10 @@ pub const Parser = struct {
}

///////////// Utilities ////////////////////////
pub fn peekPrecedence(self: *Parser) Precedence {
pub inline fn peekPrecedence(self: *Parser) Precedence {
return Precedence.fromTokenType(self.peekToken.Type);
}
pub fn curPrecedence(self: *Parser) Precedence {
pub inline fn curPrecedence(self: *Parser) Precedence {
return Precedence.fromTokenType(self.curToken.Type);
}

Expand Down Expand Up @@ -476,13 +476,13 @@ pub const Parser = struct {
try self.errors.append(self.arena.allocator(), ctx);
}

pub fn registerPrefix(self: *Parser, currentToken: TokenType, prefixFunc: prefixParseFn) void {
pub inline fn registerPrefix(self: *Parser, currentToken: TokenType, prefixFunc: prefixParseFn) void {
self.prefixParseFns.put(self.arena.allocator(), currentToken, prefixFunc) catch |err| {
std.debug.panic("Error: {any}", .{err});
};
}

pub fn registerInfix(self: *Parser, currentToken: TokenType, infixFunc: infixParseFn) void {
pub inline fn registerInfix(self: *Parser, currentToken: TokenType, infixFunc: infixParseFn) void {
self.infixParseFns.put(self.arena.allocator(), currentToken, infixFunc) catch |err| {
std.debug.panic("Error: {any}", .{err});
};
Expand Down
2 changes: 0 additions & 2 deletions src/repl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub fn start(gpa: std.mem.Allocator) !void {
var bufOut = std.ArrayList(u8).init(gpa);
const writer = bufOut.writer();
const env = try Environment.newEnvironment(gpa);
const internPool = evaluator.InternPool(gpa);
_ = internPool;

while (true) {
print("{s}", .{Prompt});
Expand Down

0 comments on commit 6a0ace8

Please sign in to comment.