Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1.     public static Object[] evaluateIfExpression(String expression, int opCount, int[] iValues, String[] sValues, long[] lValues,
  2.         int intLocals, int stringLocals, int longLocals, String[] variableNames, HashMap<String, LocalVariable> variables, String[] lines, int index, int switchBlocksCount, HashMap<Integer, HashMap<Integer, Integer>> switchBlocks) {
  3.         ArrayList<CS2Instruction> instructions = new ArrayList<>();
  4.         boolean hasBlock = expression.endsWith("{");
  5.         Pattern pattern = Pattern.compile("else(\\{)?");
  6.         Matcher matcher = pattern.matcher(expression.replaceAll("\\s", ""));
  7.         System.out.println("Evaluating if: "+expression);
  8.         int elseIndex = -1;
  9.         if(expression.contains("else if(") || expression.contains("else if (")) {
  10.             elseIndex = opCount++;
  11.             instructions.add(CS2Instruction.GOTO);
  12.         }
  13.         if(!matcher.matches()) {
  14.             System.out.println(expression+" does not match else( \\{)?");
  15.             String ifExpression = expression.substring(expression.indexOf("(") + 1, expression.lastIndexOf(")"));
  16.             Object[] comparison = getComparison(ifExpression);
  17.             if (comparison == null)
  18.                 throw new CompilerException("Unable to evaluate if statement: " + ifExpression);
  19.             CS2Instruction instruction = (CS2Instruction) comparison[1];
  20.             String[] split = ifExpression.split(" ?" + (String) comparison[0] + " ?");
  21.             for (String ex : split) {
  22.                 Object[] values = evaluateParameter(ex, opCount, iValues,
  23.                         sValues, lValues, intLocals,
  24.                         stringLocals, longLocals, variableNames, variables, lines, index, switchBlocksCount, switchBlocks);
  25.                 instructions.addAll((ArrayList<CS2Instruction>) values[0]);
  26.                 opCount = (int) values[1];
  27.                 iValues = (int[]) values[2];
  28.                 sValues = (String[]) values[3];
  29.                 lValues = (long[]) values[4];
  30.                 intLocals = (int) values[5];
  31.                 stringLocals = (int) values[6];
  32.                 longLocals = (int) values[7];
  33.                 if (values[8] != null)
  34.                     variableNames = (String[]) values[8];
  35.                 variables = (HashMap<String, LocalVariable>) values[9];
  36.                 index = (int) values[10];
  37.                 switchBlocksCount = (int) values[11];
  38.                 switchBlocks = (HashMap<Integer, HashMap<Integer, Integer>>) values[12];
  39.             }
  40.             instructions.add(instruction);
  41.             iValues[opCount++] = 1;
  42.         }
  43.         instructions.add(CS2Instruction.GOTO);
  44.         int sizeIndex = opCount++;
  45.         if (!hasBlock) {
  46.             String nextLine = lines[index++];
  47.             Object[] values = evaluateExpression(
  48.                     nextLine, opCount,
  49.                     iValues, sValues, lValues,
  50.                     intLocals, stringLocals, longLocals, variableNames, variables, lines, index, switchBlocksCount, switchBlocks);
  51.             instructions.addAll((ArrayList<CS2Instruction>) values[0]);
  52.             opCount = (int) values[1];
  53.             iValues = (int[]) values[2];
  54.             sValues = (String[]) values[3];
  55.             lValues = (long[]) values[4];
  56.             intLocals = (int) values[5];
  57.             stringLocals = (int) values[6];
  58.             longLocals = (int) values[7];
  59.             if (values[8] != null)
  60.                 variableNames = (String[]) values[8];
  61.             variables = (HashMap<String, LocalVariable>) values[9];
  62.             index = (int) values[10];
  63.             switchBlocksCount = (int) values[11];
  64.             switchBlocks = (HashMap<Integer, HashMap<Integer, Integer>>) values[12];
  65.             iValues[sizeIndex] = opCount - sizeIndex - 1;
  66.         } else {
  67.             while(!(expression = lines[index++]).equals("}")) {
  68.                 Object[] values = evaluateExpression(expression, opCount, iValues, sValues, lValues, intLocals,
  69.                         stringLocals, longLocals, variableNames, variables, lines, index, switchBlocksCount, switchBlocks);
  70.                 instructions.addAll((ArrayList<CS2Instruction>) values[0]);
  71.                 opCount = (int) values[1];
  72.                 iValues = (int[]) values[2];
  73.                 sValues = (String[]) values[3];
  74.                 lValues = (long[]) values[4];
  75.                 intLocals = (int) values[5];
  76.                 stringLocals = (int) values[6];
  77.                 longLocals = (int) values[7];
  78.                 if (values[8] != null)
  79.                     variableNames = (String[]) values[8];
  80.                 variables = (HashMap<String, LocalVariable>) values[9];
  81.                 index = (int) values[10];
  82.                 switchBlocksCount = (int) values[11];
  83.                 switchBlocks = (HashMap<Integer, HashMap<Integer, Integer>>) values[12];
  84.             }
  85.             iValues[sizeIndex] = opCount - sizeIndex-1;
  86.         }
  87.         String nextLine = lines[index].replaceAll("\\t", "").replaceAll(" {4}", "");
  88.         System.out.println("Next Line: "+nextLine);
  89.         if(nextLine.startsWith("else")) {
  90.             iValues[sizeIndex]++;
  91.             Object[] values = evaluateIfExpression(lines[index++], opCount, iValues, sValues, lValues, intLocals,
  92.                     stringLocals, longLocals, variableNames, variables, lines, index, switchBlocksCount, switchBlocks);
  93.             instructions.addAll((ArrayList<CS2Instruction>) values[0]);
  94.             opCount = (int) values[1];
  95.             iValues = (int[]) values[2];
  96.             sValues = (String[]) values[3];
  97.             lValues = (long[]) values[4];
  98.             intLocals = (int) values[5];
  99.             stringLocals = (int) values[6];
  100.             longLocals = (int) values[7];
  101.             if (values[8] != null)
  102.                 variableNames = (String[]) values[8];
  103.             variables = (HashMap<String, LocalVariable>) values[9];
  104.             index = (int) values[10];
  105.             switchBlocksCount = (int) values[11];
  106.             switchBlocks = (HashMap<Integer, HashMap<Integer, Integer>>) values[12];
  107.         }
  108.         if (elseIndex != -1) {
  109.             iValues[elseIndex] = opCount - elseIndex-1;
  110.             System.out.println("ElseIndex: "+elseIndex);
  111.         }
  112.         return new Object[] { instructions, opCount, iValues, sValues, lValues, intLocals, stringLocals, longLocals,
  113.                 variableNames, variables, index, switchBlocksCount, switchBlocks };
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement