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.elaboration.contexts.ReplaceContext;
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
12 import org.simantics.scl.compiler.errors.ErrorLog;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
18 public class ERealLiteral extends SimplifiableExpression {
20 public EVariable constraint;
22 public ERealLiteral(String value) {
26 private Expression tryToConvertToPrimitive(ErrorLog errorLog, Type requiredType) {
27 if(requiredType.equals(Types.DOUBLE))
28 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
29 else if(requiredType.equals(Types.FLOAT))
30 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
31 else if(requiredType.equals(Types.INTEGER)) {
32 errorLog.log(location, "Cannot convert real literal to Integer.");
35 else if(requiredType.equals(Types.LONG)) {
36 errorLog.log(location, "Cannot convert real literal to Long.");
44 public Expression checkBasicType(TypingContext context, Type requiredType) {
45 requiredType = Types.canonical(requiredType);
46 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), requiredType);
50 setType(requiredType);
51 constraint = new EVariable(location, null);
52 constraint.setType(Types.pred(Types.REAL, requiredType));
53 context.addConstraintDemand(constraint);
59 protected void updateType() throws MatchException {
60 throw new InternalCompilerError("TODO");
64 public Expression simplify(SimplificationContext context) {
65 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());
69 context.getConstant(Names.Prelude_fromDouble, getType()),
70 constraint.simplify(context),
71 context.literal(new DoubleConstant(Double.parseDouble(value)))
76 public Expression resolve(TranslationContext context) {
81 public Expression resolveAsPattern(TranslationContext context) {
82 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
86 public Expression replace(ReplaceContext context) {
87 ERealLiteral copy = new ERealLiteral(value);
88 copy.setType(getType().replace(context.tvarMap));
93 public void setLocationDeep(long loc) {
94 if(location == Locations.NO_LOCATION) {
96 if(constraint != null)
97 constraint.setLocationDeep(loc);
102 public void accept(ExpressionVisitor visitor) {
106 public String getValue() {
111 public Expression accept(ExpressionTransformer transformer) {
112 return transformer.transform(this);