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 public class EIntegerLiteral extends SimplifiableExpression {
21 public EVariable constraint;
23 public EIntegerLiteral(String value) {
27 public String getValue() {
31 private Expression tryToConvertToPrimitive(Type requiredType) {
32 if(requiredType.equals(Types.INTEGER))
33 return new ELiteral(new IntegerConstant(Integer.parseInt(value)));
34 else if(requiredType.equals(Types.DOUBLE))
35 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
36 else if(requiredType.equals(Types.FLOAT))
37 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
38 else if(requiredType.equals(Types.LONG))
39 return new ELiteral(new LongConstant(Long.parseLong(value)));
45 public Expression checkBasicType(TypingContext context, Type requiredType) {
46 requiredType = Types.canonical(requiredType);
49 Expression primitive = tryToConvertToPrimitive(requiredType);
52 } catch(NumberFormatException e) {
53 context.getErrorLog().log(getLocation(), "Invalid number format.");
56 setType(requiredType);
57 constraint = new EVariable(location, null);
58 constraint.setType(Types.pred(Types.RING, requiredType));
59 context.addConstraintDemand(constraint);
64 protected void updateType() throws MatchException {
65 throw new InternalCompilerError();
69 public Expression simplify(SimplificationContext context) {
71 Expression primitive = tryToConvertToPrimitive(Types.canonical(getType()));
75 context.getConstant(Names.Prelude_fromInteger, getType()),
76 constraint.simplify(context),
77 context.literal(new IntegerConstant(Integer.parseInt(value)))
79 } catch(NumberFormatException e) {
80 context.getErrorLog().log(getLocation(), "Invalid number format (maybe too long for the expected number type).");
86 public Expression resolve(TranslationContext context) {
91 public Expression resolveAsPattern(TranslationContext context) {
96 public Expression replace(ReplaceContext context) {
97 EIntegerLiteral copy = new EIntegerLiteral(value);
98 copy.setType(getType().replace(context.tvarMap));
99 copy.constraint = (EVariable)constraint.replace(context);
104 public boolean isEffectful() {
109 public void setLocationDeep(long loc) {
110 if(location == Locations.NO_LOCATION) {
112 if(constraint != null)
113 constraint.setLocationDeep(loc);
118 public void accept(ExpressionVisitor visitor) {
123 public boolean isPattern(int arity) {
128 public Expression accept(ExpressionTransformer transformer) {
129 return transformer.transform(this);