1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195import { TokenType, Token } from "./interfaces/token"; const KEYWORDS: { [key: string]: TokenType } = { "f": TokenType.FUNCTION, "true": TokenType.TRUE, "false": TokenType.FALSE, "and": TokenType.AND, "or": TokenType.OR, "not": TokenType.NOT, "if": TokenType.IF, "else": TokenType.ELSE, "while": TokenType.WHILE, "break": TokenType.BREAK, "return": TokenType.RETURN } export class Lexer { #source: string; #character: string; #cursor: number; #next_cursor: number; constructor(source: string) { this.#source = source; this.#cursor = 0; this.#next_cursor = 0; this.#character = "\0"; this.read_char(); } public isEOF(): boolean { return this.#cursor > this.#source.length; } public peek(): string { if (this.#next_cursor >= this.#source.length) return "\0"; return this.#source[this.#next_cursor]; } /** * returns the next token * @throws {Error} */ public next(): Token { this.skip_whitespace(); let token: Token; switch (this.#character) { case "=": if (this.peek() == '=' || this.peek() == '>') { let char = this.#character; this.read_char(); token = new Token(this.#character == '=' ? TokenType.EQUALS : TokenType.ARROW, char + this.#character); } else { token = new Token(TokenType.ASSIGN, this.#character); } break; case "{": token = new Token(TokenType.LBRACE, this.#character); break; case "}": token = new Token(TokenType.RBRACE, this.#character); break; case "(": token = new Token(TokenType.LPAREN, this.#character); break; case ")": token = new Token(TokenType.RPAREN, this.#character); break; case "[": token = new Token(TokenType.LBRACKET, this.#character); break; case "]": token = new Token(TokenType.RBRACKET, this.#character); break; case ",": token = new Token(TokenType.COMMA, this.#character); break; case ":": token = new Token(TokenType.COLON, this.#character); break; case ".": token = new Token(TokenType.DOT, this.#character); break; case "+": token = new Token(TokenType.PLUS, this.#character); break; case "-": token = new Token(TokenType.MINUS, this.#character); break; case "*": if (this.peek() == '*') { let char = this.#character; this.read_char(); token = new Token(TokenType.DOUBLE_ASTERISK, char + this.#character); } else { token = new Token(TokenType.ASTERISK, this.#character); } break; case "/": token = new Token(TokenType.SLASH, this.#character); break; case "%": token = new Token(TokenType.PERCENT, this.#character); break; case ">": if (this.peek() == '=') { let char = this.#character; this.read_char(); token = new Token(TokenType.GTE, char + this.#character); } else { token = new Token(TokenType.GT, this.#character); } break; case "<": if (this.peek() == '=') { let char = this.#character; this.read_char(); token = new Token(TokenType.LTE, char + this.#character); } else { token = new Token(TokenType.LT, this.#character); } break; case ";": token = new Token(TokenType.SEMICOLON, this.#character); break; case "\0": token = new Token(TokenType.EOF, this.#character); break; case "\"": token = new Token(TokenType.STRING, this.read_string()); break; case "!": if (this.peek() == '=') { this.read_char(); token = new Token(TokenType.NOT_EQUALS, "!="); } else token = new Token(TokenType.BANG, this.#character); break; default: token = new Token(TokenType.ILLEGAL, this.#character); if (this.isalpha()) { token.literal = this.read_identifier(); token.type = this.lookup_identifier(token.literal); return token; } else if (this.isdigit()) { token.literal = this.read_number(); token.type = TokenType.INT; return token; } } this.read_char(); return token; } private read_char(): void { if (this.#next_cursor >= this.#source.length) { this.#character = "\0"; } else this.#character = this.#source[this.#next_cursor] this.#cursor = this.#next_cursor; this.#next_cursor += 1; } private read_identifier(): string { let pos = this.#cursor; while (this.isalpha() || this.isdigit()) this.read_char(); return this.#source.slice(pos, this.#cursor); } private read_number(): string { let pos = this.#cursor; while (this.isdigit()) this.read_char(); return this.#source.slice(pos, this.#cursor); } private read_string(): string { let result = ""; let escaped = false; while (!this.isEOF()) { this.read_char(); if (escaped) { switch (this.#character) { case 'n': result += '\n'; break; case 't': result += '\t'; break; case '\"': result += '\"'; break; case '\\': result += '\\'; break; default: result += this.#character; } escaped = false; // Reset escape flag } else { if (this.#character === '\\') escaped = true; // The next character is an escape sequence if (this.#character === '\"') break; result += this.#character; // Normal character, add to result } } return result; } private lookup_identifier(ident: string): TokenType { if (ident in KEYWORDS) return KEYWORDS[ident]; return TokenType.IDENTIFIER; } private skip_whitespace(): void { const whitespace = new Set(['\n', '\t', '\r', ' ']) while (whitespace.has(this.#character)) this.read_char(); } private isalpha(): boolean { return 'A' <= this.#character && this.#character <= 'Z' || 'a' <= this.#character && this.#character <= 'z' || this.#character == '_'; } private isdigit(): boolean { return "0" <= this.#character && this.#character <= '9'; } }