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

prompto.expression.MultiplyExpression Maven / Gradle / Ivy

The newest version!
package prompto.expression;

import java.util.HashMap;
import java.util.Map;

import prompto.compiler.CompilerUtils;
import prompto.compiler.Flags;
import prompto.compiler.IOperand;
import prompto.compiler.IOperatorFunction;
import prompto.compiler.MethodConstant;
import prompto.compiler.MethodInfo;
import prompto.compiler.Opcode;
import prompto.compiler.ResultInfo;
import prompto.declaration.CategoryDeclaration;
import prompto.error.PromptoError;
import prompto.error.SyntaxError;
import prompto.parser.CodeSection;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.CharacterType;
import prompto.type.DecimalType;
import prompto.type.IType;
import prompto.type.IntegerType;
import prompto.type.TextType;
import prompto.utils.CodeWriter;
import prompto.value.IMultiplyable;
import prompto.value.IValue;

public class MultiplyExpression extends CodeSection implements IExpression {

	IExpression left;
	IExpression right;
	
	public MultiplyExpression(IExpression left, IExpression right) {
		this.left = left;
		this.right = right;
	}
	
	@Override
	public void toDialect(CodeWriter writer) {
		left.toDialect(writer);
		writer.append(" * ");
		right.toDialect(writer);
	}
	
	@Override
	public IType check(Context context) {
		IType lt = left.check(context);
		IType rt = right.check(context);
		return lt.checkMultiply(context, rt, true, this);
	}
	
	@Override
	public IValue interpret(Context context) throws PromptoError {
		IValue lval = left.interpret(context);
		IValue rval = right.interpret(context);
        return lval.multiply(context, rval);
	}

	static Map, IOperatorFunction> multipliers = createMultipliers();
	
	private static Map, IOperatorFunction> createMultipliers() {
		Map, IOperatorFunction> map = new HashMap<>(); 
		map.put(String.class, TextType::compileMultiply); 
		map.put(java.lang.Character.class, CharacterType::compileMultiply); 
		map.put(double.class, DecimalType::compileMultiply);
		map.put(Double.class, DecimalType::compileMultiply); 
		map.put(long.class, IntegerType::compileMultiply);
		map.put(Long.class, IntegerType::compileMultiply); 
		return map;
	}

	@Override
	public ResultInfo compile(Context context, MethodInfo method, Flags flags) {
		ResultInfo lval = left.compile(context, method, flags);
		IOperatorFunction multiplier = multipliers.get(lval.getType());
		if(multiplier==null && lval.getType().getTypeName().startsWith("π.χ."))
			multiplier = CategoryDeclaration::compileMultiply;
		if(multiplier!=null)
			return multiplier.compile(context, method, flags, lval, right);
		else if(IMultiplyable.class.isAssignableFrom((Class)lval.getType())) // TODO for now
			return compileMultiplyable(context, method, lval, flags);
		else {
			System.err.println("Missing IOperatorFunction for multiply " + lval.getType().getTypeName());
			throw new SyntaxError("Cannot multiply " + lval.getType().getTypeName() + " with " + right.check(context).getFamilyInfo(context));
		}
	}

	private ResultInfo compileMultiplyable(Context context, MethodInfo method, ResultInfo lval, Flags flags) {
		try {
			ResultInfo rval = right.compile(context, method, flags);
			// for now we only support multiply by Integer
			if(Long.class==rval.getType())
				CompilerUtils.LongToint(method);
			else
				CompilerUtils.longToint(method);
			Class klass = (Class)lval.getType();
			Class resultType = klass.getMethod("multiply", int.class).getReturnType();
			IOperand oper = new MethodConstant(lval.getType(), "multiply", 
					int.class, resultType);
			method.addInstruction(Opcode.INVOKEVIRTUAL, oper);
			return new ResultInfo(resultType);
		} catch(NoSuchMethodException e) {
			throw new SyntaxError(e.getMessage());
		}
	}
	
	@Override
	public void declare(Transpiler transpiler) {
	    IType lt = this.left.check(transpiler.getContext());
	    IType rt = this.right.check(transpiler.getContext());
	    lt.declareMultiply(transpiler, rt, true, this.left, this.right, this);
	}
	
	@Override
	public boolean transpile(Transpiler transpiler) {
		IType lt = this.left.check(transpiler.getContext());
		IType rt = this.right.check(transpiler.getContext());
	    lt.transpileMultiply(transpiler, rt, true, this.left, this.right);
	    return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy