diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index 0536bdeba5f..f98650beade 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -1201,10 +1201,140 @@ protected algorithm if isSome(annOpt) then SOME(ann) := annOpt; - json := JSON.addPair("annotation", dumpJSONMod(ann.modification), json); + json := JSON.addPair("annotation", dumpJSONAnnotationMod(ann.modification), json); end if; end dumpJSONAnnotationOpt; +function dumpJSONAnnotationMod + input SCode.Mod mod; + output JSON json; +algorithm + json := match mod + case SCode.Mod.MOD() + then dumpJSONAnnotationSubMods(mod.subModLst); + + else JSON.makeNull(); + end match; +end dumpJSONAnnotationMod; + +function dumpJSONAnnotationSubMods + input list subMods; + output JSON json = JSON.makeNull(); +algorithm + for m in subMods loop + json := dumpJSONAnnotationSubMod(m, json); + end for; +end dumpJSONAnnotationSubMods; + +function dumpJSONAnnotationSubMod + input SCode.SubMod subMod; + input output JSON json; +protected + String name; + SCode.Mod mod; + Absyn.Exp binding; +algorithm + SCode.SubMod.NAMEMOD(ident = name, mod = mod) := subMod; + + () := match mod + case SCode.Mod.MOD(binding = SOME(binding)) + algorithm + json := JSON.addPair(name, dumpJSONAbsynExpression(binding), json); + then + (); + + case SCode.Mod.MOD() + algorithm + json := JSON.addPair(name, dumpJSONAnnotationSubMods(mod.subModLst), json); + then + (); + + else (); + end match; +end dumpJSONAnnotationSubMod; + +function dumpJSONAbsynExpression + input Absyn.Exp exp; + output JSON json; +protected + Integer i; + String r; +algorithm + json := match exp + case Absyn.Exp.INTEGER() then JSON.makeInteger(exp.value); + case Absyn.Exp.REAL() then JSON.makeNumber(stringReal(exp.value)); + case Absyn.Exp.CREF() then dumpJSONAbsynCref(exp.componentRef); + case Absyn.Exp.STRING() then JSON.makeString(exp.value); + case Absyn.Exp.BOOL() then JSON.makeBoolean(exp.value); + + case Absyn.Exp.UNARY(op = Absyn.Operator.UMINUS(), exp = Absyn.Exp.INTEGER(value = i)) + then JSON.makeInteger(-i); + + case Absyn.Exp.UNARY(op = Absyn.Operator.UMINUS(), exp = Absyn.Exp.REAL(value = r)) + then JSON.makeNumber(-stringReal(r)); + + case Absyn.Exp.CALL() + algorithm + json := JSON.emptyObject(); + json := JSON.addPair("kind", JSON.makeString("call"), json); + json := JSON.addPair("name", dumpJSONAbsynCref(exp.function_), json); + json := dumpJSONAbsynFunctionArgs(exp.functionArgs, json); + then + json; + + case Absyn.Exp.ARRAY() + algorithm + json := JSON.emptyArray(listLength(exp.arrayExp)); + for e in exp.arrayExp loop + json := JSON.addElement(dumpJSONAbsynExpression(e), json); + end for; + then + json; + + else JSON.makeString(Dump.printExpStr(exp)); + end match; +end dumpJSONAbsynExpression; + +function dumpJSONAbsynCref + input Absyn.ComponentRef cref; + output JSON json; +algorithm + json := JSON.makeString(Dump.printComponentRefStr(cref)); +end dumpJSONAbsynCref; + +function dumpJSONAbsynFunctionArgs + input Absyn.FunctionArgs args; + input output JSON json; +protected + JSON json_args; +algorithm + () := match args + case Absyn.FunctionArgs.FUNCTIONARGS() + algorithm + if not listEmpty(args.args) then + json_args := JSON.makeNull(); + for arg in args.args loop + json_args := JSON.addElement(dumpJSONAbsynExpression(arg), json_args); + end for; + + json := JSON.addPair("args", json_args, json); + end if; + + if not listEmpty(args.argNames) then + json_args := JSON.makeNull(); + for arg in args.argNames loop + json_args := JSON.addPair(arg.argName, dumpJSONAbsynExpression(arg.argValue), json_args); + end for; + + json := JSON.addPair("namedArgs", json_args, json); + end if; + then + (); + + else (); + end match; +end dumpJSONAbsynFunctionArgs; + function dumpJSONConnections input Sections sections; output JSON json = JSON.makeNull(); diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceAnnotation1.mos b/testsuite/openmodelica/instance-API/GetModelInstanceAnnotation1.mos index 6497d33f4e8..f1c27f4f88d 100644 --- a/testsuite/openmodelica/instance-API/GetModelInstanceAnnotation1.mos +++ b/testsuite/openmodelica/instance-API/GetModelInstanceAnnotation1.mos @@ -21,18 +21,54 @@ getModelInstance(M, prettyPrint=true); // \"name\": \"M\", // \"annotation\": { // \"Icon\": { -// \"modifiers\": { -// \"graphics\": { -// \"value\": \"{Rectangle(extent = {{-66, 78}, {70, -56}}, lineColor = {28, 108, 200})}\" +// \"graphics\": [ +// { +// \"kind\": \"call\", +// \"name\": \"Rectangle\", +// \"namedArgs\": { +// \"extent\": [ +// [ +// -66, +// 78 +// ], +// [ +// 70, +// -56 +// ] +// ], +// \"lineColor\": [ +// 28, +// 108, +// 200 +// ] +// } // } -// } +// ] // }, // \"Diagram\": { -// \"modifiers\": { -// \"graphics\": { -// \"value\": \"{Ellipse(extent = {{-62, 68}, {56, -60}}, lineColor = {28, 108, 200})}\" +// \"graphics\": [ +// { +// \"kind\": \"call\", +// \"name\": \"Ellipse\", +// \"namedArgs\": { +// \"extent\": [ +// [ +// -62, +// 68 +// ], +// [ +// 56, +// -60 +// ] +// ], +// \"lineColor\": [ +// 28, +// 108, +// 200 +// ] +// } // } -// } +// ] // } // } // }" diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceConnection1.mos b/testsuite/openmodelica/instance-API/GetModelInstanceConnection1.mos index f609e3d713a..0307d4538ce 100644 --- a/testsuite/openmodelica/instance-API/GetModelInstanceConnection1.mos +++ b/testsuite/openmodelica/instance-API/GetModelInstanceConnection1.mos @@ -193,11 +193,24 @@ getModelInstance(M, prettyPrint = true); // \"rhs\": \"c3\", // \"annotation\": { // \"Line\": { -// \"modifiers\": { -// \"points\": { -// \"value\": \"{{-25, 30}, {10, 30}, {10, -20}, {40, -20}}\" -// } -// } +// \"points\": [ +// [ +// -25, +// 30 +// ], +// [ +// 10, +// 30 +// ], +// [ +// 10, +// -20 +// ], +// [ +// 40, +// -20 +// ] +// ] // } // } // },