The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interp.sol 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. -- The Solterpreter! A simple command-line interface for the compiler.
  2. print('Solterpreter/Viperpreter v0.1')
  3. print('(Runtime version ', debug.version, ')')
  4. __interp = {
  5. running = 1,
  6. buffer = '',
  7. ps1 = '>>> ',
  8. ps2 = '... ',
  9. stmt_stack=0,
  10. }
  11. func exit()
  12. __interp.running=0
  13. end
  14. quit = exit
  15. while __interp.running do
  16. if #__interp.buffer then
  17. io.stdout << __interp.ps2
  18. else
  19. io.stdout << __interp.ps1
  20. end
  21. __interp.line = io.stdin:read(io.LINE)
  22. __interp.line = __interp.line:sub(0, -1)
  23. --prepr(__interp.line)
  24. if (__interp.line:sub(-4, None)=="then") then
  25. __interp.buffer+=__interp.line+" "
  26. __interp.stmt_stack+=1
  27. else
  28. if (__interp.line:sub(-2, None)=="do") then
  29. __interp.buffer+=__interp.line+" "
  30. __interp.stmt_stack-=1
  31. else
  32. if __interp.line:sub(-1, None)=="\" then
  33. __interp.buffer+=__interp.line:sub(0, -1)+" "
  34. else
  35. __interp.buffer+=__interp.line+" "
  36. if __interp.line:sub(-3, None)=="end" then
  37. __interp.stmt_stack-=1
  38. end
  39. if __interp.stmt_stack<=0 then
  40. __interp.stmt_stack=0
  41. __interp.program = try(parse, __interp.buffer)
  42. if !__interp.program[0] then
  43. print('Syntax error')
  44. else
  45. if __interp.program[1].stmtlist[0].type == ast.ST_EXPR then
  46. __interp.program[1] = __interp.program[1].stmtlist[0].expr
  47. __interp.isexpr = 1
  48. else
  49. __interp.isexpr = 0
  50. end
  51. __interp.result = try(__interp.program[1])
  52. if !__interp.result[0] then
  53. print(__interp.result[1])
  54. print(__interp.result[2])
  55. for ent in __interp.result[2] do
  56. st = ent[0]
  57. scope = ent[1]
  58. if st.type == ast.ST_LIST then continue end
  59. print('In', st, 'at', st.loc.line, ',', st.loc.col, ':')
  60. ast.print(st)
  61. print(scope)
  62. print('---')
  63. end
  64. else
  65. if __interp.isexpr then
  66. prepr(__interp.result[1])
  67. end
  68. end
  69. end
  70. __interp.buffer=''
  71. end
  72. end
  73. end
  74. end
  75. end