From 24382254c00e51d86336190a0b12f15f8e1daa43 Mon Sep 17 00:00:00 2001 From: kel Date: Thu, 12 Sep 2024 13:39:11 +0200 Subject: [PATCH] added long --- .../intocps/maestro/ast/MableAstFactory.java | 11 ++ .../framework/fmi2/api/FmiBuilder.java | 6 + .../fmi2/api/mabl/scoping/ScopeFmi2Api.java | 5 + .../api/mabl/values/LongExpressionValue.java | 124 ++++++++++++++++++ .../mabl/variables/LongVariableFmi2Api.java | 59 +++++++++ 5 files changed, 205 insertions(+) create mode 100644 frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/LongExpressionValue.java create mode 100644 frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/LongVariableFmi2Api.java diff --git a/ast/src/main/java/org/intocps/maestro/ast/MableAstFactory.java b/ast/src/main/java/org/intocps/maestro/ast/MableAstFactory.java index 9fdab6d2..874207c2 100644 --- a/ast/src/main/java/org/intocps/maestro/ast/MableAstFactory.java +++ b/ast/src/main/java/org/intocps/maestro/ast/MableAstFactory.java @@ -381,6 +381,12 @@ public static AIntLiteralExp newAIntLiteralExp(Integer value) { return exp; } + public static ALongLiteralExp newALongLiteralExp(Long value) { + ALongLiteralExp exp = new ALongLiteralExp(); + exp.setValue(value); + return exp; + } + public static AStringLiteralExp newAStringLiteralExp(String value) { AStringLiteralExp exp = new AStringLiteralExp(); @@ -445,6 +451,11 @@ public static AIntNumericPrimitiveType newAIntNumericPrimitiveType() { return type; } + public static ALongNumericPrimitiveType newALongNumericPrimitiveType() { + ALongNumericPrimitiveType type = new ALongNumericPrimitiveType(); + return type; + } + public static AIntNumericPrimitiveType newIntType() { return newAIntNumericPrimitiveType(); } diff --git a/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/FmiBuilder.java b/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/FmiBuilder.java index 5fe95acc..ff6ad15b 100644 --- a/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/FmiBuilder.java +++ b/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/FmiBuilder.java @@ -462,6 +462,12 @@ interface IntVariable extends Variable, ProvidesTy void increment(); } + interface LongVariable extends Variable, ProvidesTypedReferenceExp, NumericTypedReferenceExp { + void decrement(); + + void increment(); + } + interface UIntVariable extends Variable, ProvidesTypedReferenceExp, NumericTypedReferenceExp{ void decrement(); diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/scoping/ScopeFmi2Api.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/scoping/ScopeFmi2Api.java index 3a2283a7..53d875b3 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/scoping/ScopeFmi2Api.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/scoping/ScopeFmi2Api.java @@ -251,6 +251,11 @@ public UIntVariableFmi2Api storeUInt(String name, long value) { return storeUInt(() -> builder.getNameGenerator().getName(name), value); } + @Override + public LongVariableFmi2Api store(String name, long value) { + return store(() -> builder.getNameGenerator().getName(name), value); + } + @Override public ArrayVariableFmi2Api store(String name, V[] value) { return store(() -> builder.getNameGenerator().getName(name), value); diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/LongExpressionValue.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/LongExpressionValue.java new file mode 100644 index 00000000..26943f1d --- /dev/null +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/LongExpressionValue.java @@ -0,0 +1,124 @@ +package org.intocps.maestro.framework.fmi2.api.mabl.values; + +import org.intocps.maestro.ast.node.AIntNumericPrimitiveType; +import org.intocps.maestro.ast.node.ALongNumericPrimitiveType; +import org.intocps.maestro.ast.node.PExp; +import org.intocps.maestro.ast.node.PType; +import org.intocps.maestro.framework.fmi2.api.FmiBuilder; +import org.intocps.maestro.framework.fmi2.api.mabl.NumericExpressionValueFmi2Api; + +import static org.intocps.maestro.ast.MableAstFactory.*; + + +public class LongExpressionValue extends NumericExpressionValueFmi2Api implements FmiBuilder.LongExpressionValue { + + final PType type = new ALongNumericPrimitiveType(); + final PExp exp; + + public LongExpressionValue(PExp exp) { + this.exp = exp; + } + + public LongExpressionValue(long value) { + this.exp = newALongLiteralExp(value); + } + + public static LongExpressionValue of(int i) { + return new LongExpressionValue(newAIntLiteralExp(i)); + } + + @Override + public PExp getExp() { + return this.exp.clone(); + } + + @Override + public PType getType() { + return this.type; + } + + @Override + public LongExpressionValue subtraction(int v) { + return new LongExpressionValue(newMinusExp(getExp(), newAIntLiteralExp(v))); + } + + @Override + public LongExpressionValue addition(int v) { + return new LongExpressionValue(newPlusExp(getExp(), newAIntLiteralExp(v))); + } + + // TODO: This one is tricky. And int divided by and int, should that be a double or an int? It depends on the target variable. + // Not considered for now. + @Override + public DoubleExpressionValue divide(int v) { + return new DoubleExpressionValue(newDivideExp(getExp(), newAIntLiteralExp(v))); + } + + @Override + public LongExpressionValue multiply(int v) { + return new LongExpressionValue(newMultiplyExp(getExp(), newAIntLiteralExp(v))); + } + + @Override + public DoubleExpressionValue subtraction(double v) { + return new DoubleExpressionValue(newMinusExp(getExp(), newARealLiteralExp(v))); + } + + @Override + public DoubleExpressionValue addition(double v) { + return new DoubleExpressionValue(newPlusExp(getExp(), newARealLiteralExp(v))); + } + + @Override + public DoubleExpressionValue divide(double v) { + return new DoubleExpressionValue(newDivideExp(getExp(), newARealLiteralExp(v))); + } + + @Override + public DoubleExpressionValue multiply(double v) { + return new DoubleExpressionValue(newMultiplyExp(getExp(), newARealLiteralExp(v))); + } + + @Override + public NumericExpressionValueFmi2Api addition(FmiBuilder.NumericTypedReferenceExp v) { + if (v instanceof DoubleExpressionValue) { + return new DoubleExpressionValue(newPlusExp(this.getExp(), v.getExp())); + } else if (v instanceof LongExpressionValue) { + return new LongExpressionValue(newPlusExp(this.getExp(), v.getExp())); + } else { + throw new RuntimeException(v + " is not of type NumericExpressionValue."); + } + } + + @Override + public NumericExpressionValueFmi2Api divide(FmiBuilder.NumericTypedReferenceExp v) { + if (v instanceof DoubleExpressionValue || v instanceof LongExpressionValue) { + return new DoubleExpressionValue(newDivideExp(this.getExp(), v.getExp())); + } else { + throw new RuntimeException(v + " is not of type IntExpressionValue nor DoubleExpressionValue."); + } + } + + @Override + public NumericExpressionValueFmi2Api subtraction(FmiBuilder.NumericTypedReferenceExp v) { + if (v instanceof DoubleExpressionValue) { + return new DoubleExpressionValue(newMinusExp(this.getExp(), v.getExp())); + } else if (v instanceof LongExpressionValue) { + return new LongExpressionValue(newMinusExp(this.getExp(), v.getExp())); + } else { + throw new RuntimeException(v + " is not of type IntExpressionValue nor DoubleExpressionValue."); + } + } + + @Override + public NumericExpressionValueFmi2Api multiply(FmiBuilder.NumericTypedReferenceExp v) { + if (v instanceof DoubleExpressionValue) { + return new DoubleExpressionValue(newMultiplyExp(this.getExp(), v.getExp())); + } else if (v instanceof LongExpressionValue) { + return new LongExpressionValue(newMultiplyExp(this.getExp(), v.getExp())); + } else { + throw new RuntimeException(v + " is not of type IntExpressionValue nor DoubleExpressionValue."); + + } + } +} diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/LongVariableFmi2Api.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/LongVariableFmi2Api.java new file mode 100644 index 00000000..36f33a2d --- /dev/null +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/LongVariableFmi2Api.java @@ -0,0 +1,59 @@ +package org.intocps.maestro.framework.fmi2.api.mabl.variables; + +import org.intocps.maestro.ast.node.*; +import org.intocps.maestro.framework.fmi2.api.FmiBuilder; +import org.intocps.maestro.framework.fmi2.api.mabl.scoping.IMablScope; +import org.intocps.maestro.framework.fmi2.api.mabl.values.LongExpressionValue; + +import static org.intocps.maestro.ast.MableAstFactory.*; + +public class LongVariableFmi2Api extends VariableFmi2Api implements FmiBuilder.LongVariable { + public LongVariableFmi2Api(PStm declaration, IMablScope declaredScope, FmiBuilder.DynamicActiveScope dynamicScope, + PStateDesignator designator, PExp referenceExp) { + super(declaration, newALongNumericPrimitiveType(), declaredScope, dynamicScope, designator, referenceExp); + } + + + @Override + public void decrement() { + this.decrement(dynamicScope); + } + + public void decrement(FmiBuilder.Scope scope) { + scope.add(newAAssignmentStm(this.getDesignator(), newMinusExp(this.getReferenceExp(), newAIntLiteralExp(1)))); + } + + @Override + public void increment() { + this.increment(dynamicScope); + } + + @Override + public LongExpressionValue toMath() { + return new LongExpressionValue(this.getReferenceExp().clone()); + } + + @Override + public void setValue(FmiBuilder.LongExpressionValue addition) { + declaredScope.add(newAAssignmentStm(this.getDesignator(), addition.getExp())); + } + + public void increment(FmiBuilder.Scope scope) { + scope.add(newAAssignmentStm(this.getDesignator(), newPlusExp(this.getReferenceExp(), newAIntLiteralExp(1)))); + } + + @Override + public PType getType() { + return new AIntNumericPrimitiveType(); + } + + @Override + public PExp getExp() { + return this.getReferenceExp(); + } + + @Override + public LongVariableFmi2Api clone(PStm declaration, IMablScope declaredScope, PStateDesignator designator, PExp referenceExp) { + return new LongVariableFmi2Api(declaration, declaredScope, dynamicScope, designator, referenceExp); + } +}