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