diff --git a/ast/src/main/resources/maestrov2.astv2 b/ast/src/main/resources/maestrov2.astv2 index 476914b..b0835ea 100644 --- a/ast/src/main/resources/maestrov2.astv2 +++ b/ast/src/main/resources/maestrov2.astv2 @@ -28,7 +28,7 @@ Abstract Syntax Tree specification {-> package='org.maestro.ast.specification'} = {language} [name]:identifier [functions]:type.function* - | {simulation} [body]:stm* + | {simulation} [body]:stm ; exp {-> package='org.maestro.ast.expression'} @@ -37,19 +37,28 @@ exp {-> package='org.maestro.ast.expression'} | {intLiteral} [value]:java_Integer | {stringLiteral} [value]:java_String | {identifier} [value]:java_String + | {tupleIdentifier} [value]:identifier* | {booleanLiteral} [value]:java_Boolean - | {load} [uri]:exp.stringLiteral [importName]:identifier | {apply} [root]:exp [functionName]:identifier [args]:exp* | {seqComp} [members]:exp* + | #binary + ; + +#binary {-> package='org.maestro.ast.expression' + | [left]:exp + | [right]:exp } + = {lessThan} + | {addition} ; stm {-> package='org.maestro.ast.statements'} - = {unload} [fmu]:identifier + = {load} [uri]:exp.stringLiteral [importName]:identifier [target]:tupleIdentifier + | {unload} [fmu]:identifier | {assignment} [target]:identifier [exp]:exp | {variableDeclaration} [type]:type [name]:identifier [initializer]:exp | {import} [name]:identifier - | {while} [exp]:exp [statement]:block - | {if} [exp]:exp [thenStm]:block [elseStm]:block + | {while} [condition]:exp [body]:stm + | {if} [exp]:exp [thenStm]:stm [elseStm]:stm | {block} [body]:stm* ; diff --git a/test-program/src/main/java/Main.java b/test-program/src/main/java/Main.java deleted file mode 100644 index 5788183..0000000 --- a/test-program/src/main/java/Main.java +++ /dev/null @@ -1,121 +0,0 @@ -import org.maestro.ast.expression.*; -import org.maestro.ast.specification.ASimulationSpecification; -import org.maestro.ast.statements.AAssignmentStm; -import org.maestro.ast.statements.PStm; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public class Main { - static Factory f = new Factory(); - - public static void main() { - - - ASimulationSpecification spec = new ASimulationSpecification(); - - final String tankDecl = "tankDecl"; - final String ctrlDecl = "ctrlDecl"; - - spec.getBody().add(f.assignment(ctrlDecl, new ALoadExp(f.newStringLiteral("controller.fmu"), f.newIdentifierExp("fmi2")))); - spec.getBody().add(f.assignment(tankDecl, new ALoadExp(f.newStringLiteral("tank.fmu"), f.newIdentifierExp("fmi2")))); - - //TODO how do I know that I need to make these instances? - final String tank = "tank"; - final String ctrl = "ctrl"; - - spec.getBody().add(f.assignment(ctrl, - f.newApply(ctrlDecl, "instantiate", f.newStringLiteral("controller"), f.newBoolLiteral(false), f.newBoolLiteral(true)))); - spec.getBody().add(f.assignment(tank, - f.newApply(ctrlDecl, "instantiate", f.newStringLiteral("tank"), f.newBoolLiteral(false), f.newBoolLiteral(true)))); - - - spec.getBody().add(f.assignment(ctrl, - f.newApply(ctrlDecl, "setupExperiment", f.newVariableExp(ctrl), f.newBoolLiteral(false), f.newRealLiteral(0.0), f.newRealLiteral(0.0), - f.newBoolLiteral(true), f.newRealLiteral(0.0)))); - spec.getBody().add(f.assignment(ctrl, - f.newApply(tankDecl, "setupExperiment", f.newVariableExp(tank), f.newBoolLiteral(false), f.newRealLiteral(0.0), f.newRealLiteral(0.0), - f.newBoolLiteral(true), f.newRealLiteral(0.0)))); - - - spec.getBody().add(setReal("s", ctrlDecl, ctrl, ints(1, 2), reals(1, 2))); - - spec.getBody().add(f.assignment(ctrl, f.newApply(ctrlDecl, "enterInitializationMode", f.newVariableExp(ctrl)))); - spec.getBody().add(f.assignment(ctrl, f.newApply(tankDecl, "enterInitializationMode", f.newVariableExp(tank)))); - - final String level = "level"; - final String valve = "valve"; - - } - - public static int[] ints(int... v) { - return v; - } - - public static double[] reals(double... v) { - return v; - } - - public static PStm setReal(String result, String decl, String comp, int[] vref, double[] values) { - return f.assignment(result, f.newApply(decl, "setReal", f.newVariableExp(comp), - f.newSeqComp(Arrays.stream(vref).mapToObj(v -> f.newIntLiteral(v)).collect(Collectors.toList())), - f.newSeqComp(Arrays.stream(values).mapToObj(v -> f.newRealLiteral(v)).collect(Collectors.toList())))); - } - - @SuppressWarnings("all") - public static class Factory { - - public AIdentifierExp newIdentifierExp(String id) { - return new AIdentifierExp(id); - } - - public AStringLiteralExp newStringLiteral(String value) { - return new AStringLiteralExp(value); - } - - public PStm assignment(String target, PExp source) { - AAssignmentStm assignmentStm = new AAssignmentStm(); - assignmentStm.setExp(source); - assignmentStm.setTarget(newIdentifierExp(target)); - return assignmentStm; - } - - public PExp newApply(String rootId, String functionId, PExp... args) { - AApplyExp apply = new AApplyExp(); - apply.setRoot(newIdentifierExp(rootId)); - apply.setFunctionName(newIdentifierExp(functionId)); - apply.setArgs(Arrays.asList(args)); - return apply; - } - - public PExp newBoolLiteral(boolean b) { - return new ABooleanLiteralExp(b); - } - - public PExp newVariableExp(String ctrl) { - AVariableExp v = new AVariableExp(); - v.setName(newIdentifierExp(ctrl)); - return v; - } - - public PExp newRealLiteral(double v) { - ARealLiteralExp l = new ARealLiteralExp(); - l.setValue(v); - return l; - } - - public PExp newSeqComp(List members) { - ASeqCompExp comp = new ASeqCompExp(); - comp.setMembers(members); - return comp; - } - - public PExp newIntLiteral(int v) { - return new AIntLiteralExp(v); - - } - } - -} diff --git a/test-program/src/main/java/org/intocps/maestro/Main.java b/test-program/src/main/java/org/intocps/maestro/Main.java new file mode 100644 index 0000000..217a5fd --- /dev/null +++ b/test-program/src/main/java/org/intocps/maestro/Main.java @@ -0,0 +1,254 @@ +package org.intocps.maestro; + +import org.maestro.ast.analysis.AnalysisException; +import org.maestro.ast.expression.*; +import org.maestro.ast.specification.ASimulationSpecification; +import org.maestro.ast.statements.*; +import org.maestro.ast.types.AArrayType; +import org.maestro.ast.types.ARealBasicType; +import org.maestro.ast.types.*; +import org.maestro.ast.types.SBasicType; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Main { + static Factory f = new Factory(); + + public static void main(String[] args) throws AnalysisException { + + + ABlockStm list = f.newBlock(); + + list.getBody().add(f.newImport("fmi2")); + + final String tankDecl = "tankDecl"; + final String ctrlDecl = "ctrlDecl"; + + list.getBody().add(f.newVariableDecl(tankDecl, f.newNamedType("fmi2"), null)); + list.getBody().add(f.newVariableDecl(ctrlDecl, f.newNamedType("fmi2"), null)); + + list.getBody().add(new ALoadStm(f.newStringLiteral("controller.fmu"), f.newIdentifierExp("fmi2"), + new ATupleIdentifierExp(Arrays.asList(f.newIdentifierExp("errorCode"),f.newIdentifierExp(ctrlDecl))))); + + list.getBody().add(new ALoadStm(f.newStringLiteral("tank.fmu"), f.newIdentifierExp("fmi2"), + new ATupleIdentifierExp(Arrays.asList(f.newIdentifierExp("errorCode"),f.newIdentifierExp(tankDecl))))); + +// list.getBody().add(f.assignment("err", new ALoadExp(f.newStringLiteral("controller.fmu"), f.newIdentifierExp("fmi2")))); +// list.getBody().add(f.assignment("err", new ALoadExp(f.newStringLiteral("tank.fmu"), f.newIdentifierExp("fmi2")))); + + //TODO how do I know that I need to make these instances? + final String tank = "tank"; + final String ctrl = "ctrl"; + + list.getBody().add(f.assignment(ctrl, + f.newApply(ctrlDecl, "instantiate", f.newStringLiteral("controller"), f.newStringLiteral("uuid"), f.newStringLiteral("uri"), + f.newBoolLiteral(false), f.newBoolLiteral(true)))); list.getBody().add(f.assignment(tank, + f.newApply(ctrlDecl, "instantiate", f.newStringLiteral("tank"), f.newStringLiteral("uuid"), f.newStringLiteral("uri"), + f.newBoolLiteral(false), f.newBoolLiteral(true)))); + + + list.getBody().add(f.assignment("s", + f.newApply(ctrlDecl, "setupExperiment", f.newVariableExp(ctrl), f.newBoolLiteral(false), f.newRealLiteral(0.0), f.newRealLiteral(0.0), + f.newBoolLiteral(true), f.newRealLiteral(0.0)))); + list.getBody().add(f.assignment("s", + f.newApply(tankDecl, "setupExperiment", f.newVariableExp(tank), f.newBoolLiteral(false), f.newRealLiteral(0.0), f.newRealLiteral(0.0), + f.newBoolLiteral(true), f.newRealLiteral(0.0)))); + + + list.getBody().add(setReal("s", ctrlDecl, ctrl, ints(1, 2), reals(1, 2))); + + list.getBody().add(f.assignment("s", f.newApply(ctrlDecl, "enterInitializationMode", f.newVariableExp(ctrl)))); + + + final String level = "level"; + final String valve = "valve"; + + list.getBody().add(f.newVariableDecl(level, f.newArrayType(1, f.newRealType()), null)); + list.getBody().add(f.newVariableDecl(valve, f.newArrayType(1, f.newRealType()), null)); + + list.getBody().add(getReal("s", tankDecl, tank, ints(1), f.newVariableExp(level))); + list.getBody().add(setReal("s", ctrlDecl, ctrl, ints(3), f.newVariableExp(level))); + list.getBody().add(getReal("s", ctrlDecl, ctrl, ints(4), f.newVariableExp(valve))); + list.getBody().add(getReal("s", tankDecl, tank, ints(2), f.newVariableExp(level))); + + list.getBody().add(f.assignment(ctrl, f.newApply(tankDecl, "exitInitializationMode", f.newVariableExp(tank)))); + + String time = "time"; + + list.getBody().add(f.newVariableDecl(time, f.newRealType(), f.newRealLiteral(0.0))); + + ABlockStm whileBody = f.newBlock(); + list.getBody().add(f.newWhile(f.newLessThan(f.newVariableExp(tank), f.newRealLiteral(10.0)), whileBody)); + + whileBody.getBody() + .add(f.assignment("s", f.newApply(ctrlDecl, "doStep", f.newVariableExp(time), f.newRealLiteral(0.001), f.newBoolLiteral(false)))); + whileBody.getBody() + .add(f.assignment("s", f.newApply(tankDecl, "doStep", f.newVariableExp(time), f.newRealLiteral(0.001), f.newBoolLiteral(false)))); + + + whileBody.getBody().add(getReal("s", tankDecl, tank, ints(1), f.newVariableExp(level))); + whileBody.getBody().add(setReal("s", ctrlDecl, ctrl, ints(3), f.newVariableExp(level))); + whileBody.getBody().add(getReal("s", ctrlDecl, ctrl, ints(4), f.newVariableExp(valve))); + whileBody.getBody().add(getReal("s", tankDecl, tank, ints(2), f.newVariableExp(level))); + + whileBody.getBody().add(f.assignment(time, f.newAddition(f.newVariableExp(time), f.newRealLiteral(0.001)))); + + + list.getBody().add(f.assignment("s", f.newApply(tankDecl, "terminate", f.newVariableExp(tank)))); + list.getBody().add(f.assignment("s", f.newApply(tankDecl, "freeInstance", f.newVariableExp(tank)))); + list.getBody().add(f.newUnload(tankDecl)); + + list.getBody().add(f.assignment("s", f.newApply(ctrlDecl, "terminate", f.newVariableExp(ctrl)))); + list.getBody().add(f.assignment("s", f.newApply(ctrlDecl, "freeInstance", f.newVariableExp(ctrl)))); + list.getBody().add(f.newUnload(ctrlDecl)); + + + ASimulationSpecification spec = new ASimulationSpecification(); + spec.setBody(list); + + System.out.println(spec.apply(new SourceGenerator())); + } + + public static int[] ints(int... v) { + return v; + } + + public static double[] reals(double... v) { + return v; + } + + public static PStm setReal(String result, String decl, String comp, int[] vref, double[] values) { + return f.assignment(result, f.newApply(decl, "setReal", f.newVariableExp(comp), + f.newSeqComp(Arrays.stream(vref).mapToObj(v -> f.newIntLiteral(v)).collect(Collectors.toList())), + f.newSeqComp(Arrays.stream(values).mapToObj(v -> f.newRealLiteral(v)).collect(Collectors.toList())))); + } + + public static PStm setReal(String result, String decl, String comp, int[] vref, PExp values) { + return f.assignment(result, f.newApply(decl, "setReal", f.newVariableExp(comp), + f.newSeqComp(Arrays.stream(vref).mapToObj(v -> f.newIntLiteral(v)).collect(Collectors.toList())), values)); + } + + public static PStm getReal(String result, String decl, String comp, int[] vref, PExp values) { + return f.assignment(result, f.newApply(decl, "setReal", f.newVariableExp(comp), + f.newSeqComp(Arrays.stream(vref).mapToObj(v -> f.newIntLiteral(v)).collect(Collectors.toList())), values)); + } + + @SuppressWarnings("all") + public static class Factory { + + public AIdentifierExp newIdentifierExp(String id) { + return new AIdentifierExp(id); + } + + public AStringLiteralExp newStringLiteral(String value) { + return new AStringLiteralExp(value); + } + + public PStm assignment(String target, PExp source) { + AAssignmentStm assignmentStm = new AAssignmentStm(); + assignmentStm.setExp(source); + assignmentStm.setTarget(newIdentifierExp(target)); + return assignmentStm; + } + + public PExp newApply(String rootId, String functionId, PExp... args) { + AApplyExp apply = new AApplyExp(); + apply.setRoot(newIdentifierExp(rootId)); + apply.setFunctionName(newIdentifierExp(functionId)); + apply.setArgs(Arrays.asList(args)); + return apply; + } + + public PExp newBoolLiteral(boolean b) { + return new ABooleanLiteralExp(b); + } + + public PExp newVariableExp(String ctrl) { + AVariableExp v = new AVariableExp(); + v.setName(newIdentifierExp(ctrl)); + return v; + } + + public PExp newRealLiteral(double v) { + ARealLiteralExp l = new ARealLiteralExp(); + l.setValue(v); + return l; + } + + public PExp newSeqComp(List members) { + ASeqCompExp comp = new ASeqCompExp(); + comp.setMembers(members); + return comp; + } + + public PExp newIntLiteral(int v) { + return new AIntLiteralExp(v); + + } + + public PStm newImport(String name) { + AImportStm im = new AImportStm(); + im.setName(newIdentifierExp(name)); + return im; + } + + public PStm newVariableDecl(String name, PType type, PExp initializer) { + AVariableDeclarationStm decl = new AVariableDeclarationStm(); + decl.setName(newIdentifierExp(name)); + decl.setType(type); + decl.setInitializer(initializer); + return decl; + } + + public SBasicType newRealType() { + return new ARealBasicType(); + } + + public PType newArrayType(int size, SBasicType type) { + AArrayType t = new AArrayType(); + t.setType(type); + t.setSize(size); + return t; + } + + public PExp newLessThan(PExp left, PExp right) { + ALessThanBinaryExp exp = new ALessThanBinaryExp(); + exp.setLeft(left); + exp.setRight(right); + return exp; + } + + public PStm newWhile(PExp condition, PStm body) { + AWhileStm whileStm = new AWhileStm(); + whileStm.setCondition(condition); + whileStm.setBody(body); + return whileStm; + } + + public ABlockStm newBlock() { + return new ABlockStm(); + } + + public PStm newUnload(String name) { + AUnloadStm stm = new AUnloadStm(); + stm.setFmu(f.newIdentifierExp(name)); + return stm; + } + + public PExp newAddition(PExp left, PExp right) { + AAdditionBinaryExp exp = new AAdditionBinaryExp(); + exp.setLeft(left); + exp.setRight(right); + return exp; + } + + public PType newNamedType(String name) { + ANameType t = new ANameType(); + t.setName(f.newIdentifierExp(name)); + return t; + } + } + +} diff --git a/test-program/src/main/java/org/intocps/maestro/SourceGenerator.java b/test-program/src/main/java/org/intocps/maestro/SourceGenerator.java new file mode 100644 index 0000000..07ccf5b --- /dev/null +++ b/test-program/src/main/java/org/intocps/maestro/SourceGenerator.java @@ -0,0 +1,197 @@ +package org.intocps.maestro; + +import org.maestro.ast.analysis.AnalysisException; +import org.maestro.ast.analysis.AnswerAdaptor; +import org.maestro.ast.expression.*; +import org.maestro.ast.node.INode; +import org.maestro.ast.specification.ASimulationSpecification; +import org.maestro.ast.statements.*; +import org.maestro.ast.types.*; +import org.maestro.ast.types.ABooleanBasicType; +import org.maestro.ast.types.AIntBasicType; +import org.maestro.ast.types.ARealBasicType; + +import java.util.Iterator; + + + +public class SourceGenerator extends AnswerAdaptor { + @Override + public String createNewReturnValue(INode node) throws AnalysisException { + return ""; + } + + @Override + public String createNewReturnValue(Object node) throws AnalysisException { + return ""; + } + + @Override + public String defaultSBinaryExp(SBinaryExp node) throws AnalysisException { + return node.getLeft().apply(this) + " + " + node.getRight().apply(this); + } + + @Override + public String caseAWhileStm(AWhileStm node) throws AnalysisException { + return String.format("white(%s)\n%s", node.getCondition().apply(this), node.getBody().apply(this)); + } + + @Override + public String caseASimulationSpecification(ASimulationSpecification node) throws AnalysisException { + + return node.getBody().apply(this); + + } + + + + @Override + public String caseAApplyExp(AApplyExp node) throws AnalysisException { + + if (node.getFunctionName().getValue().equals("instantiate")) { + String name = node.getArgs().get(0).apply(this); + String uuid = node.getArgs().get(1).apply(this); + String uri = node.getArgs().get(2).apply(this); + String visible = node.getArgs().get(3).apply(this); + String logginOn = node.getArgs().get(4).apply(this); + + // String functionsCbName = name.replaceAll("[^a-zA-Z0-9]", "")+"func"; + // String funnctions = " fmi2CallbackFunctions "+functionsCbName+" = {.logger = &fmuLogger, .allocateMemory = NULL, .freeMemory = NULL, " + + // ".stepFinished = " + + // "NULL, .componentEnvironment = NULL};"; + + return String.format("%s->instantiate(%s,fmi2CoSimulation,%s,%s,&%s,%s,%s)",node.getRoot().apply(this), name, uuid, uri, "callback", + visible, + logginOn); + } + + /* + auto comp = fmu.instantiate("name", fmi2CoSimulation, "jkkk", + "file:/Users/kgl/data/au/into-cps-association/rabbitmq-fmu/fmu-loader/tmp/resources", &cbFuncs, true, + true); + + fmu.enterInitializationMode(comp); + + + fmi2ValueReference refs[1]={}; + fmi2Real reals[1]; + fmu.setReal(comp,refs,1,reals);*/ + + + String ret = node.getRoot().apply(this) + "." + node.getFunctionName().apply(this) + "("; + Iterator itr = node.getArgs().iterator(); + while (itr.hasNext()) { + ret += itr.next().apply(this); + if (itr.hasNext()) { + ret += ","; + } + } + return ret + ")"; + + } + + @Override + public String caseAAssignmentStm(AAssignmentStm node) throws AnalysisException { + + + return node.getTarget().apply(this) + " = " + node.getExp().apply(this); + + + } + + @Override + public String caseAIdentifierExp(AIdentifierExp node) throws AnalysisException { + return node.getValue(); + } + + @Override + public String caseAImportStm(AImportStm node) throws AnalysisException { + return "//import " + node.getName(); + } + + @Override + public String caseAArrayType(AArrayType node) throws AnalysisException { + return node.getType().apply(this) + "[" + node.getSize() + "]"; + } + + @Override + public String caseARealBasicType(ARealBasicType node) throws AnalysisException { + return "double"; + } + + @Override + public String caseAIntBasicType(AIntBasicType node) throws AnalysisException { + return "int"; + } + + @Override + public String caseARealLiteralExp(ARealLiteralExp node) throws AnalysisException { + return node.getValue() + ""; + } + + @Override + public String caseABooleanBasicType(ABooleanBasicType node) throws AnalysisException { + return "bool"; + } + + @Override + public String caseABooleanLiteralExp(ABooleanLiteralExp node) throws AnalysisException { + return node.getValue() + ""; + } + + @Override + public String caseAVariableExp(AVariableExp node) throws AnalysisException { + return node.getName().apply(this); + } + + @Override + public String caseAStringLiteralExp(AStringLiteralExp node) throws AnalysisException { + return "\"" + node.getValue() + "\""; + } + + + @Override + public String caseABlockStm(ABlockStm node) throws AnalysisException { + + String ret = "{"; + Iterator itr = node.getBody().iterator(); + while (itr.hasNext()) { + ret += itr.next().apply(this); + ret += ";\n"; + } + return ret + "}"; + } + + @Override + public String caseANameType(ANameType node) throws AnalysisException { + String name = node.getName().apply(this); + + if(name.equals("fmi2")) + return "FMU"; + else + return name; + } + + @Override + public String caseAVariableDeclarationStm(AVariableDeclarationStm node) throws AnalysisException { + String res = node.getType().apply(this) + " " + node.getName().apply(this); + + if (node.getInitializer() != null) { + res += (" = " + node.getInitializer().apply(this)); + } + + return res; + } + + @Override + public String caseAUnloadStm(AUnloadStm node) throws AnalysisException { + return "//unlooad "+node.getFmu().apply(this); + } + + @Override + public String caseALoadStm(ALoadStm node) throws AnalysisException { + return "auto "+node.getTarget().getValue().get(0).apply(this)+" = loadDll("+node.getUri().apply(this)+","+ + "&"+node.getTarget().getValue().get(1).apply(this)+ + ")"; + } +}