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

com.creativewidgetworks.goldparser.simple3.rulehandlers.IfThenElse Maven / Gradle / Ivy

package com.creativewidgetworks.goldparser.simple3.rulehandlers;

import com.creativewidgetworks.goldparser.engine.ParserException;
import com.creativewidgetworks.goldparser.engine.Reduction;
import com.creativewidgetworks.goldparser.parser.GOLDParser;
import com.creativewidgetworks.goldparser.parser.ProcessRule;
import com.creativewidgetworks.goldparser.simple3.Simple3;

@ProcessRule(rule={
    " ::= if  then  end",
    " ::= if  then  else  end"
})

/**
 * Rule handler for the If-Then-Else rules.
 *
 * if  then 
 *   
 * end
 * 
 * if  then
 *   
 * else
 *   
 * end
 *
 * if  then
 *   
 * else if  then
 *   
 * else if ...
 *   
 * else
 *   
 * end end end ..
 *
 * @author Ralph Iden (http://www.creativewidgetworks.com)
 * @version 5.0.0 
 */
public class IfThenElse extends Reduction {
    private Reduction conditional;
    private Reduction thenStatements;
    private Reduction elseStatements;

    public IfThenElse(GOLDParser parser) {
        Reduction reduction = parser.getCurrentReduction();
        if (reduction != null) {
            if (reduction.size() == 5) {
                // if x then y end
                conditional    = reduction.get(1).asReduction();
                thenStatements = reduction.get(3).asReduction();                
            } else if (reduction.size() == 7) {
                // if x then y else z end
                conditional    = reduction.get(1).asReduction();
                thenStatements = reduction.get(3).asReduction(); 
                elseStatements = reduction.get(5).asReduction();
            } else {
                if (reduction.size() < 5) {
                    // if x then y form
                    parser.raiseParserException(Simple3.formatMessage("error.param_count", "5", String.valueOf(reduction.size())));
                }
                // if x then y else z end form
                parser.raiseParserException(Simple3.formatMessage("error.param_count", "7", String.valueOf(reduction.size())));
            }
        } else {
            parser.raiseParserException(Simple3.formatMessage("error.no_reduction"));
        }        
     }

    @Override
    public void execute() throws ParserException {
       if (conditional.getValue().asBool()) {
           thenStatements.execute();
       } else if (elseStatements != null) {
           elseStatements.execute();
       }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy