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

com.g2forge.alexandria.expression.IExpression Maven / Gradle / Ivy

Go to download

A library for expressions and their evaluation. Includes a basic implementation for math.

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.expression;

public interface IExpression, N extends IEnvironment> {
	public static , V extends IVariable, N extends IEnvironment> L eval(IExpression _this, Class type) {
		if (type != null) {
			if (type.isInstance(_this)) return type.cast(_this);
		} else {
			if (_this instanceof ILiteral) {
				@SuppressWarnings("unchecked")
				final L cast = (L) _this;
				return cast;
			}
		}
		final IExpression reduced = _this.reduce();
		if (reduced == _this) throw new ExpressionNotEvaluableException(_this, type);
		return reduced.eval(type);
	}

	/**
	 * Apply the given environment to this expression. May either be eager or lazy, at the implementors discretion.
	 * 
	 * @param environment The environment to apply to this expression.
	 * @return An expression equivalent to this one, with any variables present in the environment substituted for their values.
	 */
	public IExpression apply(N environment);

	/**
	 * Evaluate this expression completely to a literal value.
	 * 
	 * @param  The (static) type of the literal result.
	 * @param type The type of the literal result to be expected (and required) from this evaluation.
	 * @return A literal expression resulting from the evaluation.
	 * @throws ExpressionNotEvaluableException if this expression cannot be completely evaluated.
	 */
	public default > L eval(Class type) throws ExpressionNotEvaluableException {
		return eval(this, type);
	}

	/**
	 * Eagerly reduce this expression.
	 * 
	 * @return A reduced version of this expression.
	 */
	public IExpression reduce();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy