All Downloads are FREE. Search and download functionalities are using the official Maven repository.

manifold.preprocessor.statement.IfStatement Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2019 - Manifold Systems LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package manifold.preprocessor.statement;

import java.util.List;
import manifold.preprocessor.definitions.Definitions;
import manifold.preprocessor.TokenType;
import manifold.preprocessor.expression.EmptyExpression;
import manifold.preprocessor.expression.Expression;

public class IfStatement extends Statement
{
  private final Expression _expr;
  private final List _ifBlock;
  private final List _elifs;
  private final List _elseBlock;
  private final int _elseStart;

  public IfStatement( TokenType tokenType, int start, int end, Expression expr,
                      List ifBlock, List elifs, List elseBlock, int elseStart )
  {
    super( tokenType, start, end );
    _expr = expr;
    _ifBlock = ifBlock;
    _elifs = elifs;
    _elseStart = elseStart;
    _elseBlock = elseBlock;
  }

  @Override
  public void execute( StringBuilder result, CharSequence source, boolean visible, Definitions definitions )
  {
    preserveMaskedOutSpace( result, source, getTokenStart(), _expr.getEndOffset() );

    boolean ifCond = visible && evalExpr( definitions );
    for( Statement stmt: _ifBlock )
    {
      stmt.execute( result, source, ifCond, definitions );
    }

    boolean elifPassed = false;
    for( IfStatement elif: _elifs )
    {
      boolean elifCond = visible && !elifPassed && !ifCond && elif.evalExpr( definitions );
      elifPassed = elifPassed || elifCond;
      elif.execute( result, source, elifCond, definitions );
    }

    if( _elseStart >= 0 )
    {
      preserveMaskedOutSpace( result, source, _elseStart, _elseStart + "#else".length() );
    }

    for( Statement stmt: _elseBlock )
    {
      stmt.execute( result, source, visible && !ifCond && !elifPassed, definitions );
    }

    int endIfStart = getTokenEnd() - "#endif".length();
    if( endIfStart >= 0 && source.subSequence( endIfStart, getTokenEnd() ).toString().equals( "#endif" ) )
    {
      preserveMaskedOutSpace( result, source, endIfStart, getTokenEnd() );
    }
  }

  @Override
  public void execute( List result, boolean visible, Definitions definitions )
  {
    if( !visible )
    {
      return;
    }

    boolean ifCond = evalExpr( definitions );
    for( Statement stmt: _ifBlock )
    {
      stmt.execute( result, ifCond, definitions );
    }

    boolean elifPassed = false;
    for( IfStatement elif: _elifs )
    {
      boolean elifCond = !elifPassed && !ifCond && elif.evalExpr( definitions );
      elifPassed = elifPassed || elifCond;
      elif.execute( result, elifCond, definitions );
    }

    for( Statement stmt: _elseBlock )
    {
      stmt.execute( result, !ifCond && !elifPassed, definitions );
    }
  }

  @Override
  public boolean hasPreprocessorDirectives()
  {
    return true;
  }

  private boolean evalExpr( Definitions definitions )
  {
    return !(_expr instanceof EmptyExpression) && !_expr.hasErrors() && _expr.evaluate( definitions );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy