Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local file = tArgs[1]
- local code = ""
- if file and fs.exists(file) then
- local ex = fs.open(tArgs[1],"r")
- code = ex.readAll()
- else
- printError("gimme a valid file fool")
- return
- end
- --local tEnv = {running = false}
- local lineInt = 0
- function throw(er)
- printError("Error at line "..lineInt..": "..er)
- error()
- end
- local ipairs = ipairs
- local vars = {}
- function preprocess(sStr)
- local val = sStr:match("%%%<%s?%*%&%s?(%S+)%s?%/%&")
- if val then
- if vars[val] then
- return vars[val]
- else
- throw("Attempt to use non-initialized variable ("..val..")")
- end
- else
- return sStr
- end
- end
- local funchandles = {
- {
- key = "v%>",
- print = {
- key = "%>%_",
- func = function(arg)
- print(arg)
- end,
- }
- },
- }
- local lexicon = {
- {
- key = "%%%>%s?.*%s?%d%s?%*%&.*%/%&%s?%%%?",
- def = function(str)
- -- %>*%0*&Oh Hi There!!!/&%?
- local varType = str:match("%%%>%s?([^%s]*)%s?%d")
- local varName = str:match("%%%>%s?.*%s?%d+")
- local varNP = varName:find("%d")
- varName = varName:sub(varNP)
- local varContents = str:match("%*%&(.*)%/%&%s?%%%?")
- if varType == "!%" then
- vars[varName] = tonumber(varContents)
- elseif varType == "*%" then
- vars[varName] = varContents
- --print("Store "..varContents.." into "..varName)
- else
- throw("Invalid variable type! Got '"..varType.."'")
- end
- end,
- },
- {
- key = "%^%>.*%^%?",
- def = function(str)
- local func = str:match("%^%>%s?(.*)%s?%^%?")
- for k,v in ipairs(funchandles) do
- local s,e = func:find("^%s*("..v.key..")")
- if s and e then
- local agp = func:sub(e+1)
- for kk,vv in pairs(v) do
- if type(vv)=="table" then
- if agp:match("^%s*("..vv.key..")%s?%*%&") then
- local args = agp:match("^%s*"..vv.key.."%s?%*%&(.+)%/%&")
- args = args:match("%s*(.*)%s*")
- local tArg = {}
- local ss,ee = args:find("%%%.")
- if ss and ee then
- local le = 1
- while ss and ee do
- local n = args:sub(le,ss-1)
- n = n:match("%s*(.*)%s*")
- le = ee+1
- table.insert(tArg,preprocess(n))
- ss,ee = args:find("%%%.",ee)
- end
- table.insert(tArg,preprocess(args:sub(le)))
- vv.func(unpack(tArg))
- else
- vv.func(preprocess(args))
- end
- break
- end
- end
- end
- break
- end
- end
- end,
- },
- }
- local lines = {}
- for line in io.lines(tArgs[1]) do
- lines[#lines+1] = line
- end
- local opened = false
- for k,v in ipairs(lines) do
- lineInt = k
- if not opened then
- if v:match("%&%^%>%*%&") then
- opened = true
- end
- else
- --print("proccess line "..k)
- for kk,vv in ipairs(lexicon) do
- local mtc = v:match(vv.key)
- if mtc then
- vv.def(mtc)
- break
- end
- end
- if v:match("^%s*%/%&") then
- break
- end
- end
- end
- if not opened then
- printError("Program was never opened!")
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement