diff --git a/compiler2.lua b/compiler2.lua index 900874f..f12def3 100644 --- a/compiler2.lua +++ b/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",line} + noblock = false + -- We gonna define header stuff + elseif noblock and line:lower():match("enable%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif noblock and line:lower():match("disable%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif noblock and line:lower():match("loadfile%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif noblock and line:lower():match("entry%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif noblock and line:lower():match("using%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif noblock and line:lower():match("version%s(.+)") then + groupStack:append{group,line_num,"",line} + elseif line:match("choice%s+\".+\"") then + groupStack:append{group,line_num,"",line} choice = true elseif line:match("::([_:,%w%(%)]+)::") then - print(line_num,"LABEL_BLOCK",line) + groupStack:append{group,line_num,"",line} + elseif line:match("for%s*[_%w]-%s") then + groupStack:append{group,line_num,"",line} + elseif line:match("%s*while%s*.+") then + groupStack:append{group,line_num,"",line} elseif choice then choice = false if line:match("\".*\"%s*[_:,%w%(%)]+%(.*%)") then - print(line_num,"CHOICE_OPTION",line) + groupStack:append{group,line_num,"",line} choice = true else goto back end + elseif line:match("[%s,_%w]*=.-%(.+%)") then + groupStack:append{group,line_num,"",line} + elseif line:match(".-%(.+%)") then + groupStack:append{group,line_num,"",line} + elseif line:match("[%s,_%w]*=(.+)") then + groupStack:append{group,line_num,"",line} + elseif line:match("\"(.+)\"") then + groupStack:append{group,line_num,"",line} else - print(line_num,line) + groupStack:append{group,line_num,"",line} end ::continue:: end \ No newline at end of file diff --git a/test.dms b/test.dms index 677b02e..9ebd695 100644 --- a/test.dms +++ b/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()]