Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string:split(delimiter)
- local result = { }
- local from = 1
- local delim_from, delim_to = string.find( self, delimiter, from )
- while delim_from do
- table.insert( result, string.sub( self, from , delim_from-1 ) )
- from = delim_to + 1
- delim_from, delim_to = string.find( self, delimiter, from )
- end
- table.insert( result, string.sub( self, from ) )
- return result
- end
- function table.HasValue( t, val )
- for k,v in pairs(t) do
- if (v == val) then return true end
- end
- return false
- end
- function table.print(t, indent)
- local indentstr = '';
- for i=1,indent do indentstr = indentstr .. '\t' end
- for k,v in pairs(t) do
- if (type(v) == 'table') then
- table.print(v, indent + 1)
- else
- print(indentstr .. k .. " = " .. v)
- end
- end
- end
- print("PRE Interpreter")
- io.stdout:write("Script: ")
- filename = io.stdin:read()
- f = io.open(filename, 'r')
- filedata = f:read('*a')
- fileline = string.split(filedata, '\n')
- f:close()
- --Remove comments
- validComments = {'//', '--'}
- local tempfilelines = {}
- local tempfilelinesIdx = 1
- for idx,line in pairs(fileline) do
- local first2chars = string.sub(line, 1, 2)
- if not table.HasValue(validComments, first2chars) then
- tempfilelines[tempfilelinesIdx] = line
- tempfilelinesIdx = tempfilelinesIdx + 1
- end
- end
- fileline = tempfilelines
- --Interpret
- str2bytecode = {}
- str2bytecode['#define'] = 01
- str2bytecode['#push'] = 02
- str2bytecode['#pop'] = 03
- str2bytecode['#call'] = 04
- str2bytecode['#label'] = 05
- str2bytecode['#goto'] = 06
- str2bytecode['#if'] = 11
- str2bytecode['#else'] = 12
- str2bytecode['#endif'] = 13
- str2bytecode['#while'] = 21
- str2bytecode['#endwhile'] = 22
- str2bytecode['#for'] = 23
- str2bytecode['#endfor'] = 24
- ---Create variables
- script_vars = {}
- for idx,line in pairs(fileline) do
- local splitline = string.split(line, ' ')
- if (splitline[1] == '#define') then
- script_vars_tempval = splitline[3]
- if (string.sub(splitline[3], 1, 1) == '[' and string.sub(splitline[3], -1) == ']') then
- local func = loadstring("script_vars_tempval = " .. string.sub(splitline[3], 2, -2))
- func()
- end
- script_vars[splitline[2]] = script_vars_tempval
- end
- end
- table.print(script_vars, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement