123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package me.km.snuviscript;
- import me.km.KajetansMod;
- import me.km.exception.PrescriptException;
- import me.km.exception.NoSuchMethodException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.stream.Collectors;
- public class CodeFunction
- {
- private int function;
- private Object[] pars;
-
- public CodeFunction(String code)
- {
- function = 0;
- findFunction(code);
- }
-
- private void findFunction(String c)
- {
- //System.out.println(c);
- if(c.equals("")) // Springt in { } ohne Kopf
- {
- function = KajetansMod.scripts.getQuestParser().translateMethod("godeeper");
- pars = new Object[0];
- return;
- }
- int pos = c.indexOf("(");
- if(pos == -1)
- {
- throw new PrescriptException("Falsche Syntax bei: " + c);
- }
- try
- {
- function = KajetansMod.scripts.getQuestParser().translateMethod(c.substring(0, pos).trim().toLowerCase());
- }
- catch(NullPointerException ex)
- {
- throw new NoSuchMethodException(c.substring(0, pos).trim());
- }
- pos++;
-
- ArrayList<Object> pars = new ArrayList<>();
- int last = pos;
- int counter = 1;
- boolean string = false;
- while(pos < c.length())
- {
- if(string)
- {
- if(c.charAt(pos) == '"')
- {
- string = !string;
- }
- pos++;
- continue;
- }
- switch(c.charAt(pos))
- {
- case '"':
- string = !string;
- break;
- case ',':
- if(last != pos)
- {
- pars.add(ScriptUtils.convertInput(c.substring(last, pos).trim()));
- }
- last = pos + 1;
- break;
- case ')':
- if(counter <= 0)
- {
- throw new PrescriptException(") ohne (");
- }
- if(last != pos)
- {
- pars.add(ScriptUtils.convertInput(c.substring(last, pos).trim()));
- }
- break;
- case '(':
- counter++;
- pos++;
- while(counter != 1 && pos < c.length())
- {
- switch(c.charAt(pos))
- {
- case '(':
- counter++;
- break;
- case ')':
- counter--;
- break;
- }
- pos++;
- }
- pars.add(new CodeFunction(c.substring(last, pos).trim()));
- while(pos < c.length() && c.charAt(pos) != ',' && c.charAt(pos) != ')')
- {
- pos++;
- }
- last = pos + 1;
- break;
- }
- pos++;
- }
- this.pars = pars.toArray(new Object[pars.size()]);
- }
-
- public int getFunction()
- {
- return function;
- }
-
- public Object[] getParameters()
- {
- return pars;
- }
- @Override
- public String toString()
- {
- return function + "(" + Arrays.asList(pars).stream().map(o -> String.valueOf(o)).collect(Collectors.joining(", ")) + ")";
- }
- }
|