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 collectVars(TObjectIntHashMap<Variable> allVars,
36 public String getValue() {
40 private Expression tryToConvertToPrimitive(Type requiredType) {
41 if(requiredType.equals(Types.INTEGER))
42 return new ELiteral(new IntegerConstant(Integer.parseInt(value)));
43 else if(requiredType.equals(Types.DOUBLE))
44 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
45 else if(requiredType.equals(Types.FLOAT))
46 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
47 else if(requiredType.equals(Types.LONG))
48 return new ELiteral(new LongConstant(Long.parseLong(value)));
54 public Expression checkBasicType(TypingContext context, Type requiredType) {
55 requiredType = Types.canonical(requiredType);
58 Expression primitive = tryToConvertToPrimitive(requiredType);
61 } catch(NumberFormatException e) {
62 context.getErrorLog().log(getLocation(), "Invalid number format.");
65 setType(requiredType);
66 constraint = new EVariable(location, null);
67 constraint.setType(Types.pred(Types.RING, requiredType));
68 context.addConstraintDemand(constraint);
73 protected void updateType() throws MatchException {
74 throw new InternalCompilerError();
78 public void collectFreeVariables(THashSet<Variable> vars) {
82 public Expression simplify(SimplificationContext context) {
84 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
88 context.getConstant(Names.Prelude_fromInteger, getType()),
89 constraint.simplify(context),
90 context.literal(new IntegerConstant(Integer.parseInt(value)))
92 } catch(NumberFormatException e) {
93 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
99 public Expression resolve(TranslationContext context) {
104 public Expression resolveAsPattern(TranslationContext context) {
109 public Expression replace(ReplaceContext context) {
110 EIntegerLiteral copy = new EIntegerLiteral(value);
111 copy.setType(getType().replace(context.tvarMap));
112 copy.constraint = (EVariable)constraint.replace(context);
117 public boolean isEffectful() {
122 public void collectEffects(THashSet<Type> effects) {
126 public void setLocationDeep(long loc) {
127 if(location == Locations.NO_LOCATION) {
129 if(constraint != null)
130 constraint.setLocationDeep(loc);
135 public void accept(ExpressionVisitor visitor) {
140 public boolean isPattern(int arity) {
145 public Expression accept(ExpressionTransformer transformer) {
146 return transformer.transform(this);