]> gerrit.simantics Code Review - simantics/platform.git/blob
f2e17aa00cf6b78f418d993e6615149acb171ff3
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;\r
2 \r
3 \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
17 \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
21 \r
22 public class ERealLiteral extends SimplifiableExpression {\r
23     public String value;\r
24     EVariable constraint;\r
25 \r
26     public ERealLiteral(String value) {\r
27         this.value = value;\r
28     }\r
29 \r
30     @Override\r
31     public void collectRefs(TObjectIntHashMap<Object> allRefs,\r
32             TIntHashSet refs) {\r
33     }\r
34 \r
35     @Override\r
36     public void collectVars(TObjectIntHashMap<Variable> allVars,\r
37             TIntHashSet vars) {\r
38     }\r
39 \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
48         }\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
52         }\r
53         else\r
54             return null;\r
55     }\r
56     \r
57     @Override\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
62             return primitive;\r
63         else {\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
68             return this;\r
69         }\r
70     }\r
71 \r
72     @Override\r
73     protected void updateType() throws MatchException {\r
74         throw new InternalCompilerError("TODO");\r
75     }\r
76     \r
77     @Override\r
78     public void collectFreeVariables(THashSet<Variable> vars) {\r
79     }\r
80 \r
81     @Override\r
82     public Expression simplify(SimplificationContext context) {\r
83         Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());\r
84         if(primitive != null)\r
85             return primitive;\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
90                 );\r
91     }\r
92 \r
93     @Override\r
94     public Expression resolve(TranslationContext context) {\r
95         return this;\r
96     }\r
97     \r
98     @Override\r
99     public Expression resolveAsPattern(TranslationContext context) {\r
100         return new ELiteral(new DoubleConstant(Double.parseDouble(value)));\r
101     }\r
102     \r
103     @Override\r
104     public Expression replace(ReplaceContext context) {\r
105         ERealLiteral copy = new ERealLiteral(value);\r
106         copy.setType(getType().replace(context.tvarMap));\r
107         return copy;\r
108     }\r
109     \r
110     @Override\r
111     public Expression decorate(ExpressionDecorator decorator) {     \r
112         return decorator.decorate(this);\r
113     }\r
114 \r
115     @Override\r
116     public void collectEffects(THashSet<Type> effects) {\r
117     }\r
118     \r
119     @Override\r
120     public void setLocationDeep(long loc) {\r
121         if(location == Locations.NO_LOCATION) {\r
122             location = loc;\r
123             if(constraint != null)\r
124                 constraint.setLocationDeep(loc);\r
125         }\r
126     }\r
127     \r
128     @Override\r
129     public void accept(ExpressionVisitor visitor) {\r
130         visitor.visit(this);\r
131     }\r
132 \r
133     public String getValue() {\r
134         return value;\r
135     }\r
136 \r
137     @Override\r
138     public void forVariables(VariableProcedure procedure) {\r
139         if(constraint != null)\r
140             constraint.forVariables(procedure);\r
141     }\r
142     \r
143     @Override\r
144     public Expression accept(ExpressionTransformer transformer) {\r
145         return transformer.transform(this);\r
146     }\r
147 \r
148 }\r