]> gerrit.simantics Code Review - simantics/platform.git/blob
8c861004cadbd2cb88fbddd1bacab331d311534d
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;
2
3
4 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
5 import org.simantics.scl.compiler.common.names.Names;
6 import org.simantics.scl.compiler.constants.DoubleConstant;
7 import org.simantics.scl.compiler.constants.FloatConstant;
8 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
12 import org.simantics.scl.compiler.errors.ErrorLog;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.TIntHashSet;
20
21 public class ERealLiteral extends SimplifiableExpression {
22     public String value;
23     public EVariable constraint;
24
25     public ERealLiteral(String value) {
26         this.value = value;
27     }
28
29     @Override
30     public void collectVars(TObjectIntHashMap<Variable> allVars,
31             TIntHashSet vars) {
32     }
33
34     private Expression tryToConvertToPrimitive(ErrorLog errorLog, Type requiredType) {
35         if(requiredType.equals(Types.DOUBLE))
36             return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
37         else if(requiredType.equals(Types.FLOAT))
38             return new ELiteral(new FloatConstant(Float.parseFloat(value)));
39         else if(requiredType.equals(Types.INTEGER)) {
40             errorLog.log(location, "Cannot convert real literal to Integer.");
41             return new EError();
42         }
43         else if(requiredType.equals(Types.LONG)) {
44             errorLog.log(location, "Cannot convert real literal to Long.");
45             return new EError();
46         }
47         else
48             return null;
49     }
50     
51     @Override
52     public Expression checkBasicType(TypingContext context, Type requiredType) {
53         requiredType = Types.canonical(requiredType);
54         Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), requiredType);
55         if(primitive != null)
56             return primitive;
57         else {
58             setType(requiredType);        
59             constraint = new EVariable(location, null);
60             constraint.setType(Types.pred(Types.REAL, requiredType));
61             context.addConstraintDemand(constraint);
62             return this;
63         }
64     }
65
66     @Override
67     protected void updateType() throws MatchException {
68         throw new InternalCompilerError("TODO");
69     }
70
71     @Override
72     public Expression simplify(SimplificationContext context) {
73         Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());
74         if(primitive != null)
75             return primitive;
76         return context.apply(
77                 context.getConstant(Names.Prelude_fromDouble, getType()),
78                 constraint.simplify(context),
79                 context.literal(new DoubleConstant(Double.parseDouble(value)))
80                 );
81     }
82
83     @Override
84     public Expression resolve(TranslationContext context) {
85         return this;
86     }
87     
88     @Override
89     public Expression resolveAsPattern(TranslationContext context) {
90         return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
91     }
92     
93     @Override
94     public Expression replace(ReplaceContext context) {
95         ERealLiteral copy = new ERealLiteral(value);
96         copy.setType(getType().replace(context.tvarMap));
97         return copy;
98     }
99     
100     @Override
101     public void setLocationDeep(long loc) {
102         if(location == Locations.NO_LOCATION) {
103             location = loc;
104             if(constraint != null)
105                 constraint.setLocationDeep(loc);
106         }
107     }
108     
109     @Override
110     public void accept(ExpressionVisitor visitor) {
111         visitor.visit(this);
112     }
113
114     public String getValue() {
115         return value;
116     }
117     
118     @Override
119     public Expression accept(ExpressionTransformer transformer) {
120         return transformer.transform(this);
121     }
122
123 }