CodeFunction.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package me.km.snuviscript;
  2. import me.km.KajetansMod;
  3. import me.km.exception.PrescriptException;
  4. import me.km.exception.NoSuchMethodException;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.stream.Collectors;
  8. public class CodeFunction
  9. {
  10. private int function;
  11. private Object[] pars;
  12. public CodeFunction(String code)
  13. {
  14. function = 0;
  15. findFunction(code);
  16. }
  17. private void findFunction(String c)
  18. {
  19. //System.out.println(c);
  20. if(c.equals("")) // Springt in { } ohne Kopf
  21. {
  22. function = KajetansMod.scripts.getQuestParser().translateMethod("godeeper");
  23. pars = new Object[0];
  24. return;
  25. }
  26. int pos = c.indexOf("(");
  27. if(pos == -1)
  28. {
  29. throw new PrescriptException("Falsche Syntax bei: " + c);
  30. }
  31. try
  32. {
  33. function = KajetansMod.scripts.getQuestParser().translateMethod(c.substring(0, pos).trim().toLowerCase());
  34. }
  35. catch(NullPointerException ex)
  36. {
  37. throw new NoSuchMethodException(c.substring(0, pos).trim());
  38. }
  39. pos++;
  40. ArrayList<Object> pars = new ArrayList<>();
  41. int last = pos;
  42. int counter = 1;
  43. boolean string = false;
  44. while(pos < c.length())
  45. {
  46. if(string)
  47. {
  48. if(c.charAt(pos) == '"')
  49. {
  50. string = !string;
  51. }
  52. pos++;
  53. continue;
  54. }
  55. switch(c.charAt(pos))
  56. {
  57. case '"':
  58. string = !string;
  59. break;
  60. case ',':
  61. if(last != pos)
  62. {
  63. pars.add(ScriptUtils.convertInput(c.substring(last, pos).trim()));
  64. }
  65. last = pos + 1;
  66. break;
  67. case ')':
  68. if(counter <= 0)
  69. {
  70. throw new PrescriptException(") ohne (");
  71. }
  72. if(last != pos)
  73. {
  74. pars.add(ScriptUtils.convertInput(c.substring(last, pos).trim()));
  75. }
  76. break;
  77. case '(':
  78. counter++;
  79. pos++;
  80. while(counter != 1 && pos < c.length())
  81. {
  82. switch(c.charAt(pos))
  83. {
  84. case '(':
  85. counter++;
  86. break;
  87. case ')':
  88. counter--;
  89. break;
  90. }
  91. pos++;
  92. }
  93. pars.add(new CodeFunction(c.substring(last, pos).trim()));
  94. while(pos < c.length() && c.charAt(pos) != ',' && c.charAt(pos) != ')')
  95. {
  96. pos++;
  97. }
  98. last = pos + 1;
  99. break;
  100. }
  101. pos++;
  102. }
  103. this.pars = pars.toArray(new Object[pars.size()]);
  104. }
  105. public int getFunction()
  106. {
  107. return function;
  108. }
  109. public Object[] getParameters()
  110. {
  111. return pars;
  112. }
  113. @Override
  114. public String toString()
  115. {
  116. return function + "(" + Arrays.asList(pars).stream().map(o -> String.valueOf(o)).collect(Collectors.joining(", ")) + ")";
  117. }
  118. }