From 56040f3ff85e77311f0c864a89afd63fcf1bdb50 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 3 Nov 2025 23:40:02 -0800 Subject: add js-lang, refactor some ts-lang stuff --- src/js-lang/core/lexer.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/js-lang/core/lexer.ts (limited to 'src/js-lang/core/lexer.ts') diff --git a/src/js-lang/core/lexer.ts b/src/js-lang/core/lexer.ts new file mode 100644 index 0000000..95e0e19 --- /dev/null +++ b/src/js-lang/core/lexer.ts @@ -0,0 +1,46 @@ +import { TokenType, Lex, Token } from "../../ts-lang"; + +const WHITESPACE_TOKENS = [ + TokenType.SPACE, + TokenType.COMMA, + TokenType.SEMICOLON, +] as string[]; + +export const lex = (raw: Raw): Lex => { + let _raw: string = raw; + let nameCollection = ""; + const tokens: Token[] = []; + + while (_raw) { + const head = _raw[0]; + _raw = _raw.slice(1); + + const processNameCollection = () => { + if (nameCollection) { + tokens.push({ type: TokenType.NAME, name: nameCollection }); + nameCollection = ""; + } + }; + + if (WHITESPACE_TOKENS.includes(head)) { + processNameCollection(); + continue; + } + + if (head === TokenType.OPEN_PAREN) { + processNameCollection(); + tokens.push({ type: TokenType.OPEN_PAREN, name: "" }); + continue; + } + + if (head === TokenType.CLOSE_PAREN) { + processNameCollection(); + tokens.push({ type: TokenType.CLOSE_PAREN, name: "" }); + continue; + } + + nameCollection += head; + } + + return tokens as Lex; +}; -- cgit v1.2.3-70-g09d2