jorts/src/token.zig

108 lines
2 KiB
Zig
Raw Normal View History

2019-06-01 00:23:51 +00:00
const std = @import("std");
pub const TokenType = enum(u6) {
2019-06-01 00:23:51 +00:00
// Single-character tokens.
LEFT_PAREN,
RIGHT_PAREN,
LEFT_BRACE,
RIGHT_BRACE,
COMMA,
DOT,
MINUS,
PLUS,
SEMICOLON,
SLASH,
STAR,
// One or two character tokens.
BANG,
BANG_EQUAL,
EQUAL,
EQUAL_EQUAL,
GREATER,
GREATER_EQUAL,
LESS,
LESS_EQUAL,
// Literals.
IDENTIFIER,
STRING,
NUMBER,
// Keywords.
AND,
CLASS,
ELSE,
FALSE,
FUN,
FOR,
IF,
NIL,
OR,
PRINT,
RETURN,
SUPER,
THIS,
TRUE,
VAR,
WHILE,
EOF,
};
pub fn TokenFactory(
comptime T: type,
) type {
return struct {
const Self = @This();
ttype: TokenType,
lexeme: []u8,
line: usize,
literal: T,
pub fn init(
ttype: TokenType,
lexeme: []u8,
line: usize,
literal: T,
) Self {
return Self{
.ttype = ttype,
.lexeme = lexeme,
.line = line,
.literal = literal,
};
}
pub fn printToken(self: Self, stdout: var) !void {
if (T == void) {
try stdout.print(
"Token(type={x}, lexeme='{}', line={})\n",
self.ttype,
self.lexeme,
self.line,
);
} else {
try stdout.print(
"Token(type={x}, lexeme='{}', line={} literal='{}')\n",
self.ttype,
self.lexeme,
self.line,
self.literal,
);
}
2019-06-01 00:23:51 +00:00
}
};
}
pub const SimpleToken = TokenFactory(void);
pub const SliceToken = TokenFactory([]u8);
2019-06-01 01:08:41 +00:00
pub const NumberToken = TokenFactory(f32);
2019-06-01 00:23:51 +00:00
pub const Token = union(enum) {
2019-06-01 00:23:51 +00:00
Simple: SimpleToken,
Slice: SliceToken,
2019-06-01 01:08:41 +00:00
Number: NumberToken,
2019-06-01 00:23:51 +00:00
};