org.snapscript.tree.Cast Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree;
import org.snapscript.core.Context;
import org.snapscript.core.Evaluation;
import org.snapscript.core.Module;
import org.snapscript.core.Scope;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.convert.ConstraintConverter;
import org.snapscript.core.convert.ConstraintMatcher;
import org.snapscript.tree.constraint.Constraint;
import org.snapscript.tree.constraint.ConstraintReference;
public class Cast extends Evaluation {
private final ConstraintReference reference;
private final Evaluation evaluation;
public Cast(Evaluation evaluation, Constraint constraint) {
this.reference = new ConstraintReference(constraint);
this.evaluation = evaluation;
}
@Override
public void compile(Scope scope) throws Exception {
evaluation.compile(scope);
}
@Override
public Value evaluate(Scope scope, Object left) throws Exception {
Value value = evaluation.evaluate(scope, left);
Type type = reference.getConstraint(scope);
Object object = value.getValue();
Module module = scope.getModule();
Context context = module.getContext();
ConstraintMatcher matcher = context.getMatcher();
ConstraintConverter converter = matcher.match(type);
Object result = converter.convert(object);
return Value.getTransient(result, type);
}
}