Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. public String calculateArg(String arg){
  2.  
  3.         String numfinalregex = "^[+-]?[0-9 ]+$";
  4.         Pattern numfinalpattern = Pattern.compile(numfinalregex);
  5.         Matcher numfinalmatcher = numfinalpattern.matcher(arg);
  6.  
  7.         String registerfinalregex = "^(%e[abcd]x)+$";
  8.         Pattern registerfinalpattern = Pattern.compile(registerfinalregex);
  9.         Matcher registerfinalmatcher = registerfinalpattern.matcher(arg);
  10.  
  11.         String bracketregex = "\\((.*)\\)";
  12.         Pattern bracketpattern = Pattern.compile(bracketregex);
  13.         Matcher bracketmatcher;
  14.  
  15.         String inbracketregex = "(?<=\\()(.*)(?=\\))";
  16.         Pattern inbracketpattern = Pattern.compile(inbracketregex);
  17.         Matcher inbracketmatcher;
  18.  
  19.         String anybracketregex = "\\(|\\)";
  20.         Pattern anybracketpattern = Pattern.compile(anybracketregex);
  21.         Matcher anybracketmatcher = anybracketpattern.matcher(arg);
  22.  
  23.         String multidivregex = "(^[+-])?( )*[0-9]+( )*[*/]( )*[0-9]+";
  24.         Pattern multidivpattern = Pattern.compile(multidivregex);
  25.         Matcher multidivmatcher;
  26.  
  27.         String addsubregex = "(^[+-])?( )*[0-9]+( )*[\\+\\-]( )*[0-9]+";
  28.         Pattern addsubpattern = Pattern.compile(addsubregex);
  29.         Matcher addsubmatcher;
  30.  
  31.         String registerregex ="(%e[abcd]x)+";
  32.         Pattern registerpattern = Pattern.compile(registerregex);
  33.         Matcher registermatcher;
  34.  
  35.         String errorregex = "^( )*[0-9 ]+( )*[\\+\\-\\*\\/]$";
  36.         Pattern errorpattern = Pattern.compile(errorregex);
  37.         Matcher errormatcher;
  38.  
  39.         boolean regs;
  40.  
  41.         if(!this.keyword.equals("int") && !(this.keyword.equals("push") && registerfinalmatcher.find())){
  42.             while(!numfinalmatcher.find()) {
  43.  
  44.                 numfinalmatcher = numfinalpattern.matcher(arg);
  45.                 bracketmatcher = bracketpattern.matcher(arg);
  46.                 inbracketmatcher = inbracketpattern.matcher(arg);
  47.                 anybracketmatcher = anybracketpattern.matcher(arg);
  48.                 multidivmatcher = multidivpattern.matcher(arg);
  49.                 addsubmatcher = addsubpattern.matcher(arg);
  50.                 registermatcher = registerpattern.matcher(arg);
  51.                 errormatcher = errorpattern.matcher(arg);
  52.  
  53.                 regs = false;
  54.  
  55.                 while(registermatcher.find()){
  56.                     int tmpreg = 0;
  57.                     if (registermatcher.group(0).equals("%eax")) tmpreg = this.parent.reg.getEax();
  58.                     if (registermatcher.group(0).equals("%ebx")) tmpreg = this.parent.reg.getEbx();
  59.                     if (registermatcher.group(0).equals("%ecx")) tmpreg = this.parent.reg.getEcx();
  60.                     if (registermatcher.group(0).equals("%edx")) tmpreg = this.parent.reg.getEdx();
  61.  
  62.                     arg = arg.replaceAll(registermatcher.group(0), Integer.toString(tmpreg));
  63.  
  64.                     regs = true;
  65.                 }
  66.                 if(regs) continue;
  67.                 if(anybracketmatcher.find()){
  68.                     if(bracketmatcher.find()) {
  69.                         if (inbracketmatcher.find()) {
  70.                             String temp;
  71.                             if ((temp = calculateArg(inbracketmatcher.group(0))) != "#ERR") {
  72.                                 arg = arg.replace(bracketmatcher.group(0), temp);
  73.                                 //arg = calculateArg(arg);
  74.                             } else return "#ERR";
  75.                         } else return "#ERR";
  76.                     } else return "#ERR";
  77.                 } else if(multidivmatcher.find()){
  78.                     String ma1 = "";
  79.                     String ma2 = "";
  80.                     String mop = "";
  81.  
  82.                     String marg1regex = "";
  83.                     String marg2regex = "";
  84.                     String mopregex = "";
  85.  
  86.                     String mtmptxt = "";
  87.  
  88.                     Pattern mtmppattern;
  89.                     Matcher mtmpmatcher;
  90.  
  91.                     mtmptxt = multidivmatcher.group(0);
  92.  
  93.                     marg1regex = "(^[+-])?( )*[0-9]+( )*(?=[\\*\\/])";
  94.  
  95.                     mtmppattern = Pattern.compile(marg1regex);
  96.                     mtmpmatcher = mtmppattern.matcher(arg);
  97.                     if(mtmpmatcher.find()) ma1 = mtmpmatcher.group(0);
  98.  
  99.                     marg2regex = "(?<=("+ma1+")[\\*\\/])( )*[0-9]+( )*";
  100.                     mopregex = "(?<=("+ma1+"))[\\*\\/]";
  101.  
  102.                     mtmppattern = Pattern.compile(marg2regex);
  103.                     mtmpmatcher = mtmppattern.matcher(arg);
  104.                     if(mtmpmatcher.find()) ma2 = mtmpmatcher.group(0);
  105.  
  106.                     mtmppattern = Pattern.compile(mopregex);
  107.                     mtmpmatcher = mtmppattern.matcher(arg);
  108.                     if(mtmpmatcher.find()) mop = mtmpmatcher.group(0);
  109.  
  110.                     arg = arg.replace(mtmptxt, this.solve(ma1, ma2, mop));
  111.  
  112.                 } else if(addsubmatcher.find()){
  113.                     String a1 = "";
  114.                     String a2 = "";
  115.                     String op = "";
  116.  
  117.                     String arg1regex = "";
  118.                     String arg2regex = "";
  119.                     String opregex = "";
  120.  
  121.                     String tmptxt = "";
  122.  
  123.                     Pattern tmppattern;
  124.                     Matcher tmpmatcher;
  125.  
  126.                     tmptxt = addsubmatcher.group(0);
  127.  
  128.                     arg1regex = "(^[+-])?( )*[0-9]+( )*(?=[\\+\\-])";
  129.  
  130.                     tmppattern = Pattern.compile(arg1regex);
  131.                     tmpmatcher = tmppattern.matcher(arg);
  132.                     if(tmpmatcher.find()) a1 = tmpmatcher.group(0);
  133.  
  134.                     arg2regex = "(?<=("+a1+")[\\+\\-])( )*[0-9]+( )*";
  135.                     opregex = "(?<=("+a1+"))[\\+\\-]";
  136.  
  137.                     tmppattern = Pattern.compile(arg2regex);
  138.                     tmpmatcher = tmppattern.matcher(arg);
  139.                     if(tmpmatcher.find()) a2 = tmpmatcher.group(0);
  140.  
  141.                     tmppattern = Pattern.compile(opregex);
  142.                     tmpmatcher = tmppattern.matcher(arg);
  143.                     if(tmpmatcher.find()) op = tmpmatcher.group(0);
  144.  
  145.                     arg = arg.replace(tmptxt, this.solve(a1, a2, op));
  146.  
  147.                 } else if(errormatcher.find()){
  148.                     return "#ERR";
  149.                 } //else return calculateArg(arg);
  150.             }
  151.             return arg;
  152.         }
  153.         return arg;
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement