|
@@ -58,7 +58,6 @@ public class LineCompiler
|
|
{
|
|
{
|
|
if(function)
|
|
if(function)
|
|
{
|
|
{
|
|
- //System.out.println("push last result");
|
|
|
|
if(index + 1 != lastSpecial.peek())
|
|
if(index + 1 != lastSpecial.peek())
|
|
{
|
|
{
|
|
co.add(new Code(Code.convertInput(strings, line.substring(index + 1, lastSpecial.pop()), true), realLine, layer));
|
|
co.add(new Code(Code.convertInput(strings, line.substring(index + 1, lastSpecial.pop()), true), realLine, layer));
|
|
@@ -92,146 +91,352 @@ public class LineCompiler
|
|
|
|
|
|
public void compile(ArrayList<Code> co, HashMap<String, String> strings)
|
|
public void compile(ArrayList<Code> co, HashMap<String, String> strings)
|
|
{
|
|
{
|
|
- int old;
|
|
|
|
- int j = line.length() - 1;
|
|
|
|
- int commas;
|
|
|
|
- String syntax;
|
|
|
|
- while(j >= 0)
|
|
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- syntax = null;
|
|
|
|
- switch(line.charAt(j))
|
|
|
|
|
|
+ int old;
|
|
|
|
+ int j = line.length() - 1;
|
|
|
|
+ boolean arrayState = true;
|
|
|
|
+ String arrayFunction = null;
|
|
|
|
+ int commas;
|
|
|
|
+ String syntax;
|
|
|
|
+ while(j >= 0)
|
|
{
|
|
{
|
|
- case '+': syntax = "add"; break;
|
|
|
|
- case '-': syntax = "sub"; break;
|
|
|
|
- case '*': syntax = "mul"; break;
|
|
|
|
- case '/': syntax = "div"; break;
|
|
|
|
- case '%': syntax = "math.mod"; break;
|
|
|
|
- case '^': syntax = "math.pow"; break;
|
|
|
|
- case '<': syntax = "less"; break;
|
|
|
|
- case '>': syntax = "greater"; break;
|
|
|
|
- /*
|
|
|
|
- changeBiSyntax("||", "or", sb, false);
|
|
|
|
- changeBiSyntax("&&", "and", sb, false);
|
|
|
|
- */
|
|
|
|
- case '=':
|
|
|
|
|
|
+ syntax = null;
|
|
|
|
+ switch(line.charAt(j))
|
|
{
|
|
{
|
|
- char before = line.charAt(j - 1);
|
|
|
|
- switch(before)
|
|
|
|
- {
|
|
|
|
- case '=': syntax = "equal"; break;
|
|
|
|
- case '!': syntax = "notequal"; break;
|
|
|
|
- case '>': syntax = "greaterequal"; break;
|
|
|
|
- case '<': syntax = "lessequal"; break;
|
|
|
|
- }
|
|
|
|
- if(syntax != null)
|
|
|
|
|
|
+ case '+':
|
|
|
|
+ if(line.charAt(j - 1) == '+')
|
|
|
|
+ {
|
|
|
|
+ int last = lastSpecial.pop();
|
|
|
|
+ if(last - j >= 2)
|
|
|
|
+ {
|
|
|
|
+ // ++i
|
|
|
|
+ if(j + 1 - last == 0)
|
|
|
|
+ {
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(line.substring(j + 1, last), realLine, layer));
|
|
|
|
+ co.add(new Code("inc", 1, realLine, layer));
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Code c = co.get(co.size() - 1);
|
|
|
|
+ if("array.get".equals(c.function))
|
|
|
|
+ {
|
|
|
|
+ co.set(co.size() - 1, new Code("array.inc", 2, c.realLine, c.layer));
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ function = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ // i++
|
|
|
|
+ j--;
|
|
|
|
+ old = j;
|
|
|
|
+ j--;
|
|
|
|
+ if(line.charAt(j) == ']')
|
|
|
|
+ {
|
|
|
|
+ arrayFunction = "postinc";
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ while(j >= 0 && isAllowedChar(line.charAt(j)))
|
|
|
|
+ {
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ j++;
|
|
|
|
+ if(j - old == 0)
|
|
|
|
+ {
|
|
|
|
+ co.forEach(s -> System.out.println(s));
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(line.substring(j, old), realLine, layer));
|
|
|
|
+ co.add(new Code("postinc", 1, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ function = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ syntax = "add";
|
|
|
|
+ break;
|
|
|
|
+ case '-':
|
|
|
|
+ if(line.charAt(j - 1) == '-')
|
|
|
|
+ {
|
|
|
|
+ int last = lastSpecial.pop();
|
|
|
|
+ if(last - j >= 2)
|
|
|
|
+ {
|
|
|
|
+ // ++i
|
|
|
|
+ if(j + 1 - last == 0)
|
|
|
|
+ {
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(line.substring(j + 1, last), realLine, layer));
|
|
|
|
+ co.add(new Code("dec", 1, realLine, layer));
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Code c = co.get(co.size() - 1);
|
|
|
|
+ if("array.get".equals(c.function))
|
|
|
|
+ {
|
|
|
|
+ co.set(co.size() - 1, new Code("array.dec", 2, c.realLine, c.layer));
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ function = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ // i++
|
|
|
|
+ j--;
|
|
|
|
+ old = j;
|
|
|
|
+ j--;
|
|
|
|
+ if(line.charAt(j) == ']')
|
|
|
|
+ {
|
|
|
|
+ arrayFunction = "postdec";
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ while(j >= 0 && isAllowedChar(line.charAt(j)))
|
|
|
|
+ {
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ j++;
|
|
|
|
+ if(j - old == 0)
|
|
|
|
+ {
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(line.substring(j, old), realLine, layer));
|
|
|
|
+ co.add(new Code("postdec", 1, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ function = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ syntax = "sub";
|
|
|
|
+ break;
|
|
|
|
+ case '*': syntax = "mul"; break;
|
|
|
|
+ case '/': syntax = "div"; break;
|
|
|
|
+ case '%': syntax = "math.mod"; break;
|
|
|
|
+ case '^': syntax = "math.pow"; break;
|
|
|
|
+ case '<': syntax = "less"; break;
|
|
|
|
+ case '>': syntax = "greater"; break;
|
|
|
|
+ case '|':
|
|
{
|
|
{
|
|
- function = push(strings, co, j);
|
|
|
|
- nextFunction.put(bracketCounter, syntax);
|
|
|
|
- j--;
|
|
|
|
- lastSpecial.push(j);
|
|
|
|
- j--;
|
|
|
|
- continue;
|
|
|
|
|
|
+ if(line.charAt(j - 1) == '|')
|
|
|
|
+ {
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ nextFunction.put(bracketCounter, "or");
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- function = push(strings, co, j);
|
|
|
|
- //if(function)
|
|
|
|
- //{
|
|
|
|
- //System.out.println("push last result");
|
|
|
|
- //}
|
|
|
|
- int checker = j - 2;
|
|
|
|
- while(checker >= 0)
|
|
|
|
|
|
+ case '&':
|
|
{
|
|
{
|
|
- if(!isAllowedChar(line.charAt(checker)))
|
|
|
|
|
|
+ if(line.charAt(j - 1) == '&')
|
|
{
|
|
{
|
|
- throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ nextFunction.put(bracketCounter, "and");
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- checker--;
|
|
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- String var;
|
|
|
|
- switch(before)
|
|
|
|
|
|
+ case ']':
|
|
{
|
|
{
|
|
- case '+': syntax = "add"; break;
|
|
|
|
- case '-': syntax = "sub"; break;
|
|
|
|
- case '*': syntax = "mul"; break;
|
|
|
|
- case '/': syntax = "div"; break;
|
|
|
|
- default:
|
|
|
|
- var = line.substring(0, j);
|
|
|
|
- co.add(new Code(var, realLine, layer));
|
|
|
|
|
|
+ bracketCounter++;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- if(syntax != null)
|
|
|
|
|
|
+ case '[':
|
|
{
|
|
{
|
|
- var = line.substring(0, j - 1);
|
|
|
|
- co.add(new Code(Code.convertInput(strings, var, true), realLine, layer));
|
|
|
|
- co.add(new Code(syntax, 2, realLine, layer));
|
|
|
|
- //System.out.println("push last result");
|
|
|
|
- co.add(new Code(var, realLine, layer));
|
|
|
|
|
|
+ old = lastSpecial.pop();
|
|
|
|
+ if(old - j >= 2)
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code(Code.convertInput(strings, line.substring(j + 1, old), true), realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ String sNextFunction = nextFunction.remove(bracketCounter);
|
|
|
|
+ if(sNextFunction != null)
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code(sNextFunction, 2, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ old = j;
|
|
|
|
+ j--;
|
|
|
|
+ while(j >= 0 && isAllowedChar(line.charAt(j)))
|
|
|
|
+ {
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ j++;
|
|
|
|
+ co.add(new Code(line.substring(j, old), realLine, layer));
|
|
|
|
+ if(arrayFunction != null)
|
|
|
|
+ {
|
|
|
|
+ if(arrayFunction.equals("postinc") || arrayFunction.equals("postdec"))
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code("array." + arrayFunction, 2, realLine, layer));
|
|
|
|
+ arrayFunction = null;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code("array." + arrayFunction, 3, realLine, layer));
|
|
|
|
+ arrayFunction = null;
|
|
|
|
+ j = -1;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ else if(arrayState)
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code("array.get", 2, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code("array.set", 3, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ function = true;
|
|
|
|
+ bracketCounter--;
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- co.add(new Code("setvar", 2, realLine, layer));
|
|
|
|
- j = -1;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- case ')':
|
|
|
|
- {
|
|
|
|
- bracketCounter++;
|
|
|
|
- lastSpecial.push(j);
|
|
|
|
- commaCounter.push(0);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- case ',':
|
|
|
|
- {
|
|
|
|
- commaCounter.push(commaCounter.pop() + 1);
|
|
|
|
- function = push(strings, co, j);
|
|
|
|
- lastSpecial.push(j);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- case '(':
|
|
|
|
- {
|
|
|
|
- commas = commaCounter.pop() + 1;
|
|
|
|
- //if(function)
|
|
|
|
- //{
|
|
|
|
- //System.out.println("push last result");
|
|
|
|
- //}
|
|
|
|
- if(line.charAt(j + 1) != ')')
|
|
|
|
|
|
+ case '=':
|
|
{
|
|
{
|
|
- if(j + 1 != lastSpecial.peek())
|
|
|
|
|
|
+ char before = line.charAt(j - 1);
|
|
|
|
+ switch(before)
|
|
|
|
+ {
|
|
|
|
+ case '=': syntax = "equal"; break;
|
|
|
|
+ case '!': syntax = "notequal"; break;
|
|
|
|
+ case '>': syntax = "greaterequal"; break;
|
|
|
|
+ case '<': syntax = "lessequal"; break;
|
|
|
|
+ case ']': syntax = "bracket"; break;
|
|
|
|
+ }
|
|
|
|
+ if(syntax != null)
|
|
|
|
+ {
|
|
|
|
+ if(syntax.equals("bracket"))
|
|
|
|
+ {
|
|
|
|
+ arrayState = false;
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ nextFunction.put(bracketCounter, syntax);
|
|
|
|
+ j--;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ j--;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ int checker = j - 2;
|
|
|
|
+ char c;
|
|
|
|
+ while(checker >= 0)
|
|
|
|
+ {
|
|
|
|
+ c = line.charAt(checker);
|
|
|
|
+ if(c == ']')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ else if(!isAllowedChar(c))
|
|
|
|
+ {
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
|
|
+ }
|
|
|
|
+ checker--;
|
|
|
|
+ }
|
|
|
|
+ String var;
|
|
|
|
+ switch(before)
|
|
|
|
+ {
|
|
|
|
+ case '+': syntax = "add"; break;
|
|
|
|
+ case '-': syntax = "sub"; break;
|
|
|
|
+ case '*': syntax = "mul"; break;
|
|
|
|
+ case '/': syntax = "div"; break;
|
|
|
|
+ default:
|
|
|
|
+ var = line.substring(0, j);
|
|
|
|
+ co.add(new Code(var, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ if(syntax != null)
|
|
|
|
+ {
|
|
|
|
+ if(line.charAt(j - 2) == ']')
|
|
|
|
+ {
|
|
|
|
+ arrayFunction = syntax;
|
|
|
|
+ j -= 2;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(line.substring(0, j - 1), realLine, layer));
|
|
|
|
+ co.add(new Code(syntax + "var", 2, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- co.add(new Code(Code.convertInput(strings, line.substring(j + 1, lastSpecial.pop()), true), realLine, layer));
|
|
|
|
|
|
+ co.add(new Code("setvar", 2, realLine, layer));
|
|
}
|
|
}
|
|
|
|
+ j = -1;
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ case ')':
|
|
{
|
|
{
|
|
- commas = 0;
|
|
|
|
|
|
+ bracketCounter++;
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ commaCounter.push(0);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- String sNextFunction = nextFunction.remove(bracketCounter);
|
|
|
|
- if(sNextFunction != null)
|
|
|
|
|
|
+ case ',':
|
|
{
|
|
{
|
|
- co.add(new Code(sNextFunction, 2, realLine, layer));
|
|
|
|
- //System.out.println("push last result");
|
|
|
|
|
|
+ commaCounter.push(commaCounter.pop() + 1);
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ lastSpecial.push(j);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- bracketCounter--;
|
|
|
|
- function = true;
|
|
|
|
- old = j;
|
|
|
|
- j--;
|
|
|
|
- while(j >= 0 && isAllowedChar(line.charAt(j)))
|
|
|
|
|
|
+ case '(':
|
|
{
|
|
{
|
|
|
|
+ commas = commaCounter.pop() + 1;
|
|
|
|
+ if(line.charAt(j + 1) != ')')
|
|
|
|
+ {
|
|
|
|
+ if(j + 1 != lastSpecial.peek())
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code(Code.convertInput(strings, line.substring(j + 1, lastSpecial.pop()), true), realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ commas = 0;
|
|
|
|
+ }
|
|
|
|
+ String sNextFunction = nextFunction.remove(bracketCounter);
|
|
|
|
+ if(sNextFunction != null)
|
|
|
|
+ {
|
|
|
|
+ co.add(new Code(sNextFunction, 2, realLine, layer));
|
|
|
|
+ }
|
|
|
|
+ bracketCounter--;
|
|
|
|
+ function = true;
|
|
|
|
+ old = j;
|
|
j--;
|
|
j--;
|
|
|
|
+ while(j >= 0 && isAllowedChar(line.charAt(j)))
|
|
|
|
+ {
|
|
|
|
+ j--;
|
|
|
|
+ }
|
|
|
|
+ j++;
|
|
|
|
+ String functionName = line.substring(j, old).toLowerCase();
|
|
|
|
+ if(!parser.isRegisteredFunction(functionName))
|
|
|
|
+ {
|
|
|
|
+ throw new NoSuchMethodException(scriptName, realLine, functionName);
|
|
|
|
+ }
|
|
|
|
+ co.add(new Code(functionName, commas, realLine, layer));
|
|
|
|
+ lastSpecial.push(j);
|
|
}
|
|
}
|
|
- j++;
|
|
|
|
- String functionName = line.substring(j, old);
|
|
|
|
- if(!parser.isRegisteredFunction(functionName))
|
|
|
|
- {
|
|
|
|
- throw new NoSuchMethodException(scriptName, realLine, functionName);
|
|
|
|
- }
|
|
|
|
- co.add(new Code(functionName, commas, realLine, layer));
|
|
|
|
|
|
+ }
|
|
|
|
+ if(syntax != null)
|
|
|
|
+ {
|
|
|
|
+ function = push(strings, co, j);
|
|
|
|
+ nextFunction.put(bracketCounter, syntax);
|
|
lastSpecial.push(j);
|
|
lastSpecial.push(j);
|
|
}
|
|
}
|
|
|
|
+ j--;
|
|
}
|
|
}
|
|
- if(syntax != null)
|
|
|
|
- {
|
|
|
|
- function = push(strings, co, j);
|
|
|
|
- nextFunction.put(bracketCounter, syntax);
|
|
|
|
- lastSpecial.push(j);
|
|
|
|
- }
|
|
|
|
- j--;
|
|
|
|
|
|
+ }
|
|
|
|
+ catch(StringIndexOutOfBoundsException ex)
|
|
|
|
+ {
|
|
|
|
+ throw new PreScriptException(scriptName, realLine, "unexpected character");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|