mighty

The mighty programming language, compiler and tools (WIP)
Log | Files | Refs

commit bfc8d54bb3b19973746bfcdd7cc0ba10ed280422
parent 7df7dd39e477ce01bd2712e6f554048772385816
Author: citbl <citbl@citbl.org>
Date:   Sat, 23 May 2026 19:37:10 +1000

wip numbers

Diffstat:
Mmtcl/main.lua | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mmtcl/target.mty | 4+++-
2 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/mtcl/main.lua b/mtcl/main.lua @@ -12,32 +12,65 @@ function is_alpha(c) return (c >= a and c <= z) or (c >= A and c <= Z) end +function is_digit(c) + return (c >= zero and c <= nine) +end + function alpha_num(c) - return is_alpha(c) or (c >= zero and c <= nine) + return is_alpha(c) or is_digit(c) end function read_ident(start, src) local word = "" - local adv = 0 local i = start + local c = 0 while i <= #src do - local c = src:sub(i, i):byte() + c = src:sub(i, i):byte() if not alpha_num(c) then + i = i - 1 -- rewind break end i = i + 1 end - word = src:sub(start, i - 1) - print(word) + word = src:sub(start, i) return word, i end +function read_number(start, src) + local num = "" + local i = start + local c = 0 + local ch = "" + local is_float = false + while i <= #src do + ch = src:sub(i, i) + c = ch:byte() + if ch == "." then + is_float = true + end + if not is_digit(c) and ch ~= "." then + i = i - 1 -- rewind + break + end + i = i + 1 + end + num = src:sub(start, i) + return num, i, is_float +end + TK = { IDK = "UNKNOWN", COLON = "colon", COLONCOLON = "colcol", IDENT = "ident", DOT = "dot", + EQ = "assign", + EQEQ = "equality", + L_PAREN = "l paren", + R_PAREN = "r paren", + DBL_QUOTE = "dbl quote", + SGL_QUOTE = "sgl quote", + LIT_NUMBER = "num lit", } Token = { kind = TK.IDK } @@ -60,6 +93,14 @@ function lex(src) local c = src:sub(i, i) if c == "\t" or c == "\n" or c == " " or c == "\r" then + -- nothing + elseif is_alpha(c:byte()) then + word, i = read_ident(i, src) + tokens[#tokens + 1] = { kind = TK.IDENT, lexeme = word } + elseif is_digit(c:byte()) then + number, i, is_float = read_number(i, src) + kind = if is_float then TK.LIT_FLOAT else TK.LIT_INT end + tokens[#tokens + 1] = { kind = kind, lexeme = number } elseif c == ":" then if next() == ":" then tokens[#tokens + 1] = { kind = TK.COLONCOLON, lexeme = "::" } @@ -67,9 +108,21 @@ function lex(src) else tokens[#tokens + 1] = { kind = TK.COLON, lexeme = ":" } end - elseif is_alpha(c:byte()) then - word, i = read_ident(i, src) - tokens[#tokens + 1] = { kind = TK.IDENT, lexeme = word } + elseif c == "=" then + if next() == "=" then + tokens[#tokens + 1] = { kind = TK.EQEQ, lexeme = "==" } + i = i + 1 + else + tokens[#tokens + 1] = { kind = TK.EQ, lexeme = "=" } + end + elseif c == "(" then + tokens[#tokens + 1] = { kind = TK.L_PAREN, lexeme = "(" } + elseif c == ")" then + tokens[#tokens + 1] = { kind = TK.R_PAREN, lexeme = ")" } + elseif c == "'" then + tokens[#tokens + 1] = { kind = TK.SGL_QUOTE, lexeme = "'" } + elseif c == '"' then + tokens[#tokens + 1] = { kind = TK.DBL_QUOTE, lexeme = '"' } else tokens[#tokens + 1] = { kind = TK.IDK, lexeme = "" } end diff --git a/mtcl/target.mty b/mtcl/target.mty @@ -1,6 +1,8 @@ use std fx main :: int = - print("hello") + print ("hello") + j = 12 + j = 12.5 ret 0