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.

86 lines
2.2 KiB

  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. __interp.prompt = __interp.ps2
  18. else
  19. __interp.prompt = __interp.ps1
  20. end
  21. __interp.line = readline.readline(__interp.prompt)
  22. if #__interp.line then readline.add_history(__interp.line) end
  23. --prepr(__interp.line)
  24. --prepr(__interp)
  25. if (__interp.line:sub(-4, None)=="then") then
  26. __interp.buffer+=__interp.line+" "
  27. __interp.stmt_stack+=1
  28. else
  29. if (__interp.line:sub(-2, None)=="do") then
  30. __interp.buffer+=__interp.line+" "
  31. __interp.stmt_stack-=1
  32. else
  33. if __interp.line:sub(-1, None)=="\" then
  34. __interp.buffer+=__interp.line:sub(0, -1)+" "
  35. else
  36. __interp.buffer+=__interp.line+" "
  37. if __interp.line:sub(-3, None)=="end" then
  38. __interp.stmt_stack-=1
  39. end
  40. if __interp.stmt_stack<=0 then
  41. __interp.stmt_stack=0
  42. __interp.program = try(parse, __interp.buffer)
  43. if !__interp.program[0] then
  44. print('Syntax error')
  45. else
  46. if !(try(func() __interp.program[1].stmtlist[0].type end)[0]) then
  47. print('NULL program error')
  48. else
  49. if __interp.program[1].stmtlist[0].type == ast.ST_EXPR then
  50. __interp.program[1] = __interp.program[1].stmtlist[0].expr
  51. __interp.isexpr = 1
  52. else
  53. __interp.isexpr = 0
  54. end
  55. __interp.result = try(__interp.program[1])
  56. if !__interp.result[0] then
  57. print(__interp.result[1])
  58. print(__interp.result[2])
  59. for ent in __interp.result[2] do
  60. st = ent[0]
  61. scope = ent[1]
  62. if st.type == ast.ST_LIST then continue end
  63. print('In', st, 'at', st.loc.line, ',', st.loc.col, ':')
  64. ast.print(st)
  65. print(scope)
  66. print('---')
  67. end
  68. else
  69. if __interp.isexpr then
  70. prepr(__interp.result[1])
  71. _ = __interp.result[1]
  72. end
  73. end
  74. end
  75. end
  76. __interp.buffer=''
  77. end
  78. end
  79. end
  80. end
  81. end