68 lines
1003 B
Zig
68 lines
1003 B
Zig
const std = @import("std");
|
|
|
|
pub const TokenType = enum {
|
|
// 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 const Token = struct {
|
|
token_type: TokenType,
|
|
lexeme: []const u8,
|
|
line: u64,
|
|
value: ?Value,
|
|
|
|
pub const Value = union {
|
|
number: f64,
|
|
string: []const u8,
|
|
};
|
|
|
|
fn toString(self: *Token, alloc: std.mem.Allocator) ![]u8 {
|
|
return std.fmt.allocPrint(alloc, "{} {} {}", .{ self.token_type, self.lexeme, self.line });
|
|
}
|
|
};
|