Skip to content

Commit

Permalink
Implement storeAST/restoreAST API (#9306)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Aug 18, 2022
1 parent 7fd66db commit 7ff3a83
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 1 deletion.
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -4406,6 +4406,23 @@ function getModelInstance
external "builtin";
end getModelInstance;

function storeAST
output Integer id;
external "builtin";
annotation(preferredView="text",Documentation(info="<html>
<p>Stores the AST and returns an id that can be used to restore it with restoreAST.</p>
</html>"));
end storeAST;

function restoreAST
input Integer id;
output Boolean success;
external "builtin";
annotation(preferredView="text",Documentation(info="<html>
<p>Restores an AST that was previously stored with storeAST.</p>
</html>"));
end restoreAST;

// OMSimulator API calls
type oms_system = enumeration(oms_system_none,oms_system_tlm, oms_system_wc,oms_system_sc);
type oms_causality = enumeration(oms_causality_input, oms_causality_output, oms_causality_parameter, oms_causality_bidir, oms_causality_undefined);
Expand Down
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Expand Up @@ -4658,6 +4658,23 @@ function getModelInstance
external "builtin";
end getModelInstance;

function storeAST
output Integer id;
external "builtin";
annotation(preferredView="text",Documentation(info="<html>
<p>Stores the AST and returns an id that can be used to restore it with restoreAST.</p>
</html>"));
end storeAST;

function restoreAST
input Integer id;
output Boolean success;
external "builtin";
annotation(preferredView="text",Documentation(info="<html>
<p>Restores an AST that was previously stored with storeAST.</p>
</html>"));
end restoreAST;

// OMSimulator API calls
type oms_system = enumeration(oms_system_none,oms_system_tlm, oms_system_wc,oms_system_sc);
type oms_causality = enumeration(oms_causality_input, oms_causality_output, oms_causality_parameter, oms_causality_bidir, oms_causality_undefined);
Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -3105,6 +3105,12 @@ algorithm
case ("getModelInstance", {Values.CODE(Absyn.C_TYPENAME(classpath)), Values.BOOL(b)})
then NFApi.getModelInstance(classpath, b);
case ("storeAST", {})
then Values.INTEGER(SymbolTable.storeAST());
case ("restoreAST", {Values.INTEGER(integer = n)})
then Values.BOOL(SymbolTable.restoreAST(n));
end matchcontinue;
end cevalInteractiveFunctions4;
Expand Down
30 changes: 29 additions & 1 deletion OMCompiler/Compiler/Script/SymbolTable.mo
Expand Up @@ -40,6 +40,7 @@ import Absyn;
import GlobalScript;
import FCore;
import SCode;
import Vector;

protected
import AvlTreeStringString;
Expand All @@ -60,14 +61,17 @@ record SYMBOLTABLE
Absyn.Program ast "ast ; The ast" ;
Option<SCode.Program> explodedAst "the explodedAst is invalidated every time the program is updated";
list<GlobalScript.Variable> vars "List of variables with values" ;
Vector<Absyn.Program> cachedAsts;
end SYMBOLTABLE;

function reset
type Program = Absyn.Program;
algorithm
setGlobalRoot(Global.symbolTable, SYMBOLTABLE(
ast=Absyn.PROGRAM({},Absyn.TOP()),
explodedAst=NONE(),
vars={}
vars={},
cachedAsts=Vector.new<Program>()
));
updateUriMapping({});
end reset;
Expand Down Expand Up @@ -241,6 +245,30 @@ algorithm
update(table);
end deleteVarFirstEntry;

function storeAST
output Integer id;
protected
SymbolTable table;
algorithm
table := get();
Vector.push(table.cachedAsts, getAbsyn());
id := Vector.size(table.cachedAsts);
end storeAST;

function restoreAST
input Integer id;
output Boolean success;
protected
SymbolTable table;
algorithm
table := get();
success := id <= Vector.size(table.cachedAsts) and id > 0;

if success then
setAbsyn(Vector.get(table.cachedAsts, id));
end if;
end restoreAST;

protected

function isVarNamed
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/interactive-API/Makefile
Expand Up @@ -73,6 +73,7 @@ setComponentModifierValue.mos \
setSourceFileListFile.mos \
showDoc.mos \
showStructuralAnnotations.mos \
StoreAST.mos \
stringSplit.mos \
strings.mos \
variables.mos \
Expand Down
70 changes: 70 additions & 0 deletions testsuite/openmodelica/interactive-API/StoreAST.mos
@@ -0,0 +1,70 @@
// name: StoreAST
// keywords:
// status: correct
// cflags: -d=newInst
//
// Tests the storeAST/restoreAST API.
//

loadString("
model A
end A;
");

list();
id1 := storeAST();

loadString("
model B
end B;
");

list();
id2 := storeAST();

loadString("
model C
end C;
");

list();
restoreAST(id2);
list();
restoreAST(id1);
list();
restoreAST(99);
restoreAST(-17);

// Result:
// true
// "model A
// end A;"
// 1
// true
// "model B
// end B;
//
// model A
// end A;"
// 2
// true
// "model C
// end C;
//
// model B
// end B;
//
// model A
// end A;"
// true
// "model B
// end B;
//
// model A
// end A;"
// true
// "model A
// end A;"
// false
// false
// endResult

0 comments on commit 7ff3a83

Please sign in to comment.