Package org.formulacompiler.compiler.internal.expressions

Examples of org.formulacompiler.compiler.internal.expressions.ExpressionNode


   * Rewrites {@code LOOKUP( x, xs, ys [,type] )} to {@code INDEX( ys, MATCH( x, xs [,type] ))}.
   */
  private ExpressionNode rewriteVectorLookup( ExpressionNodeForFunction _fun )
  {
    // LATER Don't rewrite when over large repeating sections.
    final ExpressionNode x, xs, ys, match;
    x = _fun.argument( 0 );
    xs = _fun.argument( 1 );
    ys = _fun.argument( 2 );
    if (_fun.cardinality() >= 4) {
      final ExpressionNode type = _fun.argument( 3 );
      match = fun( INTERNAL_MATCH_INT, x, xs, type );
    }
    else {
      match = fun( INTERNAL_MATCH_INT, x, xs );
    }
View Full Code Here



  private ExpressionNode rewriteHVLookup( ExpressionNodeForFunction _fun )
  {
    final Function fun = _fun.getFunction();
    final ExpressionNode valueNode = _fun.argument( 0 );
    final ExpressionNodeForArrayReference arrayNode = (ExpressionNodeForArrayReference) _fun.argument( 1 );
    final ExpressionNode indexNode = _fun.argument( 2 );
    final ExpressionNode lookupArrayNode = getHVLookupSubArray( fun, arrayNode, 0 );

    final ExpressionNode matchNode;
    final Function matchFun = (indexNode instanceof ExpressionNodeForConstantValue) ? INTERNAL_MATCH_INT : MATCH;
    if (_fun.cardinality() >= 4) {
      final ExpressionNode typeNode = _fun.argument( 3 );
      matchNode = new ExpressionNodeForFunction( matchFun, valueNode, lookupArrayNode, typeNode );
    }
    else {
      matchNode = new ExpressionNodeForFunction( matchFun, valueNode, lookupArrayNode );
    }

    if (indexNode instanceof ExpressionNodeForConstantValue) {
      final ExpressionNodeForConstantValue constIndex = (ExpressionNodeForConstantValue) indexNode;
      final int index = this.numericType.toInt( constIndex.value(), -1 ) - 1;
      final ExpressionNode valueArrayNode = getHVLookupSubArray( fun, arrayNode, index );
      return fun( INDEX, valueArrayNode, matchNode );
    }
    else {
      final String matchRefName = "x";
      final ExpressionNode matchRefNode = var( matchRefName );
      final ExpressionNode selectorNode = indexNode;
      final ExpressionNode defaultNode = err( "#VALUE/REF! because index is out of range in H/VLOOKUP" );

      final ArrayDescriptor desc = arrayNode.arrayDescriptor();
      final int nArrays = (fun == HLOOKUP) ? desc.numberOfRows() : desc.numberOfColumns();
      final ExpressionNodeForSwitchCase[] caseNodes = new ExpressionNodeForSwitchCase[ nArrays ];
      for (int iArray = 0; iArray < nArrays; iArray++) {
        final ExpressionNode valueArrayNode = getHVLookupSubArray( fun, arrayNode, iArray );
        final ExpressionNode lookupNode = fun( INDEX, valueArrayNode, matchRefNode );
        caseNodes[ iArray ] = new ExpressionNodeForSwitchCase( lookupNode, iArray + 1 );
      }
      final ExpressionNode switchNode = new ExpressionNodeForSwitch( selectorNode, defaultNode, caseNodes );
      final ExpressionNode matchLetNode = letByName( matchRefName, matchNode, switchNode );
      return matchLetNode;
    }
  }
View Full Code Here

  {
    final List<ExpressionNode> newArgs = New.list();
    newArgs.addAll( _fun.arguments() );
    boolean rewritten = false;
    for (int iArg = 1; iArg <= 2 && iArg < _fun.cardinality(); iArg++) {
      final ExpressionNode arg = _fun.argument( iArg );
      if (arg instanceof ExpressionNodeForFunction && ((ExpressionNodeForFunction) arg).getFunction() == MATCH) {
        final ExpressionNode newArg = fun( INTERNAL_MATCH_INT );
        newArg.arguments().addAll( arg.arguments() );
        newArgs.set( iArg, newArg );
        rewritten = true;
      }
    }
    if (rewritten) {
      final ExpressionNode newFun = _fun.cloneWithoutArguments();
      newFun.arguments().addAll( newArgs );
      return newFun;
    }
    return _fun;
  }
View Full Code Here

      else {
        dynArgs.add( (ExpressionNode) arg );
      }
    }
    if (dynArgs.size() > 0) {
      final ExpressionNode result = node().cloneWithoutArguments();
      result.arguments().addAll( dynArgs );
      return result;
    }
    else {
      return ConstResult.valueOf( !_returnThisIfFound );
    }
View Full Code Here

  protected TypedResult evaluateToNode( TypedResult... _args ) throws InterpreterException
  {
    // No need to clone leaf nodes.
    if (_args.length == 0) return node();

    ExpressionNode result = node().cloneWithoutArguments();
    for (final TypedResult arg : _args) {
      result.addArgument( valueToNode( arg ) );
    }
    return result;
  }
View Full Code Here

            final StringBuilder sb = new StringBuilder();
            CellAddressImpl.appendNameA1ForCellIndex( sb, ci, false, ri, false );
            writeAttribute( XMLConstants.Main.CELL_REFERENCE, sb.toString() );

            final Object value = cell == null ? null : cell.getValue();
            final ExpressionNode expression;
            if (cell != null && cell instanceof CellWithExpression)
              expression = ((CellWithExpression) cell).getExpression();
            else
              expression = null;
View Full Code Here

      }
      attributes.put( XMLConstants.Table.STYLE_NAME, styleName );
      this.styles.add( new Style( styleName, StyleFamilies.TABLE_CELL ) );

      if (_cell instanceof CellWithExpression) {
        final ExpressionNode exp = ((CellWithExpression) _cell).getExpression();
        attributes.put( XMLConstants.Table.FORMULA, ExpressionFormatter.format( exp, _cell.getCellIndex() ) );
      }
      else {
        final Object constantValue = _cell.getValue();
        if (constantValue != null) {
View Full Code Here

  /**
   * Pops the top node and adds it as the last child to the then top node.
   */
  protected final void popNodeAndMergeIntoTopNode()
  {
    final ExpressionNode popped = popNode();
    final ExpressionNode top = peekNode();
    top.addArgument( popped );
  }
View Full Code Here

  }

  @Override
  public ExpressionNode getExpression() throws SpreadsheetException
  {
    final ExpressionNode own = super.getExpression();
    if (null != own) {
      return own;
    }
    else {
      try {
        final ExpressionNode parsed = this.expressionParser.parseExpression( this.getCellIndex() );
        setExpression( parsed );
        return parsed;
      }
      catch (CompilerException e) {
        final SpreadsheetException.UnsupportedExpression thrown = new SpreadsheetException.UnsupportedExpression( e );
View Full Code Here

  }

  @Override
  protected void convertRangesToCells( final boolean _allowRanges )
  {
    final ExpressionNode node = peekNode();
    convertRangesToCells( new ExpressionNodeSource()
    {
      public ExpressionNode getExpressionNode()
      {
        return node;
View Full Code Here

TOP

Related Classes of org.formulacompiler.compiler.internal.expressions.ExpressionNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.