1 package org.simantics.scl.compiler.elaboration.expressions;
\r
4 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
5 import org.simantics.scl.compiler.constants.DoubleConstant;
\r
6 import org.simantics.scl.compiler.constants.FloatConstant;
\r
7 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
8 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
9 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
10 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
11 import org.simantics.scl.compiler.errors.ErrorLog;
\r
12 import org.simantics.scl.compiler.errors.Locations;
\r
13 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
14 import org.simantics.scl.compiler.types.Type;
\r
15 import org.simantics.scl.compiler.types.Types;
\r
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
18 import gnu.trove.map.hash.TObjectIntHashMap;
\r
19 import gnu.trove.set.hash.THashSet;
\r
20 import gnu.trove.set.hash.TIntHashSet;
\r
22 public class ERealLiteral extends SimplifiableExpression {
\r
23 public String value;
\r
24 EVariable constraint;
\r
26 public ERealLiteral(String value) {
\r
31 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
36 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
40 private Expression tryToConvertToPrimitive(ErrorLog errorLog, Type requiredType) {
\r
41 if(requiredType.equals(Types.DOUBLE))
\r
42 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
\r
43 else if(requiredType.equals(Types.FLOAT))
\r
44 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
\r
45 else if(requiredType.equals(Types.INTEGER)) {
\r
46 errorLog.log(location, "Cannot convert real literal to Integer.");
\r
47 return new EError();
\r
49 else if(requiredType.equals(Types.LONG)) {
\r
50 errorLog.log(location, "Cannot convert real literal to Long.");
\r
51 return new EError();
\r
58 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
59 requiredType = Types.canonical(requiredType);
\r
60 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), requiredType);
\r
61 if(primitive != null)
\r
64 setType(requiredType);
\r
65 constraint = new EVariable(location, null);
\r
66 constraint.setType(Types.pred(Types.REAL, requiredType));
\r
67 context.addConstraintDemand(constraint);
\r
73 protected void updateType() throws MatchException {
\r
74 throw new InternalCompilerError("TODO");
\r
78 public void collectFreeVariables(THashSet<Variable> vars) {
\r
82 public Expression simplify(SimplificationContext context) {
\r
83 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());
\r
84 if(primitive != null)
\r
86 return context.apply(
\r
87 context.getConstant(SimplificationContext.FROM_DOUBLE, getType()),
\r
88 constraint.simplify(context),
\r
89 context.literal(new DoubleConstant(Double.parseDouble(value)))
\r
94 public Expression resolve(TranslationContext context) {
\r
99 public Expression resolveAsPattern(TranslationContext context) {
\r
100 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
\r
104 public Expression replace(ReplaceContext context) {
\r
105 ERealLiteral copy = new ERealLiteral(value);
\r
106 copy.setType(getType().replace(context.tvarMap));
\r
111 public Expression decorate(ExpressionDecorator decorator) {
\r
112 return decorator.decorate(this);
\r
116 public void collectEffects(THashSet<Type> effects) {
\r
120 public void setLocationDeep(long loc) {
\r
121 if(location == Locations.NO_LOCATION) {
\r
123 if(constraint != null)
\r
124 constraint.setLocationDeep(loc);
\r
129 public void accept(ExpressionVisitor visitor) {
\r
130 visitor.visit(this);
\r
133 public String getValue() {
\r
138 public void forVariables(VariableProcedure procedure) {
\r
139 if(constraint != null)
\r
140 constraint.forVariables(procedure);
\r
144 public Expression accept(ExpressionTransformer transformer) {
\r
145 return transformer.transform(this);
\r