commit 67a49ea1fb8b528515d10112a90f12a30ff0aff4
parent ef651a5ed314db795fe0573e45d482f78c66193f
Author: citbl <citbl@citbl.org>
Date: Sun, 24 May 2026 15:07:49 +1000
wip
Diffstat:
2 files changed, 70 insertions(+), 10 deletions(-)
diff --git a/mtcl/lexer.lua b/mtcl/lexer.lua
@@ -1,6 +1,6 @@
local lexer = {
tokens = {},
- file = "",
+ file = "---",
line = 1,
col = 1,
}
@@ -82,7 +82,7 @@ TK = {
LIT_INT = "a int",
}
-Token = { kind = TK.IDK, file = "", line = 0, col = 0 }
+Token = { kind = TK.IDK, lexeme = "", file = "", line = 0, col = 0 }
local function print_token(t)
print(
@@ -111,6 +111,7 @@ end
function lexer:lex(file, src)
local i = 1
+ self.file = file
local function next()
if i + 1 <= #src then
diff --git a/mtcl/parser.lua b/mtcl/parser.lua
@@ -1,19 +1,78 @@
-local parser = { file, src, tokens }
+local parser = { pos = 1 }
+local ast = require("ast")
+local node = {}
+
+function parser:peek()
+ return self.tokens[self.pos] or 0
+end
+
+function parser:advance()
+ self.pos = self.pos + 1
+end
+
+function parser:swallow(word)
+ local current = self.tokens[self.pos]
+ if current == word then
+ parser:advance()
+ else
+ unhandled(current, "EXPECT: " .. word)
+ end
+end
+
+function parser:parse_function()
+ print("parse function" .. self.pos)
+end
+
+function parser:parse_use()
+ print("parse use" .. self.pos)
+ self:swallow("use")
+end
+
+function unhandled(token, extramsg)
+ print(
+ token.file
+ .. " at L"
+ .. token.line
+ .. ":"
+ .. token.col
+ .. "\n\t UNHANDLED TOKEN "
+ .. token.lexeme
+ .. "\t "
+ .. (extramsg or "")
+ )
+end
+
+function parser:parse_program()
+ while self:peek() ~= 0 do
+ local token = self:peek()
+ local word = token.lexeme
+ print("peek:", word)
+ if word == "use" then
+ self:parse_use()
+ elseif word == "fx" then
+ self:parse_function()
+ else
+ unhandled(token)
+ break
+ end
+ self:advance()
+ end
+end
function parser:parse(file, src, tokens)
- --
self.file = file
self.src = src
self.tokens = tokens
-
+ self.pos = 1
local i = 1
- while i <= #tokens do
- local t = tokens[i]
- t2 = tokens[i + 1] or 0
+ self:parse_program()
- i = i + 1
- end
+ -- while i <= #tokens do
+ -- local t = tokens[i]
+ -- t2 = tokens[i + 1] or 0
+ -- i = i + 1
+ -- end
end
return parser