Script.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package me.hammerle.snuviscript.code;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Stack;
  5. import me.hammerle.snuviscript.variable.Variable;
  6. public class Script
  7. {
  8. protected int currentLine;
  9. protected Instruction[] code;
  10. protected final HashMap<String, Integer> labels;
  11. protected final Stack<Integer> returnStack;
  12. protected final Stack<HashMap<String, Variable>> localVars;
  13. protected Object returnValue;
  14. protected final boolean subScript;
  15. protected final String[] subScriptInput;
  16. protected final HashMap<String, Script> subScripts;
  17. public Script(List<String> code)
  18. {
  19. this.subScriptInput = null;
  20. this.subScripts = new HashMap<>();
  21. this.labels = new HashMap<>();
  22. this.returnStack = new Stack<>();
  23. this.localVars = new Stack<>();
  24. this.subScript = false;
  25. this.currentLine = 0;
  26. this.code = Compiler.compile(this, code, labels, subScript, 0);
  27. /*System.out.println("__________________________________");
  28. subScripts.forEach((k, v) ->
  29. {
  30. System.out.println(k);
  31. for(Instruction in : v.code)
  32. {
  33. System.out.println(in);
  34. }
  35. System.out.println("__________________________________");
  36. });*/
  37. }
  38. public Script(List<String> code, String[] subScriptInput, Script sc, int lineOffset)
  39. {
  40. this.subScriptInput = subScriptInput;
  41. this.subScripts = sc.subScripts;
  42. this.labels = new HashMap<>();
  43. this.returnStack = new Stack<>();
  44. this.localVars = sc.localVars;
  45. this.subScript = true;
  46. this.code = Compiler.compile(this, code, labels, subScript, lineOffset);
  47. this.currentLine = 0;
  48. }
  49. public HashMap<String, Variable> getLocalVars()
  50. {
  51. return localVars.peek();
  52. }
  53. public Object run()
  54. {
  55. int length = code.length;
  56. returnValue = null;
  57. while(currentLine < length)
  58. {
  59. //System.out.println("EXECUTE: " + code[currentLine]);
  60. code[currentLine].execute(this);
  61. currentLine++;
  62. }
  63. return returnValue;
  64. }
  65. public void end()
  66. {
  67. currentLine = code.length;
  68. }
  69. }