Changeset 173 for trunk/mdcl.d

Show
Ignore:
Timestamp:
05/27/07 16:59:34 (2 years ago)
Author:
JarrettBillingsley
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/mdcl.d

    r170 r173  
    4545    writefln(); 
    4646    writefln("Flags:"); 
    47     writefln("\t-i   Enter interactive mode, after executing any script file."); 
    48     writefln("\t-v   Print the version of the CLI."); 
    49     writefln("\t-h   Print this message and end."); 
     47    writefln("\t-i      Enter interactive mode, after executing any script file."); 
     48    writefln("\t-v      Print the version of the CLI."); 
     49    writefln("\t-h      Print this message and end."); 
     50    writefln("\t-I path Specifies an import path to search when importing modules."); 
    5051    writefln(); 
    5152    writefln("If mdcl is called without any arguments, it will be as if you passed it"); 
    5253    writefln("the -v and -i arguments (it will print the version and enter interactive"); 
    5354    writefln("mode)."); 
     55    writefln(); 
     56    writefln("If the filename has no extension, it will be treated as a MiniD import-"); 
     57    writefln("style module name.  So \"a.b\" will look for a module named b in the a"); 
     58    writefln("directory.  The -I flag also affects the search paths used for this."); 
    5459    writefln(); 
    5560    writefln("When passing a filename followed by args, all the args will be available"); 
     
    8186    char[] inputFile; 
    8287    char[][] scriptArgs; 
     88    char[][] importPaths; 
    8389 
    8490    if(args.length == 1) 
     
    107113                printUsage(); 
    108114                return; 
     115                 
     116            case "-I": 
     117                i++; 
     118                 
     119                if(i >= args.length) 
     120                { 
     121                    writefln("-I must be followed by a path"); 
     122                    printUsage(); 
     123                    return; 
     124                } 
     125                 
     126                importPaths ~= args[i]; 
     127                break; 
    109128 
    110129            default: 
     
    123142 
    124143    MDState state = MDInitialize(); 
     144     
     145    foreach(path; importPaths) 
     146        MDGlobalState().addImportPath(path); 
    125147 
    126148    if(inputFile.length > 0) 
     
    132154        else if(inputFile.length >= 4 && inputFile[$ - 4 .. $] == ".mdm") 
    133155            def = MDModuleDef.loadFromFile(inputFile); 
     156 
     157        MDValue[] params = new MDValue[scriptArgs.length]; 
     158 
     159        foreach(i, arg; scriptArgs) 
     160            params[i] = arg; 
     161 
     162        if(def is null) 
     163        { 
     164            try 
     165            { 
     166                if(MDGlobalState().loadModuleFromFile(state, utf.toUTF32(inputFile), params) is null) 
     167                    writefln("Error: could not find module '%s'", inputFile); 
     168            } 
     169            catch(MDException e) 
     170            { 
     171                writefln("Error: ", e); 
     172                writefln(MDState.getTracebackString()); 
     173            } 
     174        } 
    134175        else 
    135176        { 
    136             char[] sourceName = inputFile ~ ".md"; 
    137             char[] moduleName = inputFile ~ ".mdm"; 
    138  
    139             if(file.exists(sourceName)) 
    140             { 
    141                 if(file.exists(moduleName)) 
    142                 { 
    143                     long sourceTime; 
    144                     long moduleTime; 
    145                     long dummy; 
    146  
    147                     file.getTimes(sourceName, dummy, dummy, sourceTime); 
    148                     file.getTimes(moduleName, dummy, dummy, moduleTime); 
    149  
    150                     if(sourceTime > moduleTime) 
    151                         def = compileModule(sourceName); 
    152                     else 
    153                         def = MDModuleDef.loadFromFile(moduleName); 
    154                 } 
    155                 else 
    156                     def = compileModule(sourceName); 
    157             } 
    158             else 
    159                 def = MDModuleDef.loadFromFile(moduleName); 
    160         } 
    161  
    162         MDNamespace ns = MDGlobalState().registerModule(def, state); 
    163  
    164         MDValue[] params = new MDValue[scriptArgs.length]; 
    165          
    166         foreach(i, v; scriptArgs) 
    167             params[i] = v; 
    168  
    169         try 
    170             MDGlobalState().staticInitModule(def, ns, state, params); 
    171         catch(MDException e) 
    172         { 
    173             writefln("Error: ", e); 
    174             writefln(MDState.getTracebackString()); 
     177            try 
     178                MDGlobalState().initializeModule(state, def, params); 
     179            catch(MDException e) 
     180            { 
     181                writefln("Error: ", e); 
     182                writefln(MDState.getTracebackString()); 
     183            } 
    175184        } 
    176185    } 
     
    181190        bool run = true; 
    182191 
    183         MDGlobalState().setGlobal("exit"d, MDGlobalState().newClosure 
     192        MDGlobalState().globals["exit"d] = MDGlobalState().newClosure 
    184193        ( 
    185194            (MDState s, uint numParams) 
     
    188197                return 0; 
    189198            }, "exit" 
    190         ))
     199        )
    191200 
    192201        version(Windows) 
     
    233242            { 
    234243                scope closure = MDGlobalState().newClosure(def); 
    235                 state.easyCall(closure, 0, MDValue(MDGlobalState().globals)); 
     244                state.easyCall(closure, 0, MDValue(MDGlobalState().globals.ns)); 
    236245            } 
    237246            catch(MDException e)