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.internal.elaboration.utils.ExpressionDecorator;
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 ERealLiteral extends SimplifiableExpression {
27 public ERealLiteral(String value) {
32 public void collectRefs(TObjectIntHashMap<Object> allRefs,
37 public void collectVars(TObjectIntHashMap<Variable> allVars,
41 private Expression tryToConvertToPrimitive(ErrorLog errorLog, Type requiredType) {
42 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.INTEGER)) {
47 errorLog.log(location, "Cannot convert real literal to Integer.");
50 else if(requiredType.equals(Types.LONG)) {
51 errorLog.log(location, "Cannot convert real literal to Long.");
59 public Expression checkBasicType(TypingContext context, Type requiredType) {
60 requiredType = Types.canonical(requiredType);
61 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), requiredType);
65 setType(requiredType);
66 constraint = new EVariable(location, null);
67 constraint.setType(Types.pred(Types.REAL, requiredType));
68 context.addConstraintDemand(constraint);
74 protected void updateType() throws MatchException {
75 throw new InternalCompilerError("TODO");
79 public void collectFreeVariables(THashSet<Variable> vars) {
83 public Expression simplify(SimplificationContext context) {
84 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());
88 context.getConstant(Names.Prelude_fromDouble, getType()),
89 constraint.simplify(context),
90 context.literal(new DoubleConstant(Double.parseDouble(value)))
95 public Expression resolve(TranslationContext context) {
100 public Expression resolveAsPattern(TranslationContext context) {
101 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
105 public Expression replace(ReplaceContext context) {
106 ERealLiteral copy = new ERealLiteral(value);
107 copy.setType(getType().replace(context.tvarMap));
112 public Expression decorate(ExpressionDecorator decorator) {
113 return decorator.decorate(this);
117 public void collectEffects(THashSet<Type> effects) {
121 public void setLocationDeep(long loc) {
122 if(location == Locations.NO_LOCATION) {
124 if(constraint != null)
125 constraint.setLocationDeep(loc);
130 public void accept(ExpressionVisitor visitor) {
134 public String getValue() {
139 public void forVariables(VariableProcedure procedure) {
140 if(constraint != null)
141 constraint.forVariables(procedure);
145 public Expression accept(ExpressionTransformer transformer) {
146 return transformer.transform(this);