1 package org.simantics.scl.compiler.elaboration.expressions;
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.constants.IntegerConstant;
9 import org.simantics.scl.compiler.constants.LongConstant;
10 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
11 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
13 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
14 import org.simantics.scl.compiler.errors.Locations;
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;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class EIntegerLiteral extends SimplifiableExpression {
27 public EIntegerLiteral(String value) {
32 public void collectRefs(TObjectIntHashMap<Object> allRefs,
37 public void collectVars(TObjectIntHashMap<Variable> allVars,
41 public String getValue() {
45 private Expression tryToConvertToPrimitive(Type requiredType) {
46 if(requiredType.equals(Types.INTEGER))
47 return new ELiteral(new IntegerConstant(Integer.parseInt(value)));
48 else if(requiredType.equals(Types.DOUBLE))
49 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
50 else if(requiredType.equals(Types.FLOAT))
51 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
52 else if(requiredType.equals(Types.LONG))
53 return new ELiteral(new LongConstant(Long.parseLong(value)));
59 public Expression checkBasicType(TypingContext context, Type requiredType) {
60 requiredType = Types.canonical(requiredType);
63 Expression primitive = tryToConvertToPrimitive(requiredType);
66 } catch(NumberFormatException e) {
67 context.getErrorLog().log(getLocation(), "Invalid number format.");
70 setType(requiredType);
71 constraint = new EVariable(location, null);
72 constraint.setType(Types.pred(Types.RING, requiredType));
73 context.addConstraintDemand(constraint);
78 protected void updateType() throws MatchException {
79 throw new InternalCompilerError();
83 public void collectFreeVariables(THashSet<Variable> vars) {
87 public Expression simplify(SimplificationContext context) {
89 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
93 context.getConstant(Names.Prelude_fromInteger, getType()),
94 constraint.simplify(context),
95 context.literal(new IntegerConstant(Integer.parseInt(value)))
97 } catch(NumberFormatException e) {
98 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
104 public Expression resolve(TranslationContext context) {
109 public Expression resolveAsPattern(TranslationContext context) {
114 public Expression replace(ReplaceContext context) {
115 EIntegerLiteral copy = new EIntegerLiteral(value);
116 copy.setType(getType().replace(context.tvarMap));
117 copy.constraint = (EVariable)constraint.replace(context);
122 public boolean isEffectful() {
127 public void collectEffects(THashSet<Type> effects) {
131 public void setLocationDeep(long loc) {
132 if(location == Locations.NO_LOCATION) {
134 if(constraint != null)
135 constraint.setLocationDeep(loc);
140 public void accept(ExpressionVisitor visitor) {
145 public boolean isPattern(int arity) {
150 public Expression accept(ExpressionTransformer transformer) {
151 return transformer.transform(this);