commit c89518dc5e05b4a38ac040883acc5ddf5d9177fc
parent 012ddf049ee400fba989ec4bd2356e6ea124c11b
Author: citbl <citbl@citbl.org>
Date: Mon, 25 May 2026 19:51:42 +1000
changes and parsing
Diffstat:
4 files changed, 53 insertions(+), 17 deletions(-)
diff --git a/mtcl/idea.mty b/mtcl/idea.mty
@@ -0,0 +1,27 @@
+
+use std
+
+fx main(int a, int b) int ::
+ print("hello")
+ ret 0
+end
+
+fx main :: print("hello world") end
+
+fx main ::
+ print("hello, world")
+ while true do
+ end
+end
+
+pub struct Person ::
+ float age
+ int height
+ str name
+end
+
+enum Type ::
+ FLOAT
+ INT
+ JACK
+end
diff --git a/mtcl/lexer.lua b/mtcl/lexer.lua
@@ -90,6 +90,8 @@ TK = {
IDK = "UNKNOWN",
COLON = "colon",
COLONCOLON = "colcol",
+ EOS = "EOS end stmt",
+ DO = "DO KEYWORD",
IDENT = "ident",
KEYWORD = "keyword",
DOT = "dot",
@@ -109,23 +111,17 @@ TK = {
local keywords = {
["ns"] = true,
- ["in"] = true,
- ["from"] = true,
["use"] = true,
+ ["from"] = 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,
+ ["end"] = true,
["pub"] = true,
["fx"] = true,
["fn"] = true,
@@ -182,6 +178,7 @@ function lexer:lex(file, src)
if is_space(c) then
if c == "\n" then
+ self:add(TK.EOS, "CR")
self.col = 1
self.line = self.line + 1
else
@@ -244,6 +241,10 @@ function lexer:lex(file, src)
self:add(TK.R_BRACE, "}")
self.col = self.col + 1
i = i + 1
+ elseif c == ";" then
+ self:add(TK.EOS, ";")
+ self.col = self.col + 1
+ i = i + 1
elseif c == "," then
self:add(TK.COMMA, ",")
self.col = self.col + 1
diff --git a/mtcl/parser.lua b/mtcl/parser.lua
@@ -33,7 +33,10 @@ function parser:add(ast)
end
function parser:parse_statement()
- print("todo parse statement")
+ local next = parser:peek().kind
+ print("todo parse statement: " .. next)
+ if next == TK.IDENT then
+ end
parser:advance()
end
@@ -66,9 +69,10 @@ function parser:parse_function(is_exported)
break
end
end
- parser:expect(TK.L_BRACE, "no rbrace end of function")
+ parser:expect(TK.COLONCOLON, "no rbrace start of function")
- while parser:peek().kind ~= TK.R_BRACE do -- function body statements
+ while parser:peek().lexeme ~= "end" do
+ -- function body statements
local stmt = parser:parse_statement()
statements[#statements + 1] = stmt
end
@@ -80,6 +84,8 @@ function parser:parse_use()
print("parse use")
self:advance()
local lib = parser:match_ident()
+ self:advance()
+ self:expect(TK.EOS, "use statement found without a end statement \\n|;")
local node = ast.use(lib)
parser:add(node)
end
@@ -91,7 +97,7 @@ function unhandled(token, extramsg)
.. token.line
.. ":"
.. token.col
- .. "\n\t UNHANDLED TOKEN "
+ .. "\n\t UNHANDLED TOKEN: "
.. token.lexeme
.. "\t "
.. (extramsg or "")
@@ -104,11 +110,13 @@ function parser:parse_program()
local kind = token.kind
local word = token.lexeme
print("peek:", word)
- if kind == "keyword" and word == "use" then
+ if kind == TK.EOS then
+ -- nothing, advance
+ elseif kind == TK.KEYWORD and word == "use" then
self:parse_use()
- elseif kind == "keyword" and word == "fx" then
+ elseif kind == TK.KEYWORD and word == "fx" then
self:parse_function(true)
- elseif kind == "keyword" and word == "fx" then
+ elseif kind == TK.KEYWORD and word == "fn" then
self:parse_function(false)
else
unhandled(token)
diff --git a/mtcl/target.mty b/mtcl/target.mty
@@ -1,6 +1,6 @@
use std
-fx main(int a, int b) int {
+fx main(int a, int b) int ::
print("hello")
ret 0
-}
+end