1 package org.simantics.scl.compiler.elaboration.expressions;
\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
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
23 public class EIntegerLiteral extends SimplifiableExpression {
\r
24 public String value;
\r
25 EVariable constraint;
\r
27 public EIntegerLiteral(String value) {
\r
32 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
37 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
41 public String getValue() {
\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
59 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
60 requiredType = Types.canonical(requiredType);
\r
63 Expression primitive = tryToConvertToPrimitive(requiredType);
\r
64 if(primitive != null)
\r
66 } catch(NumberFormatException e) {
\r
67 context.getErrorLog().log(getLocation(), "Invalid number format.");
\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
78 protected void updateType() throws MatchException {
\r
79 throw new InternalCompilerError();
\r
83 public void collectFreeVariables(THashSet<Variable> vars) {
\r
87 public Expression simplify(SimplificationContext context) {
\r
89 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
\r
90 if(primitive != null)
\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
97 } catch(NumberFormatException e) {
\r
98 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
\r
104 public Expression resolve(TranslationContext context) {
\r
109 public Expression resolveAsPattern(TranslationContext context) {
\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
122 public Expression decorate(ExpressionDecorator decorator) {
\r
123 return decorator.decorate(this);
\r
127 public boolean isEffectful() {
\r
132 public void collectEffects(THashSet<Type> effects) {
\r
136 public void setLocationDeep(long loc) {
\r
137 if(location == Locations.NO_LOCATION) {
\r
139 if(constraint != null)
\r
140 constraint.setLocationDeep(loc);
\r
145 public void accept(ExpressionVisitor visitor) {
\r
146 visitor.visit(this);
\r
150 public void forVariables(VariableProcedure procedure) {
\r
151 if(constraint != null)
\r
152 constraint.forVariables(procedure);
\r
156 public boolean isPattern(int arity) {
\r
161 public Expression accept(ExpressionTransformer transformer) {
\r
162 return transformer.transform(this);
\r