commit c1905a40537d285d31a691f12aa39126fc769fa6
parent 67a49ea1fb8b528515d10112a90f12a30ff0aff4
Author: citbl <citbl@citbl.org>
Date: Sun, 24 May 2026 15:33:23 +1000
upgrade tokens
Diffstat:
1 file changed, 36 insertions(+), 1 deletion(-)
diff --git a/mtcl/lexer.lua b/mtcl/lexer.lua
@@ -71,6 +71,7 @@ TK = {
COLON = "colon",
COLONCOLON = "colcol",
IDENT = "ident",
+ KEYWORD = "keyword",
DOT = "dot",
EQ = "assign",
EQEQ = "equality",
@@ -82,6 +83,31 @@ TK = {
LIT_INT = "a int",
}
+local keywords = {
+ ["ns"] = true,
+ ["in"] = true,
+ ["from"] = true,
+ ["use"] = true,
+ ["ffi"] = true,
+ ["drop"] = true,
+ ["as"] = true,
+ ["of"] = true,
+ ["and"] = true,
+ ["or"] = true,
+ ["ref"] = true,
+ ["struct"] = true,
+ ["enum"] = true,
+ ["pre"] = true,
+ ["post"] = true,
+ ["inv"] = true,
+ ["if"] = true,
+ ["else"] = true,
+ ["pub"] = true,
+ ["fx"] = true,
+ ["fn"] = true,
+ ["ret"] = true,
+}
+
Token = { kind = TK.IDK, lexeme = "", file = "", line = 0, col = 0 }
local function print_token(t)
@@ -98,6 +124,14 @@ local function print_token(t)
)
end
+local function upgrade(word)
+ if keywords[word] then
+ print("upgraded " .. word)
+ return TK.KEYWORD
+ end
+ return TK.IDENT
+end
+
function lexer:add(kind, lexeme)
token = {
kind = kind,
@@ -132,7 +166,8 @@ function lexer:lex(file, src)
i = i + 1
elseif is_alpha(c:byte()) then
word, i = read_ident(i, src)
- self:add(TK.IDENT, word)
+ kind = upgrade(word)
+ self:add(kind, word)
self.col = self.col + #word
i = i + 1
elseif is_digit(c:byte()) then