org.unlaxer.tinyexpression.evaluator.javacode.BooleanExpressionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinyExpression Show documentation
Show all versions of tinyExpression Show documentation
TinyExpression implemented with Unlaxer
package org.unlaxer.tinyexpression.evaluator.javacode;
import java.util.List;
import org.unlaxer.Token;
import org.unlaxer.parser.Parser;
import org.unlaxer.tinyexpression.parser.AndParser;
import org.unlaxer.tinyexpression.parser.BooleanExpressionParser;
import org.unlaxer.tinyexpression.parser.EqualEqualParser;
import org.unlaxer.tinyexpression.parser.NotEqualParser;
import org.unlaxer.tinyexpression.parser.OrParser;
import org.unlaxer.tinyexpression.parser.XorParser;
public class BooleanExpressionBuilder implements TokenCodeBuilder {
static final BooleanExpressionBuilder SINGLETON = new BooleanExpressionBuilder();
@Override
public void build(SimpleJavaCodeBuilder builder, Token token,
TinyExpressionTokens tinyExpressionTokens) {
builder.append("(");
Parser parser = token.parser;
List filteredChildren = token.filteredChildren;
if(parser instanceof OrParser) {
build(builder, filteredChildren.get(1),tinyExpressionTokens);
builder.append("||");
build(builder, filteredChildren.get(2),tinyExpressionTokens);
}else if(parser instanceof AndParser) {
build(builder, filteredChildren.get(1),tinyExpressionTokens);
builder.append("&&");
build(builder, filteredChildren.get(2),tinyExpressionTokens);
}else if(parser instanceof XorParser) {
build(builder, filteredChildren.get(1),tinyExpressionTokens);
builder.append("^");
build(builder, filteredChildren.get(2),tinyExpressionTokens);
}else if(parser instanceof EqualEqualParser) {
build(builder, filteredChildren.get(1),tinyExpressionTokens);
builder.append("==");
build(builder, filteredChildren.get(2),tinyExpressionTokens);
}else if(parser instanceof NotEqualParser) {
build(builder, filteredChildren.get(1),tinyExpressionTokens);
builder.append("!=");
build(builder, filteredChildren.get(2),tinyExpressionTokens);
}else if(parser instanceof BooleanExpressionParser) {
filteredChildren.forEach(child->{
build(builder, child, tinyExpressionTokens);
});
}else {
BooleanBuilder.SINGLETON.build(builder, token , tinyExpressionTokens);
// }else {
// throw new IllegalArgumentException();
}
builder.append(")");
}
}