Skip to content

Commit

Permalink
Add modifierToJSON API call (#10438)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Mar 23, 2023
1 parent d33f6a4 commit d06ba1f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -4420,6 +4420,14 @@ function getModelInstanceIcon
external "builtin";
end getModelInstanceIcon;

function modifierToJSON
"Parses a modifier given as a string and dumps it as JSON."
input String modifier;
input Boolean prettyPrint = false;
output String json;
external "builtin";
end modifierToJSON;

function storeAST
output Integer id;
external "builtin";
Expand Down
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Expand Up @@ -4673,6 +4673,14 @@ function getModelInstanceIcon
external "builtin";
end getModelInstanceIcon;

function modifierToJSON
"Parses a modifier given as a string and dumps it as JSON."
input String modifier;
input Boolean prettyPrint = false;
output String json;
external "builtin";
end modifierToJSON;

function storeAST
output Integer id;
external "builtin";
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -3091,6 +3091,9 @@ algorithm
case ("getModelInstanceIcon", {Values.CODE(Absyn.C_TYPENAME(classpath)), Values.BOOL(b)})
then NFApi.getModelInstanceIcon(classpath, b);
case ("modifierToJSON", {Values.STRING(str), Values.BOOL(b)})
then NFApi.modifierToJSON(str, b);
case ("storeAST", {})
then Values.INTEGER(SymbolTable.storeAST());
Expand Down
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -2151,5 +2151,22 @@ algorithm
end for;
end dumpJSONChoicesAnnotation;

function modifierToJSON
input String modifier;
input Boolean prettyPrint;
output Values.Value jsonString;
protected
Absyn.Modification amod;
SCode.Mod smod;
JSON json;
algorithm
Absyn.ElementArg.MODIFICATION(modification = SOME(amod)) :=
Parser.stringMod("dummy" + modifier);
smod := AbsynToSCode.translateMod(SOME(amod),
SCode.Final.NOT_FINAL(), SCode.Each.NOT_EACH(), AbsynUtil.dummyInfo);
json := dumpJSONSCodeMod_impl(smod, InstNode.EMPTY_NODE());
jsonString := Values.STRING(JSON.toString(json, prettyPrint));
end modifierToJSON;

annotation(__OpenModelica_Interface="backend");
end NFApi;
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Expand Up @@ -49,6 +49,7 @@ GetModelInstanceReplaceable2.mos \
GetModelInstanceReplaceable3.mos \
GetModelInstanceReplaceable4.mos \
GetModelInstanceStateMachine1.mos \
ModifierToJSON1.mos \


# test that currently fail. Move up when fixed.
Expand Down
22 changes: 22 additions & 0 deletions testsuite/openmodelica/instance-API/ModifierToJSON1.mos
@@ -0,0 +1,22 @@
// name: ModifierToJSON1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

modifierToJSON("(a(redeclare MyReal y = 4, x = 3), z = 5)", prettyPrint = true);
getErrorString();

// Result:
// "{
// \"a\": {
// \"y\": {
// \"$value\": \"redeclare MyReal y = 4\"
// },
// \"x\": \"3\"
// },
// \"z\": \"5\"
// }"
// ""
// endResult

0 comments on commit d06ba1f

Please sign in to comment.