commit 01f5f24a69ff4cbd500e8cbf0aff101956a6cb24
parent 6f86995144a177699b918a252ecf573acf625ba0
Author: citbl <citbl@citbl.org>
Date: Sun, 24 May 2026 20:23:17 +1000
parsing wip
Diffstat:
3 files changed, 64 insertions(+), 17 deletions(-)
diff --git a/mtcl/lexer.lua b/mtcl/lexer.lua
@@ -77,10 +77,13 @@ TK = {
EQEQ = "equality",
L_PAREN = "l paren",
R_PAREN = "r paren",
+ L_BRACE = "l brace",
+ R_BRACE = "r brace",
DBL_QUOTE = "dbl quo",
SGL_QUOTE = "sgl quo",
LIT_FLOAT = "a float",
LIT_INT = "a int",
+ COMMA = "comma",
}
local keywords = {
@@ -207,6 +210,18 @@ function lexer:lex(file, src)
self:add(TK.R_PAREN, ")")
self.col = self.col + 1
i = i + 1
+ elseif c == "{" then
+ self:add(TK.L_BRACE, "{")
+ self.col = self.col + 1
+ i = i + 1
+ elseif c == "}" then
+ self:add(TK.R_BRACE, "}")
+ self.col = self.col + 1
+ i = i + 1
+ elseif c == "," then
+ self:add(TK.COMMA, ",")
+ self.col = self.col + 1
+ i = i + 1
elseif c == "'" then
self:add(TK.SGL_QUOTE, "'")
self.col = self.col + 1
diff --git a/mtcl/parser.lua b/mtcl/parser.lua
@@ -10,6 +10,15 @@ function parser:advance()
self.pos = self.pos + 1
end
+function parser:expect(kind, extramsg)
+ local current = self.tokens[self.pos]
+ if current.kind ~= kind then
+ unhandled(current, "EXPECTED kind: " .. kind, extramsg)
+ end
+ parser:advance()
+ return current
+end
+
function parser:match_ident()
local current = self.tokens[self.pos]
if current.kind ~= "ident" then
@@ -18,17 +27,41 @@ function parser:match_ident()
return current
end
-function parser:swallow(word)
- local current = self.tokens[self.pos]
- if current == word then
- parser:advance()
- else
- unhandled(current, "EXPECTED: " .. word)
- end
+function parser:parse_statement()
+ print("todo parse statement")
+ parser:advance()
end
-function parser:parse_function()
- print("parse function decl")
+function parser:parse_function(is_exported)
+ local token = parser:peek()
+ parser:advance() -- consume fx/fn
+ local ident = parser:expect(TK.IDENT)
+ parser:expect(TK.L_PAREN)
+ while parser:peek().kind ~= TK.R_PAREN do
+ local kind = parser:expect(TK.IDENT, "missing kind in param")
+ local name = parser:expect(TK.IDENT, "missing name in param")
+
+ if parser:peek().kind == TK.COMMA then
+ parser:advance()
+ else
+ break
+ end
+ end
+ parser:expect(TK.R_PAREN)
+ while parser:peek().kind ~= TK.L_BRACE do -- return type(s)
+ local rettype = parser:expect(TK.IDENT, "rettype no ident")
+ if parser:peek().kind == TK.COMMA then
+ parser:advance()
+ else
+ break
+ end
+ end
+ parser:expect(TK.L_BRACE, "no rbrace end of function")
+
+ while parser:peek().kind ~= TK.R_BRACE do -- function body statements
+ parser:parse_statement()
+ end
+ local node = ast.func(name, params, ret, body)
end
function parser:add(ast)
@@ -38,10 +71,9 @@ end
function parser:parse_use()
print("parse use")
self:advance()
- local ident = parser:match_ident()
+ local lib = parser:match_ident()
local node = ast.use(lib)
table.insert(self.nodes, node)
- parser:advance()
end
function unhandled(token, extramsg)
@@ -67,7 +99,9 @@ function parser:parse_program()
if kind == "keyword" and word == "use" then
self:parse_use()
elseif kind == "keyword" and word == "fx" then
- self:parse_function()
+ self:parse_function(true)
+ elseif kind == "keyword" and word == "fx" then
+ self:parse_function(false)
else
unhandled(token)
break
diff --git a/mtcl/target.mty b/mtcl/target.mty
@@ -1,8 +1,6 @@
use std
-fx main :: int =
- print ("hello")
- j = 12
- j = 12.5
+fx main(int a, int b) int {
+ print("hello")
ret 0
-
+}