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.internal.elaboration.utils.ExpressionDecorator;
16 import org.simantics.scl.compiler.types.Type;
17 import org.simantics.scl.compiler.types.Types;
18 import org.simantics.scl.compiler.types.exceptions.MatchException;
20 import gnu.trove.map.hash.TObjectIntHashMap;
21 import gnu.trove.set.hash.THashSet;
22 import gnu.trove.set.hash.TIntHashSet;
24 public class EIntegerLiteral extends SimplifiableExpression {
28 public EIntegerLiteral(String value) {
33 public void collectRefs(TObjectIntHashMap<Object> allRefs,
38 public void collectVars(TObjectIntHashMap<Variable> allVars,
42 public String getValue() {
46 private Expression tryToConvertToPrimitive(Type requiredType) {
47 if(requiredType.equals(Types.INTEGER))
48 return new ELiteral(new IntegerConstant(Integer.parseInt(value)));
49 else if(requiredType.equals(Types.DOUBLE))
50 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
51 else if(requiredType.equals(Types.FLOAT))
52 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
53 else if(requiredType.equals(Types.LONG))
54 return new ELiteral(new LongConstant(Long.parseLong(value)));
60 public Expression checkBasicType(TypingContext context, Type requiredType) {
61 requiredType = Types.canonical(requiredType);
64 Expression primitive = tryToConvertToPrimitive(requiredType);
67 } catch(NumberFormatException e) {
68 context.getErrorLog().log(getLocation(), "Invalid number format.");
71 setType(requiredType);
72 constraint = new EVariable(location, null);
73 constraint.setType(Types.pred(Types.RING, requiredType));
74 context.addConstraintDemand(constraint);
79 protected void updateType() throws MatchException {
80 throw new InternalCompilerError();
84 public void collectFreeVariables(THashSet<Variable> vars) {
88 public Expression simplify(SimplificationContext context) {
90 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
94 context.getConstant(Names.Prelude_fromInteger, getType()),
95 constraint.simplify(context),
96 context.literal(new IntegerConstant(Integer.parseInt(value)))
98 } catch(NumberFormatException e) {
99 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
105 public Expression resolve(TranslationContext context) {
110 public Expression resolveAsPattern(TranslationContext context) {
115 public Expression replace(ReplaceContext context) {
116 EIntegerLiteral copy = new EIntegerLiteral(value);
117 copy.setType(getType().replace(context.tvarMap));
118 copy.constraint = (EVariable)constraint.replace(context);
123 public Expression decorate(ExpressionDecorator decorator) {
124 return decorator.decorate(this);
128 public boolean isEffectful() {
133 public void collectEffects(THashSet<Type> effects) {
137 public void setLocationDeep(long loc) {
138 if(location == Locations.NO_LOCATION) {
140 if(constraint != null)
141 constraint.setLocationDeep(loc);
146 public void accept(ExpressionVisitor visitor) {
151 public void forVariables(VariableProcedure procedure) {
152 if(constraint != null)
153 constraint.forVariables(procedure);
157 public boolean isPattern(int arity) {
162 public Expression accept(ExpressionTransformer transformer) {
163 return transformer.transform(this);