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

prompto.parser.Assertion Maven / Gradle / Ivy

package prompto.parser;

import prompto.compiler.Flags;
import prompto.compiler.MethodInfo;
import prompto.declaration.TestMethodDeclaration;
import prompto.error.PromptoError;
import prompto.expression.EqualsExpression;
import prompto.expression.IAssertion;
import prompto.expression.IExpression;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.BooleanType;
import prompto.type.IType;
import prompto.utils.CodeWriter;

/* the purpose of this class is simply to link an expression with a section */
public class Assertion extends CodeSection {

	IExpression expression;
	
	public Assertion(IExpression expression) {
		this.expression = expression;
	}

	public void toDialect(CodeWriter writer) {
		expression.toDialect(writer);
	}

	public Context check(Context context) {
		IType type = expression.check(context);
		if(type!=BooleanType.instance())
			context.getProblemListener().reportIllegalAssignment(this, BooleanType.instance(), type);
		// need to optionally auto-downcast
		if(expression instanceof EqualsExpression) 
			context = ((EqualsExpression)expression).downcastForCheck(context);
		return context;
	}

	public boolean interpret(Context context, TestMethodDeclaration test) throws PromptoError {
		return ((IAssertion)expression).interpretAssert(context, test);
	}

	public void compile(Context context, MethodInfo method, Flags flags, TestMethodDeclaration test) {
		((IAssertion)expression).compileAssert(context, method, flags, test);
	}

	public void declare(Transpiler transpiler) {
		expression.declare(transpiler);
	}

	public void transpile(Transpiler transpiler) {
		expression.transpile(transpiler);
	}

	public String getExpected(Context context, Dialect dialect, int escapeMode) {
		CodeWriter writer = new CodeWriter(dialect, context, escapeMode);
		expression.toDialect(writer);
		return writer.toString();
	}

	public void transpileFound(Transpiler transpiler, Dialect dialect) {
		expression.transpileFound(transpiler, dialect);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy