Skip to content

Commit

Permalink
Make helper functions explicitly inline
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiisbored committed Jul 8, 2023
1 parent 1f86323 commit 912dd36
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ pub fn deinit(self: *Self) void {

// == HELPER FUNCTIONS == //

fn errorAt(self: *Self, token: *const Scanner.Token, message: []const u8) Error!void {
inline fn errorAt(self: *Self, token: *const Scanner.Token, message: []const u8) Error!void {
self.error_token = token;
self.error_message = message;

return Error.ParserError;
}

fn @"error"(self: *Self, message: []const u8) Error!void {
inline fn @"error"(self: *Self, message: []const u8) Error!void {
try self.errorAt(&self.previous, message);
}

fn errorAtCurrent(self: *Self, message: []const u8) Error!void {
inline fn errorAtCurrent(self: *Self, message: []const u8) Error!void {
try self.errorAt(&self.current, message);
}

fn advance(self: *Self) Error!void {
inline fn advance(self: *Self) Error!void {
self.previous = self.current;

while (true) {
Expand All @@ -80,7 +80,7 @@ fn advance(self: *Self) Error!void {
}
}

fn consume(self: *Self, @"type": Scanner.TokenType, message: []const u8) Error!void {
inline fn consume(self: *Self, @"type": Scanner.TokenType, message: []const u8) Error!void {
if (self.current.type == @"type") {
try self.advance();
return;
Expand All @@ -89,11 +89,11 @@ fn consume(self: *Self, @"type": Scanner.TokenType, message: []const u8) Error!v
try self.errorAtCurrent(message);
}

fn check(self: *Self, @"type": Scanner.TokenType) bool {
inline fn check(self: *Self, @"type": Scanner.TokenType) bool {
return self.current.type == @"type";
}

fn match(self: *Self, @"type": Scanner.TokenType) Error!bool {
inline fn match(self: *Self, @"type": Scanner.TokenType) Error!bool {
if (!self.check(@"type")) {
return false;
}
Expand Down
22 changes: 11 additions & 11 deletions src/Scanner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,66 +53,66 @@ pub fn init(source: []const u8) Self {

// === HELPER FUNCTIONS === //

fn isDigit(c: u8) bool {
inline fn isDigit(c: u8) bool {
return c >= '0' and c <= '9';
}

fn isAlpha(c: u8) bool {
inline fn isAlpha(c: u8) bool {
return c >= 'A' and c <= 'Z';
}

fn isAtEnd(self: *Self) bool {
inline fn isAtEnd(self: *Self) bool {
return self.current >= self.source.len;
}

fn forward(self: *Self) void {
inline fn forward(self: *Self) void {
self.current += 1;
}

fn advance(self: *Self) u8 {
inline fn advance(self: *Self) u8 {
self.forward();
return self.source[self.current - 1];
}

fn peek(self: *Self) u8 {
inline fn peek(self: *Self) u8 {
if (self.isAtEnd()) {
return 0;
}
return self.source[self.current];
}

fn peekNext(self: *Self) u8 {
inline fn peekNext(self: *Self) u8 {
if (self.isAtEnd() or self.current + 1 >= self.source.len) {
return 0;
}
return self.source[self.current + 1];
}

fn match(self: *Self, c: u8) bool {
inline fn match(self: *Self, c: u8) bool {
if (self.isAtEnd() or self.peek() != c) {
return false;
}
self.current += 1;
return true;
}

fn makeToken(self: *Self, @"type": TokenType) Token {
inline fn makeToken(self: *Self, @"type": TokenType) Token {
return Token{
.type = @"type",
.str = self.source[self.start..self.current],
.line = self.line,
};
}

fn errorToken(self: *Self, message: []const u8) Token {
inline fn errorToken(self: *Self, message: []const u8) Token {
return Token{
.type = .@"error",
.str = message,
.line = self.line,
};
}

fn checkKeyword(
inline fn checkKeyword(
self: *Self,
start: usize,
rest: []const u8,
Expand Down

0 comments on commit 912dd36

Please sign in to comment.