Advertisement
Guest User

Resolver_TOPK_lab_3

a guest
Apr 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package lab3.main;
  2.  
  3. import lab2.util.variantNative.Code;
  4. import lab3.util.Error;
  5.  
  6. import java.util.List;
  7. import java.util.Stack;
  8.  
  9. import static javafx.scene.input.KeyCode.CLOSE_BRACKET;
  10.  
  11. public class GermanResolverAutoLevak {
  12.     protected List<Integer> codes;
  13.     protected int codeNumber = 0;
  14.     protected Stack<Integer> errors = new Stack<>();
  15.     private boolean logicCondision;
  16.  
  17.     public GermanResolverAutoLevak() {
  18.     }
  19.  
  20.     public void setCodes(List<Integer> codes) {
  21.         this.codes = codes;
  22.         codes.add(-1);
  23.     }
  24.  
  25.     public boolean check() {
  26.         reset();
  27.  
  28.         try {
  29.             return isFragment();
  30.         } catch (Throwable e) {
  31.             return false;
  32.         }
  33.     }
  34.  
  35.     public Stack<Integer> getErrors() {
  36.         return errors;
  37.     }
  38.  
  39.  
  40.     protected boolean isFragment() {
  41.         if (!isIf()) {
  42.             errors.push(Error.Fragment.BEGIN_WITH_IF);
  43.             throw new IllegalArgumentException();
  44.         }
  45.  
  46.         if (!isLogicCondition()) {
  47.             errors.push(Error.Fragment.LOGIC_CONDITION_AFTER_IF);
  48.             throw new IllegalArgumentException();
  49.         }
  50.  
  51.         if (!isThen()) {
  52.             errors.push(Error.Fragment.THEN_AFTER_LOGIC_CONDITION);
  53.             throw new IllegalArgumentException();
  54.         }
  55.  
  56.         if (!isOperator()) {
  57.             errors.push(Error.Fragment.OPERATOR);
  58.             throw new IllegalArgumentException();
  59.         }
  60.  
  61.  
  62.         errors.clear();
  63.         return true;
  64.     }
  65.  
  66.     public boolean isLogicCondition() {
  67.         if (!isEquallyCondition()) {
  68.             errors.push(Error.Expression.BEGIN_WITH_EQUALLY_CONDITION);
  69.             return false;
  70.         }
  71.  
  72.         while (true) {
  73.             if (!isSign()) {
  74.                 codeNumber--;
  75.                 errors.push(Error.Expression.SIGN_AFTER_EQUALLY_CONDITION);
  76.                 return true;
  77.             } else {
  78.                 if (!isEquallyCondition()) {
  79.                     errors.push(Error.Expression.EQUALLY_CONDITION_AFTER_SIGN);
  80.                     throw new IllegalArgumentException();
  81.                 } else {
  82.                     errors.clear();
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     public boolean isEquallyCondition() {
  89.         if (!isOpenBracket()) {
  90.             errors.push(Error.EquallyCondition.BEGIN_WITH_OPEN_BRACKET);
  91.             throw new IllegalArgumentException();
  92.         }
  93.  
  94.         if (!isOperand()) {
  95.             errors.push(Error.EquallyCondition.OPERAND_AFTER_OPEN_BRACKET);
  96.             throw new IllegalArgumentException();
  97.         }
  98.  
  99.         if (!isDoubleEqually()) {
  100.             errors.push(Error.EquallyCondition.DOUBLE_EQUALLY_AFTER_OPERAND);
  101.             throw new IllegalArgumentException();
  102.         }
  103.  
  104.         if (!isOperand()) {
  105.             errors.push(Error.EquallyCondition.OPERAND_AFTER_CLOSE_BRACKET);
  106.             throw new IllegalArgumentException();
  107.         }
  108.  
  109.         if (!isCloseBracket()) {
  110.             errors.push(Error.EquallyCondition.CLOSE_BRACKET_AFTER_OPERAND);
  111.             throw new IllegalArgumentException();
  112.         }
  113.  
  114.         errors.clear();
  115.         return true;
  116.     }
  117.  
  118.  
  119.     protected boolean isOperator() {
  120.         if (!isIden()) {
  121.             errors.push(Error.Operator.BEGIN_WITH_IDEN);
  122.             return false;
  123.         }
  124.  
  125.         if (!isEqually()) {
  126.             errors.push(Error.Operator.EQUALLY_AFTER_IDEN);
  127.             throw new IllegalArgumentException();
  128.         }
  129.  
  130.         if (!isExpression()) {
  131.             errors.push(Error.Operator.EXPRESSION);
  132.             return false;
  133.         }
  134.  
  135.         errors.clear();
  136.         return true;
  137.     }
  138.  
  139.     protected boolean isExpression() {
  140.         if (!isOperand()) {
  141.             errors.push(Error.Expression.BEGIN_WITH_OPERAND);
  142. //            throw new IllegalArgumentException();
  143.             return false;
  144.         }
  145.  
  146.         while (true) {
  147.             if (!isSign()) {
  148.                 codeNumber--;
  149.                 errors.push(Error.Expression.SIGN_AFTER_OPERAND);
  150.                 return true;
  151.             } else {
  152.                 if (!isOperand()) {
  153.                     errors.push(Error.Expression.OPERAND_AFTER_SIGN);
  154.                     throw new IllegalArgumentException();
  155.                 } else {
  156.                     errors.clear();
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     protected boolean isOperand() {
  163.         switch (codes.get(codeNumber++)) {
  164.             case Code.IDEN:
  165.             case Code.DATA:
  166.                 return true;
  167.             default:
  168.                 errors.push(Error.OPERAND);
  169.                 return false;
  170.         }
  171.     }
  172.  
  173.     protected boolean isSign() {
  174.         switch (codes.get(codeNumber++)) {
  175.             case Code.Sign.PLUS:
  176.             case Code.Sign.MINUS:
  177.                 return true;
  178.             default:
  179.                 errors.push(Error.SIGN);
  180.                 return false;
  181.         }
  182.     }
  183.  
  184.     protected boolean isIf() {
  185.         return codes.get(codeNumber++) == Code.IF;
  186.     }
  187.  
  188.     protected boolean isThen() {
  189.         return codes.get(codeNumber++) == Code.THEN;
  190.     }
  191.  
  192.     protected boolean isIden() {
  193.         return codes.get(codeNumber++) == Code.IDEN;
  194.     }
  195.  
  196.     protected boolean isEqually() {
  197.         return codes.get(codeNumber++) == Code.Sign.EQUALLY;
  198.     }
  199.  
  200.     protected boolean isDoubleEqually() {
  201.         return codes.get(codeNumber++) == Code.Sign.DOUBLE_EQUALLY;
  202.     }
  203.  
  204.     public boolean isOpenBracket() {
  205.         return codes.get(codeNumber++) == Code.OPEN_BRACKET;
  206.     }
  207.  
  208.     public boolean isCloseBracket() {
  209.         return codes.get(codeNumber++) == Code.CLOSE_BRACKET;
  210.     }
  211.  
  212.     public void reset() {
  213.         codeNumber = 0;
  214.         errors.clear();
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement