2626import org .piccode .rt .modules .PiccodeSystemModule ;
2727import org .piccode .rt .modules .PiccodeTimeModule ;
2828import org .piccode .rt .modules .PiccodeTupleModule ;
29+ import org .piccode .rt .modules .PiccodeTypesModule ;
30+ import org .piccode .rt .modules .PiccodeVirtualModule ;
2931
3032/**
3133 *
@@ -40,13 +42,7 @@ public static PiccodeValue compile(String file, String code) {
4042 public static PiccodeValue compile (String file , String code , List <PiccodeValue > args ) {
4143 try {
4244 var result = program (file , code );
43-
44- var scope_id = new IdentifierAst ("globalScope" );
45- scope_id .column = 0 ;
46- scope_id .line = 1 ;
47- scope_id .file = file ;
48-
49- prepareGlobalScope (scope_id );
45+ prepareGlobalScope (file );
5046 addGlobalFunctions ();
5147
5248 PiccodeValue res = new PiccodeUnit ();
@@ -59,17 +55,20 @@ public static PiccodeValue compile(String file, String code, List<PiccodeValue>
5955 }
6056
6157 if (has_main ) {
62- return new CallAst (new IdentifierAst ("main" ), List .of ()).execute ();
58+ var _result = new CallAst (new IdentifierAst ("main" ), List .of ()).execute ();
59+ Context .top .dropStackFrame ();
60+ return _result ;
6361 }
6462
6563 Context .top .dropStackFrame ();
6664 return res ;
6765 } catch (PiccodeException e ) {
66+ Context .top .dropStackFrame ();
6867 e .reportError ();
6968 //e.printStackTrace();
7069 return new PiccodeUnit ();
7170 } catch (Exception rte ) {
72- // Context.top.dropStackFrame();
71+ Context .top .dropStackFrame ();
7372 rte .printStackTrace ();
7473 return new PiccodeUnit ();
7574 }
@@ -102,13 +101,22 @@ private static String sanitizeSourceFile(String code) {
102101 return String .join ("\n " , lines );
103102 }
104103
105- public static void prepareGlobalScope (Ast parent ) {
106- Context .top .pushStackFrame (parent );
107- Context .top .addGlobal ("true" , new PiccodeBoolean ("true" ));
104+ public static void prepareGlobalScope (String file ) {
105+ prepareGlobalScope (file , "globalScope" );
106+ }
107+
108+ public static void prepareGlobalScope (String file , String globalScopeName ) {
109+ var scope_id = new IdentifierAst (globalScopeName );
110+ scope_id .column = 0 ;
111+ scope_id .line = 1 ;
112+ scope_id .file = file ;
113+ Context .top .pushStackFrame (scope_id );
114+ Context .top .putLocal ("true" , new PiccodeBoolean ("true" ));
108115 Context .top .putLocal ("false" , new PiccodeBoolean ("false" ));
109116 addGlobalFunctions ();
110117 }
111118
119+
112120 private static void addGlobalFunctions () {
113121 PiccodeIOModule .addFunctions ();
114122 PiccodeArrayModule .addFunctions ();
@@ -117,6 +125,8 @@ private static void addGlobalFunctions() {
117125 PiccodeMathModule .addFunctions ();
118126 PiccodeSystemModule .addFunctions ();
119127 PiccodeTimeModule .addFunctions ();
128+ PiccodeTypesModule .addFunctions ();
129+ PiccodeVirtualModule .addFunctions ();
120130 }
121131
122132}
0 commit comments