Last active
March 31, 2020 17:48
-
-
Save bastienvoirin/32297ea34736d528707325837d864217 to your computer and use it in GitHub Desktop.
Lua compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local RESERVED = { | |
BLOCK_START = { | |
DO = "do", | |
ELSE = "else", | |
ELSEIF = "elseif", | |
FOR = "for", | |
FUNCTION = "function", | |
IF = "if", | |
REPEAT = "repeat", | |
THEN = "then", | |
WHILE = "while" | |
}, | |
BLOCK_END = { | |
END = "end", | |
UNTIL = "until" | |
}, | |
BLOCK_EXIT = { | |
BREAK = "break", | |
RETURN = "return" | |
} | |
NIL = { | |
NIL = "nil" | |
}, | |
BOOLEAN = { | |
FALSE = "false", | |
TRUE = "true" | |
}, | |
KEYWORD = { | |
IN = "in", | |
LOCAL = "local" | |
}, | |
UNOP = { | |
NOT = "not", | |
NEG = "- (unary)" | |
}, | |
BINOP = { | |
POW = "^", | |
MUL = "*", | |
DIV = "/", | |
ADD = "+", | |
SUB = "-", | |
CAT = "..", | |
OR = "or", | |
AND = "and" | |
}, | |
COMP = { | |
LT = "<", | |
GT = ">", | |
LE = "<=", | |
GE = ">=", | |
NE = "~=", | |
EQ = "==" | |
} | |
} | |
local PRIORITY = { | |
{ | |
RESERVED.BINOP.POW | |
}, | |
{ | |
RESERVED.UNOP.NOT, | |
RESERVED.UNOP.NEG | |
}, | |
{ | |
RESERVED.BINOP.MUL, | |
RESERVED.BINOP.DIV | |
}, | |
{ | |
RESERVED.BINOP.ADD, | |
RESERVED.BINOP.SUB | |
}, | |
{ | |
RESERVED.BINOP.CAT | |
}, | |
{ | |
RESERVED.COMP.LT, | |
RESERVED.COMP.GT, | |
RESERVED.COMP.LE, | |
RESERVED.COMP.GE, | |
RESERVED.COMP.NE, | |
RESERVED.COMP.EQ | |
}, | |
{ | |
RESERVED.BINOP.AND | |
}, | |
{ | |
RESERVED.BINOP.OR | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isSpace(c) | |
return c <= " " | |
end | |
function isDigit(c) | |
return c >= "0" and c <= "9" | |
end | |
function isAlpha(c) | |
return c >= "a" and c <= "z" | |
or c >= "A" and c <= "Z" | |
end | |
function isAlNum(c) | |
return c >= "0" and c <= "9" | |
or c >= "a" and c <= "z" | |
or c >= "A" and c <= "Z" | |
or c == "_" | |
end | |
function lexer(src) | |
local tokens = {} | |
local i = 0 | |
local n = #src | |
local raw | |
while i <= n do | |
raw = "" | |
while i <= n and isSpace(src[i]) do | |
i = i + 1 | |
end | |
if i <= n then | |
if isAlpha(src[i]) then | |
while i <= n and isAlpha(src[i]) do | |
raw = raw .. src[i] | |
i = i + 1 | |
end | |
if raw == "do" then | |
elseif raw == "end" then | |
end | |
elseif src[i] == "'" or src[i] == '"' then | |
local delim = src[i] | |
local prepre = "" | |
local pre = "" | |
i = i + 1 | |
while i <= n and (src[i] ~= delim or prepre ~= "\\" and pre == "\\") do | |
raw = raw .. src[i] | |
prepre = pre | |
pre = src[i] | |
i = i + 1 | |
end | |
elseif src[i] == "<" or src[i] == ">" or src[i] == "~" or src[i] == "=" then | |
raw = src[i] | |
i = i + 1 | |
if i <= n and src[i] == "=" then | |
raw = raw .. "=" | |
i = i + 1 | |
end | |
tokens[#tokens+1] = {raw, ""} | |
end | |
end | |
end | |
return tokens | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ROOT = 0 | |
local INTR = 1 | |
local LEAF = 2 | |
function parser(tokens) | |
-- linked representation | |
local tree = {} | |
local roots = {} | |
local level = 0 | |
local left | |
local data | |
local right | |
for tk in tokens do | |
if tk.cat == LEAF then | |
left = nil | |
right = nil | |
elseif tk.cat == INTR then | |
end | |
end | |
return tree, roots | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment