com.github.bloodshura.x.utilities.eval.compiler.Compiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shurax-utilities Show documentation
Show all versions of shurax-utilities Show documentation
A collection of general utilities.
The newest version!
/*
* Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
* https://www.github.com/BloodShura
*/
package com.github.bloodshura.x.utilities.eval.compiler;
import com.github.bloodshura.x.utilities.eval.enumeration.Operator;
import com.github.bloodshura.x.utilities.eval.exception.EvaluationException;
import com.github.bloodshura.x.utilities.eval.operation.Operation;
import com.github.bloodshura.x.utilities.eval.operation.OperationFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Compiler {
@Nonnull
public static Operation compile(@Nonnull Tokenizer tokenizer) throws EvaluationException {
Object expression = compile(tokenizer, null, null, 0, (char) 0, -1);
if (expression instanceof Operation) {
return (Operation) expression;
}
return OperationFactory.createNop(expression);
}
@Nonnull
protected static Object compile(@Nonnull Tokenizer tokenizer, @Nullable Object preReadOperand, @Nullable Operator preReadOperator, int nestingLevel, char endOfExpressionChar, int terminatePrecedence) throws EvaluationException {
Object operand = preReadOperand != null ? preReadOperand : getOperand(tokenizer, nestingLevel);
Operator operator = preReadOperator != null ? preReadOperator : tokenizer.getOperator(endOfExpressionChar);
while (operator != Operator.END) {
if (operator == Operator.TERNARY) {
Object operand2 = compile(tokenizer, null, null, nestingLevel, ':', -1);
Object operand3 = compile(tokenizer, null, null, nestingLevel, endOfExpressionChar, -1);
operand = OperationFactory.createTernaryValue(operator, operand, operand2, operand3);
operator = Operator.END;
}
else {
Object nextOperand = getOperand(tokenizer, nestingLevel);
Operator nextOperator = tokenizer.getOperator(endOfExpressionChar);
if (nextOperator == Operator.END) {
operand = OperationFactory.createBinaryValue(operator, operand, nextOperand);
operator = Operator.END;
if (preReadOperator != null && endOfExpressionChar != 0) {
tokenizer.pushBack(Operator.END);
}
}
else if (nextOperator.getPrecedence() <= terminatePrecedence) {
operand = OperationFactory.createBinaryValue(operator, operand, nextOperand);
tokenizer.pushBack(nextOperator);
operator = Operator.END;
}
else if (operator.getPrecedence() >= nextOperator.getPrecedence()) {
operand = OperationFactory.createBinaryValue(operator, operand, nextOperand);
operator = nextOperator;
}
else {
operand = OperationFactory.createBinaryValue(operator, operand, compile(tokenizer, nextOperand, nextOperator, nestingLevel, endOfExpressionChar, operator.getPrecedence()));
operator = tokenizer.getOperator(endOfExpressionChar);
if (operator == Operator.END && preReadOperator != null && endOfExpressionChar != 0) {
tokenizer.pushBack(Operator.END);
}
}
}
}
return operand;
}
@Nonnull
protected static Object getOperand(@Nonnull Tokenizer tokenizer, int nestingLevel) throws EvaluationException {
Object operand = tokenizer.getOperand();
if (operand.equals(Tokenizer.START_EXPRESSION)) {
return compile(tokenizer, null, null, nestingLevel + 1, Tokenizer.END_EXPRESSION, -1);
}
if (operand instanceof Operator) {
return OperationFactory.createUnaryValue((Operator) operand, getOperand(tokenizer, nestingLevel));
}
return operand;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy