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