diff --git a/interpreter/src/main/java/org/intocps/maestro/interpreter/values/FloatValue.java b/interpreter/src/main/java/org/intocps/maestro/interpreter/values/FloatValue.java index 1fdcde66..3094f6c7 100644 --- a/interpreter/src/main/java/org/intocps/maestro/interpreter/values/FloatValue.java +++ b/interpreter/src/main/java/org/intocps/maestro/interpreter/values/FloatValue.java @@ -68,6 +68,10 @@ public int compareTo(Value value) { FloatValue ro = (FloatValue) other; return (int) Math.round(Math.signum(this.value - ro.getValue())); + } if (other instanceof RealValue) { + RealValue ro = (RealValue) other; + return (int) Math.round(Math.signum(this.value - ro.getValue())); + } else if (other instanceof IntegerValue) { IntegerValue ro = (IntegerValue) other; return (int) Math.round(Math.signum(this.value - ro.getValue())); diff --git a/maestro/src/test/java/org/intocps/maestro/FloatDeclTest.java b/maestro/src/test/java/org/intocps/maestro/FloatDeclTest.java new file mode 100644 index 00000000..d962c16d --- /dev/null +++ b/maestro/src/test/java/org/intocps/maestro/FloatDeclTest.java @@ -0,0 +1,69 @@ +package org.intocps.maestro; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.intocps.maestro.ast.AVariableDeclaration; +import org.intocps.maestro.ast.LexIdentifier; +import org.intocps.maestro.ast.analysis.AnalysisException; +import org.intocps.maestro.ast.display.PrettyPrinter; +import org.intocps.maestro.ast.node.AExpInitializer; +import org.intocps.maestro.ast.node.AFloatNumericPrimitiveType; +import org.intocps.maestro.ast.node.AIdentifierExp; +import org.intocps.maestro.ast.node.ALocalVariableStm; +import org.intocps.maestro.framework.fmi2.api.mabl.BaseApiTest; +import org.intocps.maestro.framework.fmi2.api.mabl.MablApiBuilder; +import org.intocps.maestro.framework.fmi2.api.mabl.scoping.DynamicActiveBuilderScope; +import org.intocps.maestro.framework.fmi2.api.mabl.variables.BooleanVariableFmi2Api; +import org.intocps.maestro.framework.fmi2.api.mabl.variables.DoubleVariableFmi2Api; +import org.intocps.maestro.framework.fmi2.api.mabl.variables.FloatVariableFmi2Api; +import org.intocps.maestro.framework.fmi2.api.mabl.variables.VariableFmi2Api; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + + +public class FloatDeclTest extends BaseApiTest { + protected FloatDeclTest() { + super(Assertions::assertTrue, Assertions::assertFalse); + } + + @Test + public void test() throws AnalysisException, IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + MablApiBuilder.MablSettings settings = new MablApiBuilder.MablSettings(); + settings.fmiErrorHandlingEnabled = false; + MablApiBuilder builder = new MablApiBuilder(settings); + + MDebugAssert assertModule = MDebugAssert.create(builder); + + DynamicActiveBuilderScope dscope = builder.getDynamicScope(); + + DoubleVariableFmi2Api r = dscope.store(123.456); + FloatVariableFmi2Api f = dscope.store(123.456f); + + AExpInitializer aExpInitializer = new AExpInitializer(); + aExpInitializer.setExp(r.getReferenceExp().clone()); + AVariableDeclaration decl = new AVariableDeclaration(); + decl.setName(new LexIdentifier("fr", null)); + decl.setType(new AFloatNumericPrimitiveType()); + decl.setInitializer(aExpInitializer); + ALocalVariableStm stm = new ALocalVariableStm(); + stm + .setDeclaration(decl); + dscope.add(stm); + + + assertModule.assertEquals(f, new VariableFmi2Api<>(stm, decl.getType().clone(), dscope,dscope, null, new AIdentifierExp(new LexIdentifier("fr", null)))); + + + String spec = PrettyPrinter.print(builder.build()); + System.out.println(spec); + + + check(spec, ""); + + } +} diff --git a/parser/src/main/java/org/intocps/maestro/parser/ParseTree2AstConverter.java b/parser/src/main/java/org/intocps/maestro/parser/ParseTree2AstConverter.java index 31d49fd0..0ac4cdfe 100644 --- a/parser/src/main/java/org/intocps/maestro/parser/ParseTree2AstConverter.java +++ b/parser/src/main/java/org/intocps/maestro/parser/ParseTree2AstConverter.java @@ -505,8 +505,8 @@ public INode visitLiteral(MablParser.LiteralContext ctx) { literal.setValue(Integer.parseInt(ctx.DECIMAL_LITERAL().getText())); return literal; } else if (ctx.FLOAT_LITERAL() != null) { - AFloatLiteralExp literal = new AFloatLiteralExp(); - literal.setValue(Float.parseFloat(ctx.FLOAT_LITERAL().getText())); + ARealLiteralExp literal = new ARealLiteralExp(); + literal.setValue(Double.parseDouble(ctx.FLOAT_LITERAL().getText())); return literal; } else if (ctx.STRING_LITERAL() != null) { diff --git a/typechecker/src/main/java/org/intocps/maestro/typechecker/TypeCheckVisitor.java b/typechecker/src/main/java/org/intocps/maestro/typechecker/TypeCheckVisitor.java index 85eb15a3..f7d6ea17 100644 --- a/typechecker/src/main/java/org/intocps/maestro/typechecker/TypeCheckVisitor.java +++ b/typechecker/src/main/java/org/intocps/maestro/typechecker/TypeCheckVisitor.java @@ -14,6 +14,7 @@ import org.intocps.maestro.typechecker.context.ModulesContext; import java.util.*; +import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; @@ -490,7 +491,11 @@ public PType caseAVariableDeclaration(AVariableDeclaration node, Context ctxt) t } else if (node.getInitializer() != null) { PType initType = node.getInitializer().apply(this, ctxt); - if (!typeComparator.compatible(type, initType)) { + + BiFunction isDoubleToFloatAssignment = (declType, assignType)-> + typeComparator.compatible(declType,new AFloatNumericPrimitiveType())&&typeComparator.compatible(assignType,new ARealNumericPrimitiveType()); + + if (!typeComparator.compatible(type, initType) && !isDoubleToFloatAssignment.apply(type,initType)) { errorReporter.report(0, type + " cannot be initialized with type: " + initType, node.getName().getSymbol()); }