Skip to content

Commit

Permalink
make input/outputs optional with a flag, fix within in 2 test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaRB committed Jul 11, 2024
1 parent 9900992 commit 0a404fa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/main/java/crml/compiler/CRMLC.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void main( String[] args ) throws Exception {
if(test.endsWith(".crml")) {
logger.trace("Translating test: " + test);
parse_file(path, test, cmd.outputDir, cmd.stacktrace, cmd.printAST ,
cmd.generateExternal, cmd.within);
cmd.generateExternal, cmd.within, cmd.causal);
if(cmd.simulate!=null) {
OMCmsg msg;
try {
Expand All @@ -125,7 +125,7 @@ public static void main( String[] args ) throws Exception {
String stripped_file_name = Utilities.stripNameEndingAndPath(path);
String outputDir = Utilities.addDirToPath(cmd.outputDir, stripped_file_name);
parse_file("", path, outputDir, cmd.stacktrace,
cmd.printAST, cmd.generateExternal, cmd.within);
cmd.printAST, cmd.generateExternal, cmd.within, cmd.causal);
if(cmd.simulate!=null){
OMCmsg msg;
try {
Expand All @@ -148,7 +148,8 @@ public static void parse_file (
String dir, String file,
String gen_dir, Boolean testMode, Boolean printAST,
Boolean generateExternal,
String within) throws Exception {
String within,
Boolean causal) throws Exception {

try {
String fullName = dir + java.io.File.separator + file;
Expand All @@ -175,9 +176,9 @@ public static void parse_file (


if (generateExternal)
visitor = new crmlVisitorImpl(parser, external_var);
visitor = new crmlVisitorImpl(parser, external_var, causal);
else
visitor = new crmlVisitorImpl(parser);
visitor = new crmlVisitorImpl(parser, causal);

try {
Value result = visitor.visit(tree);
Expand All @@ -190,6 +191,7 @@ public static void parse_file (
out_file.getParentFile().mkdirs();

BufferedWriter writer = new BufferedWriter(new FileWriter(out_file));
System.out.println("File : " + out_file.toString() + " within : " + within);
if(!within.isEmpty())
writer.write("within " + within + ";\n");
writer.write(result.contents);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/crml/compiler/CommandLineArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ public class CommandLineArgs {

@Parameter(names = "--within", description = "generate the translated modelica model within a given class")
public String within = "";

@Parameter(names = {"-causal"}, description = "Generates explicit input prefixes in helper blocks")
public Boolean causal = false;
}
25 changes: 18 additions & 7 deletions src/main/java/crml/compiler/crmlVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,32 @@ public class crmlVisitorImpl extends crmlBaseVisitor<Value> {

private String prefix= ""; //to keep track of variable prefix

private String input_prefix;
private String output_prefix;

Boolean saveExtrnal = false;
List<String> external_variables;

public crmlVisitorImpl (crmlParser parser, List<String> external_variables){
this(parser);
public crmlVisitorImpl (crmlParser parser, List<String> external_variables, Boolean causal){
this(parser, causal);
saveExtrnal= true;
this.external_variables = external_variables;
}

public crmlVisitorImpl (crmlParser parser) {
public crmlVisitorImpl (crmlParser parser, Boolean causal) {

// FIXME check that class name and class file match

this.parser = parser;

if (causal) {
input_prefix = "input";
output_prefix = "output";
} else {
input_prefix = "";
output_prefix = "";
};

types_mapping = new HashMap<String, String>();

// table for mapping CRML built in types to Modelica types
Expand Down Expand Up @@ -292,7 +303,7 @@ else if (ctx.uninstantiated_def()!=null)
sig.function_name = modelName.toString();
String mtype = bType;

definition.append("output " + bType + " out; \n");
definition.append(output_prefix + bType + " out; \n");


// generate variables
Expand All @@ -301,7 +312,7 @@ else if (ctx.uninstantiated_def()!=null)
String type = ctx.operator_def().type().get(i).getText();
mtype = types_mapping.get(type);
if(mtype == null) mtype = type;
definition.append("input " + mtype + " " + v.getText() + ";\n");
definition.append(input_prefix + mtype + " " + v.getText() + ";\n");

// TODO fix set support
variableTable.putlocalVariable(v.getText(), type, false);
Expand Down Expand Up @@ -370,7 +381,7 @@ else if (ctx.uninstantiated_def()!=null)

// generate variables
for (IdContext v : ctx.id()) {
definition.append("input " + bType + " " + v.getText() + ";\n");
definition.append(input_prefix + bType + " " + v.getText() + ";\n");

// TODO fix sets
variableTable.putlocalVariable(v.getText(), "Boolean", false);
Expand All @@ -381,7 +392,7 @@ else if (ctx.uninstantiated_def()!=null)

user_operators.put(modelName.toString(), sig);

definition.append("output " + bType + " out; \n");
definition.append(output_prefix + bType + " out; \n");

// append body
Value exp = visit(ctx.exp());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ctests/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static OMCmsg runTest( final String fileName,
try {

crml.compiler.CRMLC.parse_file(cs.testFolderIn, fileName, out_dir,
true, false, true, stripped_file_name);
true, false, true, stripped_file_name, false);

} catch (Exception e) {
fail("Unable to translate " + fileName + " to Modelica\n", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
within ;
within BecomesFalse;
model BecomesFalse_verif
extends BecomesFalse;
BecomesFalse_externals externals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
within ;
within BecomesTrue;
model BecomesTrue_verif
extends BecomesTrue;
BecomesTrue_externals externals
Expand Down

0 comments on commit 0a404fa

Please sign in to comment.