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