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 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class ERealLiteral extends SimplifiableExpression {
26 public ERealLiteral(String value) {
31 public void collectRefs(TObjectIntHashMap<Object> allRefs,
36 public void collectVars(TObjectIntHashMap<Variable> allVars,
40 private Expression tryToConvertToPrimitive(ErrorLog errorLog, Type requiredType) {
41 if(requiredType.equals(Types.DOUBLE))
42 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
43 else if(requiredType.equals(Types.FLOAT))
44 return new ELiteral(new FloatConstant(Float.parseFloat(value)));
45 else if(requiredType.equals(Types.INTEGER)) {
46 errorLog.log(location, "Cannot convert real literal to Integer.");
49 else if(requiredType.equals(Types.LONG)) {
50 errorLog.log(location, "Cannot convert real literal to Long.");
58 public Expression checkBasicType(TypingContext context, Type requiredType) {
59 requiredType = Types.canonical(requiredType);
60 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), requiredType);
64 setType(requiredType);
65 constraint = new EVariable(location, null);
66 constraint.setType(Types.pred(Types.REAL, requiredType));
67 context.addConstraintDemand(constraint);
73 protected void updateType() throws MatchException {
74 throw new InternalCompilerError("TODO");
78 public void collectFreeVariables(THashSet<Variable> vars) {
82 public Expression simplify(SimplificationContext context) {
83 Expression primitive = tryToConvertToPrimitive(context.getErrorLog(), getType());
87 context.getConstant(Names.Prelude_fromDouble, getType()),
88 constraint.simplify(context),
89 context.literal(new DoubleConstant(Double.parseDouble(value)))
94 public Expression resolve(TranslationContext context) {
99 public Expression resolveAsPattern(TranslationContext context) {
100 return new ELiteral(new DoubleConstant(Double.parseDouble(value)));
104 public Expression replace(ReplaceContext context) {
105 ERealLiteral copy = new ERealLiteral(value);
106 copy.setType(getType().replace(context.tvarMap));
111 public void collectEffects(THashSet<Type> effects) {
115 public void setLocationDeep(long loc) {
116 if(location == Locations.NO_LOCATION) {
118 if(constraint != null)
119 constraint.setLocationDeep(loc);
124 public void accept(ExpressionVisitor visitor) {
128 public String getValue() {
133 public Expression accept(ExpressionTransformer transformer) {
134 return transformer.transform(this);