Skip to content

Commit

Permalink
added support for float and test
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Sep 16, 2024
1 parent 4e1702b commit 43d112d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
69 changes: 69 additions & 0 deletions maestro/src/test/java/org/intocps/maestro/FloatDeclTest.java
Original file line number Diff line number Diff line change
@@ -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, "");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<PType,PType,Boolean> 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());
}

Expand Down

0 comments on commit 43d112d

Please sign in to comment.