Working on syntax and basic parsing
This commit is contained in:
parent
fcbe96b91e
commit
bee1d90bf1
110
compiler2.lua
110
compiler2.lua
@ -13,33 +13,125 @@ end
|
||||
function CMD:process()
|
||||
|
||||
end
|
||||
Stack = {}
|
||||
Stack.__index = Stack
|
||||
function Stack.__tostring(self)
|
||||
return table.concat(self.stack, ", ")
|
||||
end
|
||||
function Stack:new(n)
|
||||
local c = {}
|
||||
setmetatable(c,self)
|
||||
c.max = n or math.huge
|
||||
c.stack = {}
|
||||
return c
|
||||
end
|
||||
function Stack:push(n)
|
||||
table.insert(self.stack,n)
|
||||
end
|
||||
function Stack:pop()
|
||||
return table.remove(self.stack,#self.stack)
|
||||
end
|
||||
function Stack:peek(n)
|
||||
return self.stack[#self.stack - (n or 0)]
|
||||
end
|
||||
function Stack:count()
|
||||
return #self.stack
|
||||
end
|
||||
|
||||
function string:trim()
|
||||
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
||||
end
|
||||
function string.tabs(str)
|
||||
local c = 0
|
||||
for i in str:gmatch(".") do
|
||||
if i=="\t" then
|
||||
c=c+1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
return c
|
||||
end
|
||||
local choice
|
||||
local group = 0
|
||||
local lastgroup = 0
|
||||
local groupStack = Stack:new({})
|
||||
local lastpop
|
||||
groupStack:push({})
|
||||
function groupStack:append(t)
|
||||
local c = self:peek()
|
||||
val = pcall(function()
|
||||
table.insert(c,t)
|
||||
end)
|
||||
if not val then
|
||||
error("Inconsistant indentation!")
|
||||
end
|
||||
end
|
||||
local noblock = true
|
||||
for line in content:gmatch("(.-)\n") do
|
||||
line_num = line_num + 1
|
||||
line = line:trim()
|
||||
line = line:gsub("")
|
||||
--line = line:trim()
|
||||
--line = line:gsub("")
|
||||
lastgroup = group
|
||||
group = line:tabs()
|
||||
if lastgroup>group then
|
||||
for i=1,lastgroup-group do
|
||||
local c = groupStack:pop()
|
||||
lastpop = c
|
||||
for i,v in pairs(c) do
|
||||
print(table.concat(v,", "))
|
||||
end
|
||||
end
|
||||
elseif lastgroup<group then
|
||||
groupStack:push({})
|
||||
end
|
||||
if line=="" then goto continue end
|
||||
::back::
|
||||
if line:match("^%[[_:,%w%(%)]+%]") then
|
||||
print(line_num,"BLOCK_START",line)
|
||||
elseif line:match("choice%s+\".+\"%s*:") then
|
||||
print(line_num,"CHOICE_BLOCK",line)
|
||||
if line:trim()=="" then
|
||||
goto continue
|
||||
elseif line:match("^%[[_:,%w%(%)]+%]") then
|
||||
groupStack:append{group,line_num,"<BLOCK_START>",line}
|
||||
noblock = false
|
||||
-- We gonna define header stuff
|
||||
elseif noblock and line:lower():match("enable%s(.+)") then
|
||||
groupStack:append{group,line_num,"<ENABLE>",line}
|
||||
elseif noblock and line:lower():match("disable%s(.+)") then
|
||||
groupStack:append{group,line_num,"<DISABLE>",line}
|
||||
elseif noblock and line:lower():match("loadfile%s(.+)") then
|
||||
groupStack:append{group,line_num,"<LOADFILE>",line}
|
||||
elseif noblock and line:lower():match("entry%s(.+)") then
|
||||
groupStack:append{group,line_num,"<ENTRY>",line}
|
||||
elseif noblock and line:lower():match("using%s(.+)") then
|
||||
groupStack:append{group,line_num,"<USING>",line}
|
||||
elseif noblock and line:lower():match("version%s(.+)") then
|
||||
groupStack:append{group,line_num,"<VERSION>",line}
|
||||
elseif line:match("choice%s+\".+\"") then
|
||||
groupStack:append{group,line_num,"<CHOICE_BLOCK>",line}
|
||||
choice = true
|
||||
elseif line:match("::([_:,%w%(%)]+)::") then
|
||||
print(line_num,"LABEL_BLOCK",line)
|
||||
groupStack:append{group,line_num,"<LABEL_BLOCK>",line}
|
||||
elseif line:match("for%s*[_%w]-%s") then
|
||||
groupStack:append{group,line_num,"<FOR_BLOCK>",line}
|
||||
elseif line:match("%s*while%s*.+") then
|
||||
groupStack:append{group,line_num,"<WHILE_BLOCK>",line}
|
||||
elseif choice then
|
||||
choice = false
|
||||
if line:match("\".*\"%s*[_:,%w%(%)]+%(.*%)") then
|
||||
print(line_num,"CHOICE_OPTION",line)
|
||||
groupStack:append{group,line_num,"<CHOICE_OPTION>",line}
|
||||
choice = true
|
||||
else
|
||||
goto back
|
||||
end
|
||||
elseif line:match("[%s,_%w]*=.-%(.+%)") then
|
||||
groupStack:append{group,line_num,"<FUNCWR>",line}
|
||||
elseif line:match(".-%(.+%)") then
|
||||
groupStack:append{group,line_num,"<FUNCNR>",line}
|
||||
elseif line:match("[%s,_%w]*=(.+)") then
|
||||
groupStack:append{group,line_num,"<ASSIGNMENT>",line}
|
||||
elseif line:match("\"(.+)\"") then
|
||||
groupStack:append{group,line_num,"<DISP_MSG>",line}
|
||||
else
|
||||
print(line_num,line)
|
||||
groupStack:append{group,line_num,"<UNKNOWN_BLOCK>",line}
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
29
test.dms
29
test.dms
@ -1,28 +1,38 @@
|
||||
ENTRY main
|
||||
ENABLE
|
||||
DISABLE
|
||||
LOADFILE
|
||||
ENABLE test
|
||||
DISABLE test
|
||||
LOADFILE test.dat
|
||||
VERSION 1.4.5
|
||||
USING extendedDefine
|
||||
|
||||
|
||||
[main]
|
||||
"This works!"
|
||||
"What's up"
|
||||
|
||||
|
||||
Ryan "Hello how are you doing?"
|
||||
Bob "I'm good you?"
|
||||
|
||||
|
||||
list = {1,2,3,true,false, {1,2,3}}
|
||||
a = list[1]
|
||||
list[1] = "Hello"
|
||||
|
||||
::label::
|
||||
|
||||
choice "Pick one:"
|
||||
|
||||
choice "Pick one:"
|
||||
"first" func()
|
||||
"second" func()
|
||||
"third" func()
|
||||
|
||||
for x = 1,10
|
||||
...
|
||||
group1A
|
||||
group1B
|
||||
for y = 1,10
|
||||
group2A
|
||||
group2B
|
||||
for z = 1,10
|
||||
group3A
|
||||
group3B
|
||||
|
||||
while cond
|
||||
...
|
||||
@ -34,6 +44,7 @@ LOADFILE
|
||||
else
|
||||
...
|
||||
|
||||
var1,var2 = func(1,"string", 2+5)
|
||||
var1,var2 = func(1,"string", 2+5)
|
||||
func(1,"string", 2+5)
|
||||
|
||||
[newblock:function()]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user