Example #1
0
  private IExpressionNode findVariableByName(String name) {
    for (IExpressionNode node : nodeList) {
      if (node.getType() == IExpressionNode.VARIABLE_NODE)
        if (((VariableExpressionNode) node).getName().equals(name)) return node;
    }

    return null;
  }
Example #2
0
  private IExpressionNode assignmentOp(IExpressionNode variable) throws ParserException {
    if (lookahead.token == Token.EQUALS) {
      nextToken();
      IExpressionNode newVariable = expression();
      if (!(variable.getType() == IExpressionNode.VARIABLE_NODE))
        throw new ParserException(String.format(ERR_VARIABLE, variable.toString()));

      try {
        variable.accept(
            new SetVariable(((VariableExpressionNode) variable).getName(), newVariable.getValue()));
      } catch (EvaluationException ex) {
        throw new ParserException(
            String.format("The variable [%s] did not return a valid value", newVariable));
      }
    }

    return variable;
  }
Example #3
0
  private IExpressionNode sumOp(IExpressionNode expression) throws ParserException {
    if (lookahead.token == Token.PLUSMINUS) {
      // sum_op -> PLUSMINUS term sum_op
      AdditionExpressionNode sum;
      if (expression.getType() == IExpressionNode.ADDITION_NODE)
        sum = (AdditionExpressionNode) expression;
      else sum = new AdditionExpressionNode(expression, true);

      boolean positive = lookahead.sequence.equals(PLUS);
      nextToken();
      IExpressionNode t = term();
      sum.add(t, positive);

      return sumOp(sum);
    }

    // sum_op -> EPSILON
    return expression;
  }
Example #4
0
  private IExpressionNode termOp(IExpressionNode expression) throws ParserException {
    if (lookahead.token == Token.MULTDIV) {
      // term_op -> MULTDIV factor term_op
      MultiplicationExpressionNode prod;

      if (expression.getType() == IExpressionNode.MULTIPLICATION_NODE)
        prod = (MultiplicationExpressionNode) expression;
      else prod = new MultiplicationExpressionNode(expression, true);

      boolean positive = lookahead.sequence.equals(MULT);
      nextToken();
      IExpressionNode f = signedFactor();
      prod.add(f, positive);

      return termOp(prod);
    }

    // term_op -> EPSILON
    return expression;
  }