1818import org .piccode .rt .PiccodeBoolean ;
1919import org .piccode .rt .PiccodeException ;
2020import org .piccode .rt .PiccodeInfo ;
21+ import org .piccode .rt .PiccodeModule ;
2122import org .piccode .rt .PiccodeUnit ;
2223import org .piccode .rt .PiccodeValue ;
2324
2425/**
2526 *
2627 * @author hexaredecimal
2728 */
28- public class ImportAst extends Ast {
29+ public class ImportModuleCreateAst extends Ast {
2930
30- public String path ;
31- public List <Ast > lifted ;
31+ public String name ;
32+ public ImportAst import_ ;
33+ private String path ;
3234
33- public ImportAst (String path ) {
34- this .path = path ;
35- this .lifted = new ArrayList <>();
36- }
37-
38- public ImportAst (String path , List <Ast > lifted ) {
39- this .path = path ;
40- this .lifted = lifted ;
41- }
42-
43- @ Override
44- public String toString () {
45- return "import " + path ;
35+ public ImportModuleCreateAst (String name , ImportAst import_ ) {
36+ this .name = name ;
37+ this .import_ = import_ ;
38+ this .path = import_ .path ;
4639 }
4740
4841 @ Override
4942 public PiccodeValue execute (Integer frame ) {
50- var nodes = loadModuleFromStdLib (path , frame );
43+ var nodes = import_ .loadModuleFromStdLib (path , frame );
44+
45+ var final_nodes = new ArrayList <Ast >();
46+ var lifted = import_ .lifted ;
5147
5248 if (lifted .isEmpty ()) {
53- nodes .forEach (node -> node .execute (frame ));
54- return new PiccodeUnit ();
49+ nodes .forEach (node -> {
50+ if (node instanceof ModuleAst mod && mod .name .equals (name )) {
51+ final_nodes .addAll (mod .nodes );
52+ } else {
53+ final_nodes .add (node );
54+ }
55+ });
56+
57+ var top = Context .top .getValue (name );
58+ if (top != null && top instanceof PiccodeModule module ) {
59+ final_nodes .addAll (module .nodes );
60+ var mod = new ModuleAst (name , final_nodes );
61+ var result = mod .execute (frame );
62+ Context .top .putLocal (name , result );
63+ return result ;
64+ } else {
65+ var mod = new ModuleAst (name , final_nodes );
66+ var result = mod .execute (frame );
67+ return result ;
68+ }
5569 }
5670
5771 for (var liftedNode : lifted ) {
58- executeLifted (nodes , liftedNode , frame );
72+ executeLifted (nodes , liftedNode , frame , final_nodes );
73+ }
74+
75+ var top = Context .top .getValue (name );
76+ if (top != null && top instanceof PiccodeModule module ) {
77+ final_nodes .addAll (module .nodes );
78+ var mod = new ModuleAst (name , final_nodes );
79+ var result = mod .execute (frame );
80+ Context .top .putLocal (name , result );
81+ return result ;
82+ } else {
83+ var mod = new ModuleAst (name , final_nodes );
84+ var result = mod .execute (frame );
85+ return result ;
5986 }
6087
61- return new PiccodeUnit ();
6288 }
6389
64- private void executeLifted (List <Ast > moduleNodes , Ast liftedNode , Integer frame ) {
90+ private void executeLifted (List <Ast > moduleNodes , Ast liftedNode , Integer frame , List < Ast > final_nodes ) {
6591 if (liftedNode instanceof ImportId id ) {
6692 for (var node : moduleNodes ) {
6793 if (node instanceof ImportAst imp ) {
6894 imp .execute (frame );
6995 }
7096 if (node instanceof StatementList sts ) {
71- executeLifted (sts .nodes , id , frame );
97+ executeLifted (sts .nodes , id , frame , final_nodes );
7298 return ;
7399 }
74100 if (node instanceof FunctionAst func && func .name .equals (id .text )) {
75- func . execute ( frame );
101+ final_nodes . add ( node );
76102 return ;
77103 }
78104 if (node instanceof ModuleAst mod && mod .name .equals (id .text )) {
79- mod .execute (frame );
105+ if (id .text .equals (name )) {
106+ final_nodes .addAll (mod .nodes );
107+ return ;
108+ }
109+
110+ mod .createSymbol = false ;
111+ final_nodes .add (node );
80112 return ;
81113 }
82114 }
@@ -91,12 +123,12 @@ private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame)
91123 imp .execute (frame );
92124 }
93125 if (node instanceof StatementList sts ) {
94- executeLifted (sts .nodes , lift , frame );
126+ executeLifted (sts .nodes , lift , frame , final_nodes );
95127 return ;
96128 }
97129 if (node instanceof ModuleAst mod && mod .name .equals (lift .text )) {
98130 // Recursively execute lifted symbols from inside the module
99- executeLifted (mod .nodes , lift .nodes , frame );
131+ executeLifted (mod .nodes , lift .nodes , frame , final_nodes );
100132 return ;
101133 }
102134 if (node instanceof FunctionAst func && func .name .equals (lift .text )) {
@@ -116,103 +148,14 @@ private void executeLifted(List<Ast> moduleNodes, Ast liftedNode, Integer frame)
116148 }
117149 }
118150
119- private void executeLifted (List <Ast > moduleNodes , List <Ast > nestedLifted , Integer frame ) {
151+ private void executeLifted (List <Ast > moduleNodes , List <Ast > nestedLifted , Integer frame , List < Ast > final_nodes ) {
120152 for (var lifted : nestedLifted ) {
121- executeLifted (moduleNodes , lifted , frame );
122- }
123- }
124-
125- private List <Ast > loadModuleFromStdLib (String module , Integer frame ) {
126- var storage = getAppStorage ();
127- var paths = List .of (storage , "./" );
128- var nodes = new ArrayList <Ast >();
129- var _file = findModule (module , paths );
130- if (_file == null ) {
131- var err = new PiccodeException (file , line , column , "Invalid module " + module .replaceAll ("/" , "." ));
132- err .frame = frame ;
133- throw err ;
134- }
135-
136- var path_ = _file .getPath ();
137- if (Context .isImportCached (path_ )) {
138- return Context .getCached (path_ );
139- }
140-
141- var files = _file .listFiles ();
142-
143- for (var fp : files ) {
144- if (fp .getName ().endsWith (".pics" )) {
145- var code = readFile (fp );
146- if (code == null ) {
147- var err = new PiccodeException (file , line , column , "Invalid module " + module .replaceAll ("/" , "." ));
148- err .frame = frame ;
149- throw err ;
150- }
151- var program = (StatementList ) _import (fp .getAbsolutePath (), code );
152- var noImports = program .nodes
153- .stream ()
154- .filter ((value ) -> {
155- if (value instanceof ImportAst ) {
156- value .execute (frame );
157- }
158- return !(value instanceof ImportAst );
159- })
160- .toList ();
161- nodes .addAll (noImports );
162- }
163- }
164- if (!Context .isImportCached (path )) {
165- Context .cacheImport (path , lifted );
166- }
167- return nodes ;
168- }
169-
170- private Ast _import (String file , String code ) {
171- Context .top .putLocal ("true" , new PiccodeBoolean ("true" ));
172- Context .top .putLocal ("false" , new PiccodeBoolean ("false" ));
173-
174- try {
175- return Compiler .program (file , code );
176- } catch (PiccodeException e ) {
177- throw e ;
178- }
179- }
180-
181- private String readFile (File fp ) {
182- StringBuilder sb = new StringBuilder ();
183- Scanner sc ;
184- try {
185- sc = new Scanner (fp );
186- while (sc .hasNext ()) {
187- sb .append (sc .nextLine ()).append ("\n " );
188- }
189- return sb .toString ();
190- } catch (FileNotFoundException ex ) {
191- return null ;
153+ executeLifted (moduleNodes , lifted , frame , final_nodes );
192154 }
193155 }
194156
195157 @ Override
196158 public String codeGen (TargetEnvironment target ) {
197- return "" ;
198- }
199-
200- private String getAppStorage () {
201- try {
202- String path = ImportAst .class .getProtectionDomain ().getCodeSource ().getLocation ().toURI ().getPath ();
203- return new File (path ).getParentFile ().getPath ();
204- } catch (URISyntaxException ex ) {
205- return "./" ;
206- }
207- }
208-
209- private File findModule (String module , List <String > of ) {
210- for (var dir : of ) {
211- var fp = new File (dir + "/" + module );
212- if (fp .isDirectory ()) {
213- return fp ;
214- }
215- }
216- return null ;
159+ throw new UnsupportedOperationException ("Not supported yet." ); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
217160 }
218161}
0 commit comments