]> gerrit.simantics Code Review - simantics/platform.git/blob
7ccd66d0a3b8240fd25d70b37bb56bca94e07cf5
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;\r
2 \r
3 \r
4 import gnu.trove.map.hash.TObjectIntHashMap;\r
5 import gnu.trove.set.hash.THashSet;\r
6 import gnu.trove.set.hash.TIntHashSet;\r
7 \r
8 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
9 import org.simantics.scl.compiler.constants.DoubleConstant;\r
10 import org.simantics.scl.compiler.constants.FloatConstant;\r
11 import org.simantics.scl.compiler.constants.IntegerConstant;\r
12 import org.simantics.scl.compiler.constants.LongConstant;\r
13 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;\r
14 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;\r
15 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
16 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;\r
17 import org.simantics.scl.compiler.errors.Locations;\r
18 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
19 import org.simantics.scl.compiler.types.Type;\r
20 import org.simantics.scl.compiler.types.Types;\r
21 import org.simantics.scl.compiler.types.exceptions.MatchException;\r
22 \r
23 public class EIntegerLiteral extends SimplifiableExpression {\r
24     public String value;\r
25     EVariable constraint;\r
26 \r
27     public EIntegerLiteral(String value) {\r
28         this.value = value;\r
29     }\r
30 \r
31     @Override\r
32     public void collectRefs(TObjectIntHashMap<Object> allRefs,\r
33             TIntHashSet refs) {\r
34     }\r
35 \r
36     @Override\r
37     public void collectVars(TObjectIntHashMap<Variable> allVars,\r
38             TIntHashSet vars) {\r
39     }\r
40     \r
41     public String getValue() {\r
42         return value;\r
43     }\r
44     \r
45     private Expression tryToConvertToPrimitive(Type requiredType) {\r
46         if(requiredType.equals(Types.INTEGER))\r
47             return new ELiteral(new IntegerConstant(Integer.parseInt(value)));\r
48         else if(requiredType.equals(Types.DOUBLE))\r
49             return new ELiteral(new DoubleConstant(Double.parseDouble(value)));\r
50         else if(requiredType.equals(Types.FLOAT))\r
51             return new ELiteral(new FloatConstant(Float.parseFloat(value)));\r
52         else if(requiredType.equals(Types.LONG))\r
53             return new ELiteral(new LongConstant(Long.parseLong(value)));\r
54         else\r
55             return null;\r
56     }\r
57     \r
58     @Override\r
59     public Expression checkBasicType(TypingContext context, Type requiredType) {\r
60         requiredType = Types.canonical(requiredType);\r
61         \r
62         try {\r
63             Expression primitive = tryToConvertToPrimitive(requiredType);\r
64             if(primitive != null)\r
65                 return primitive;\r
66         } catch(NumberFormatException e) {\r
67             context.getErrorLog().log(getLocation(), "Invalid number format.");\r
68         }\r
69         \r
70         setType(requiredType);\r
71         constraint = new EVariable(location, null);\r
72         constraint.setType(Types.pred(Types.RING, requiredType));\r
73         context.addConstraintDemand(constraint);\r
74         return this;        \r
75     }\r
76     \r
77     @Override\r
78     protected void updateType() throws MatchException {\r
79         throw new InternalCompilerError();\r
80     }\r
81 \r
82     @Override\r
83     public void collectFreeVariables(THashSet<Variable> vars) {\r
84     }\r
85 \r
86     @Override\r
87     public Expression simplify(SimplificationContext context) {\r
88         try {\r
89             Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));\r
90             if(primitive != null)\r
91                 return primitive;\r
92             return context.apply(\r
93                     context.getConstant(SimplificationContext.FROM_INTEGER, getType()),\r
94                     constraint.simplify(context),\r
95                     context.literal(new IntegerConstant(Integer.parseInt(value)))\r
96                     );\r
97         } catch(NumberFormatException e) {\r
98             context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");\r
99             return this;\r
100         }\r
101     }\r
102 \r
103     @Override\r
104     public Expression resolve(TranslationContext context) {\r
105         return this;\r
106     }\r
107     \r
108     @Override\r
109     public Expression resolveAsPattern(TranslationContext context) {\r
110         return this;\r
111     }\r
112     \r
113     @Override\r
114     public Expression replace(ReplaceContext context) {\r
115         EIntegerLiteral copy = new EIntegerLiteral(value);\r
116         copy.setType(getType().replace(context.tvarMap));\r
117         copy.constraint = (EVariable)constraint.replace(context);\r
118         return copy;\r
119     }\r
120 \r
121     @Override\r
122     public Expression decorate(ExpressionDecorator decorator) {     \r
123         return decorator.decorate(this);\r
124     }\r
125     \r
126     @Override\r
127     public boolean isEffectful() {\r
128         return false;\r
129     }\r
130 \r
131     @Override\r
132     public void collectEffects(THashSet<Type> effects) {\r
133     }\r
134     \r
135     @Override\r
136     public void setLocationDeep(long loc) {\r
137         if(location == Locations.NO_LOCATION) {\r
138             location = loc;\r
139             if(constraint != null)\r
140                 constraint.setLocationDeep(loc);\r
141         }\r
142     }\r
143     \r
144     @Override\r
145     public void accept(ExpressionVisitor visitor) {\r
146         visitor.visit(this);\r
147     }\r
148 \r
149     @Override\r
150     public void forVariables(VariableProcedure procedure) {\r
151         if(constraint != null)\r
152             constraint.forVariables(procedure);\r
153     }\r
154     \r
155     @Override\r
156     public boolean isPattern(int arity) {\r
157         return arity == 0;\r
158     }\r
159     \r
160     @Override\r
161     public Expression accept(ExpressionTransformer transformer) {\r
162         return transformer.transform(this);\r
163     }\r
164 \r
165 }\r