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.TIntHashSet;
22 public class EIntegerLiteral extends SimplifiableExpression {
24 public EVariable constraint;
26 public EIntegerLiteral(String value) {
31 public void collectVars(TObjectIntHashMap<Variable> allVars,
35 public String getValue() {
39 private Expression tryToConvertToPrimitive(Type requiredType) {
40 if(requiredType.equals(Types.INTEGER))
41 return new ELiteral(new IntegerConstant(Integer.parseInt(value)));
42 else if(requiredType.equals(Types.DOUBLE))
43 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
44 else if(requiredType.equals(Types.FLOAT))
45 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
46 else if(requiredType.equals(Types.LONG))
47 return new ELiteral(new LongConstant(Long.parseLong(value)));
53 public Expression checkBasicType(TypingContext context, Type requiredType) {
54 requiredType = Types.canonical(requiredType);
57 Expression primitive = tryToConvertToPrimitive(requiredType);
60 } catch(NumberFormatException e) {
61 context.getErrorLog().log(getLocation(), "Invalid number format.");
64 setType(requiredType);
65 constraint = new EVariable(location, null);
66 constraint.setType(Types.pred(Types.RING, requiredType));
67 context.addConstraintDemand(constraint);
72 protected void updateType() throws MatchException {
73 throw new InternalCompilerError();
77 public Expression simplify(SimplificationContext context) {
79 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
83 context.getConstant(Names.Prelude_fromInteger, getType()),
84 constraint.simplify(context),
85 context.literal(new IntegerConstant(Integer.parseInt(value)))
87 } catch(NumberFormatException e) {
88 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
94 public Expression resolve(TranslationContext context) {
99 public Expression resolveAsPattern(TranslationContext context) {
104 public Expression replace(ReplaceContext context) {
105 EIntegerLiteral copy = new EIntegerLiteral(value);
106 copy.setType(getType().replace(context.tvarMap));
107 copy.constraint = (EVariable)constraint.replace(context);
112 public boolean isEffectful() {
117 public void setLocationDeep(long loc) {
118 if(location == Locations.NO_LOCATION) {
120 if(constraint != null)
121 constraint.setLocationDeep(loc);
126 public void accept(ExpressionVisitor visitor) {
131 public boolean isPattern(int arity) {
136 public Expression accept(ExpressionTransformer transformer) {
137 return transformer.transform(this);