Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- if #tArgs<1 then
- printError("Usage: sammc <prgm>")
- return
- end
- if not fs.exists(tArgs[1]) or fs.isDir(tArgs[1]) then
- printError("Usage: sammc <prgm>")
- return
- end
- local funcs = {
- ["print"] = {
- 1,
- {
- "print(",1,
- },
- ")",
- },
- ["store"] = {
- 2,
- {
- "",1,
- " = ",2,
- },
- "",
- },
- ["concat"] = {
- 2,
- {
- "",1,
- " = ",1,
- "..",2
- },
- "",
- },
- ["read"] = {
- 1,
- {
- "",1,
- " = read()"
- },
- "",
- }
- }
- local inFile = fs.open(tArgs[1],"r")
- function trim(s)
- -- print(s)
- return s:match "^%s*(.-)%s*$",s:find("[^ ]")
- end
- local lineCount = 0
- function parse(line)
- line = trim(line)
- for k,v in pairs(funcs) do
- if k==line then
- local fArgs = {}
- for i=1,v[1] do
- nxtLine = inFile.readLine()
- if not nxtLine then
- printError("Unexpected EOF:"..lineCount)
- error()
- end
- lineCount = lineCount+1
- local ln = trim(nxtLine)
- table.insert(fArgs,ln)
- end
- local endLine = ""
- local kk = 1
- repeat
- endLine = endLine..(v[2][kk] or "")
- endLine = endLine..(fArgs[v[2][kk+1]] or "")
- kk = kk+2
- until kk>#v[2]
- endLine = endLine..v[3]
- return endLine
- end
- end
- end
- local outStream = fs.open(tArgs[1]..".lua","w")
- while true do
- line = inFile.readLine()
- lineCount = lineCount+1
- if not line then break end
- token = parse(line)
- print(token)
- outStream.write(token.."\n")
- end
- outStream.close()
- inFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement