Skip to content

Commit

Permalink
added long
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Sep 12, 2024
1 parent 5bceacc commit 2438225
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ast/src/main/java/org/intocps/maestro/ast/MableAstFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ interface IntVariable<AST> extends Variable<AST, IntExpressionValue>, ProvidesTy
void increment();
}

interface LongVariable<AST> extends Variable<AST, LongExpressionValue>, ProvidesTypedReferenceExp, NumericTypedReferenceExp {
void decrement();

void increment();
}

interface UIntVariable<AST> extends Variable<AST, UIntExpressionValue>, ProvidesTypedReferenceExp, NumericTypedReferenceExp{
void decrement();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <V> ArrayVariableFmi2Api<V> store(String name, V[] value) {
return store(() -> builder.getNameGenerator().getName(name), value);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.");

}
}
}
Original file line number Diff line number Diff line change
@@ -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<FmiBuilder.LongExpressionValue> implements FmiBuilder.LongVariable<PStm> {
public LongVariableFmi2Api(PStm declaration, IMablScope declaredScope, FmiBuilder.DynamicActiveScope<PStm> dynamicScope,
PStateDesignator designator, PExp referenceExp) {
super(declaration, newALongNumericPrimitiveType(), declaredScope, dynamicScope, designator, referenceExp);
}


@Override
public void decrement() {
this.decrement(dynamicScope);
}

public void decrement(FmiBuilder.Scope<PStm> 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<PStm> 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);
}
}

0 comments on commit 2438225

Please sign in to comment.