11package com .annimon .ownlang .parser .ast ;
22
3+ import com .annimon .ownlang .exceptions .TypeException ;
4+ import com .annimon .ownlang .lib .ArrayValue ;
5+ import com .annimon .ownlang .lib .Types ;
6+ import com .annimon .ownlang .lib .Value ;
37import com .annimon .ownlang .modules .Module ;
48import java .lang .reflect .Method ;
59
@@ -21,24 +25,55 @@ public UseStatement(Expression expression) {
2125 @ Override
2226 public void execute () {
2327 super .interruptionCheck ();
28+ final Value value = expression .eval ();
29+ switch (value .type ()) {
30+ case Types .ARRAY :
31+ for (Value module : ((ArrayValue ) value )) {
32+ loadModule (module .asString ());
33+ }
34+ break ;
35+ case Types .STRING :
36+ loadModule (value .asString ());
37+ break ;
38+ default :
39+ throw new TypeException ("Array or string required" );
40+ }
41+ }
42+
43+ private void loadModule (String name ) {
2444 try {
25- final String moduleName = expression .eval ().asString ();
26- final Module module = (Module ) Class .forName (String .format (PACKAGE , moduleName , moduleName )).newInstance ();
45+ final Module module = (Module ) Class .forName (String .format (PACKAGE , name , name )).newInstance ();
2746 module .init ();
2847 } catch (Exception ex ) {
2948 throw new RuntimeException (ex );
3049 }
3150 }
3251
3352 public void loadConstants () {
53+ final Value value = expression .eval ();
54+ switch (value .type ()) {
55+ case Types .ARRAY :
56+ for (Value module : ((ArrayValue ) value )) {
57+ loadConstants (module .asString ());
58+ }
59+ break ;
60+ case Types .STRING :
61+ loadConstants (value .asString ());
62+ break ;
63+ default :
64+ throw new TypeException ("Array or string required" );
65+ }
66+ }
67+
68+ private void loadConstants (String moduleName ) {
3469 try {
35- final String moduleName = expression .eval ().asString ();
3670 final Class <?> moduleClass = Class .forName (String .format (PACKAGE , moduleName , moduleName ));
3771 final Method method = moduleClass .getMethod (INIT_CONSTANTS_METHOD );
3872 if (method != null ) {
3973 method .invoke (this );
4074 }
4175 } catch (Exception ex ) {
76+ // ignore
4277 }
4378 }
4479
0 commit comments