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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. else
  55. if __interp.isexpr then
  56. prepr(__interp.result[1])
  57. end
  58. end
  59. end
  60. __interp.buffer=''
  61. end
  62. end
  63. end
  64. end
  65. end