Advertisement
captmicro

Untitled

Jun 27th, 2010
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.20 KB | None | 0 0
  1. function string:split(delimiter)
  2.     local result = { }
  3.     local from = 1
  4.     local delim_from, delim_to = string.find( self, delimiter, from  )
  5.     while delim_from do
  6.         table.insert( result, string.sub( self, from , delim_from-1 ) )
  7.         from = delim_to + 1
  8.         delim_from, delim_to = string.find( self, delimiter, from  )
  9.     end
  10.     table.insert( result, string.sub( self, from  ) )
  11.     return result
  12. end
  13. function table.HasValue( t, val )
  14.     for k,v in pairs(t) do
  15.         if (v == val) then return true end
  16.     end
  17.     return false
  18. end
  19. function table.print(t, indent)
  20.     local indentstr = '';
  21.     for i=1,indent do indentstr = indentstr .. '\t' end
  22.     for k,v in pairs(t) do
  23.         if (type(v) == 'table') then
  24.             table.print(v, indent + 1)
  25.         else
  26.             print(indentstr .. k .. " = " .. v)
  27.         end
  28.     end
  29. end
  30.  
  31. print("PRE Interpreter")
  32. io.stdout:write("Script: ")
  33. filename = io.stdin:read()
  34.  
  35. f = io.open(filename, 'r')
  36. filedata = f:read('*a')
  37. fileline = string.split(filedata, '\n')
  38. f:close()
  39.  
  40. --Remove comments
  41. validComments = {'//', '--'}
  42. local tempfilelines = {}
  43. local tempfilelinesIdx = 1
  44. for idx,line in pairs(fileline) do
  45.     local first2chars = string.sub(line, 1, 2)
  46.     if not table.HasValue(validComments, first2chars) then
  47.         tempfilelines[tempfilelinesIdx] = line
  48.         tempfilelinesIdx = tempfilelinesIdx + 1
  49.     end
  50. end
  51. fileline = tempfilelines
  52.  
  53. --Interpret
  54. str2bytecode = {}
  55. str2bytecode['#define']     = 01
  56. str2bytecode['#push']       = 02
  57. str2bytecode['#pop']        = 03
  58. str2bytecode['#call']       = 04
  59. str2bytecode['#label']      = 05
  60. str2bytecode['#goto']       = 06
  61.  
  62. str2bytecode['#if']         = 11
  63. str2bytecode['#else']       = 12
  64. str2bytecode['#endif']      = 13
  65.  
  66. str2bytecode['#while']      = 21
  67. str2bytecode['#endwhile']   = 22
  68. str2bytecode['#for']        = 23
  69. str2bytecode['#endfor']     = 24
  70.  
  71. ---Create variables
  72. script_vars = {}
  73. for idx,line in pairs(fileline) do
  74.     local splitline = string.split(line, ' ')
  75.     if (splitline[1] == '#define') then
  76.         script_vars_tempval = splitline[3]
  77.         if (string.sub(splitline[3], 1, 1) == '[' and string.sub(splitline[3], -1) == ']') then
  78.             local func = loadstring("script_vars_tempval = " .. string.sub(splitline[3], 2, -2))
  79.             func()
  80.         end
  81.         script_vars[splitline[2]] = script_vars_tempval
  82.     end
  83. end
  84.  
  85. table.print(script_vars, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement