Changeset 173

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

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib.brf

    r161 r173  
     1minid/arraylib.d 
     2minid/baselib.d 
     3minid/charlib.d 
     4minid/compiler.d 
     5minid/iolib.d 
     6minid/mathlib.d 
     7minid/minid.d 
     8minid/opcodes.d 
     9minid/oslib.d 
     10minid/regexplib.d 
     11minid/stringlib.d 
     12minid/tablelib.d 
    113minid/types.d 
    2 minid/compiler.d 
    3 minid/opcodes.d 
    414minid/utils.d 
    5 minid/stringlib.d 
    6 minid/arraylib.d 
    7 minid/tablelib.d 
    8 minid/baselib.d 
    9 minid/mathlib.d 
    10 minid/charlib.d 
    11 minid/iolib.d 
    12 -clean 
    1315-Tminid.lib 
    1416-release 
    15 -op 
    16 -lib 
     17-clean 
     18-version=MDNoDynLibs 
     19-od. 
  • trunk/mdcl.brf

    r164 r173  
    33-release 
    44-O 
    5 -inline 
    65-op 
     6-version=MDNoDynLibs 
  • 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) 
  • trunk/minid/arraylib.d

    r170 r173  
    5454        ); 
    5555 
    56         MDGlobalState().setGlobal("array"d, MDNamespace.create 
     56        MDGlobalState().globals["array"d] = MDNamespace.create 
    5757        ( 
    58             "array"d,    MDGlobalState().globals
     58            "array"d,    MDGlobalState().globals.ns
    5959            "new"d,      new MDClosure(namespace, &newArray, "array.new"), 
    6060            "range"d,    new MDClosure(namespace, &range,    "array.range") 
    61         ))
     61        )
    6262    } 
    6363 
     
    446446public void init() 
    447447{ 
    448     MDNamespace namespace = new MDNamespace("array"d, MDGlobalState().globals); 
     448    MDNamespace namespace = new MDNamespace("array"d, MDGlobalState().globals.ns); 
    449449    new ArrayLib(namespace); 
    450450    MDGlobalState().setMetatable(MDValue.Type.Array, namespace); 
  • trunk/minid/baselib.d

    r170 r173  
    574574    { 
    575575        s.push(makeNamespaceIterator(s.getContext!(MDNamespace))); 
    576  
    577576        return 1; 
    578577    } 
     
    581580    { 
    582581        if(s.isParam!("class")(0)) 
    583             s.push(makeNamespaceIterator(s.getParam!(MDClass)(0).fields)); 
     582            s.push(s.getParam!(MDClass)(0).fields); 
    584583        else if(s.isParam!("instance")(0)) 
    585             s.push(makeNamespaceIterator(s.getParam!(MDInstance)(0).fields)); 
     584            s.push(s.getParam!(MDInstance)(0).fields); 
    586585        else 
    587586            s.throwRuntimeException("Expected class or instance, not '%s'", s.getParam(0u).typeString()); 
     
    593592    { 
    594593        if(s.isParam!("class")(0)) 
    595             s.push(makeNamespaceIterator(s.getParam!(MDClass)(0).methods)); 
     594            s.push(s.getParam!(MDClass)(0).methods); 
    596595        else if(s.isParam!("instance")(0)) 
    597             s.push(makeNamespaceIterator(s.getParam!(MDInstance)(0).methods)); 
     596            s.push(s.getParam!(MDInstance)(0).methods); 
    598597        else 
    599598            s.throwRuntimeException("Expected class or instance, not '%s'", s.getParam(0u).typeString()); 
     
    704703        s.push(new MDClosure(upvalues[0].as!(MDClosure).environment, &curryClosure, "curryClosure", upvalues)); 
    705704        return 1; 
    706     } 
    707      
    708     int require(MDState s, uint numParams) 
    709     { 
    710         MDGlobalState().importModule(s.getParam!(dchar[])(0)); 
    711         return 0; 
    712705    } 
    713706     
     
    746739        MDNamespace env = s.environment(1); 
    747740        s.easyCall(new MDClosure(env, def), 1, MDValue(env)); 
     741        return 1; 
     742    } 
     743     
     744    int setModuleLoader(MDState s, uint numParams) 
     745    { 
     746        MDGlobalState().setModuleLoader(s.getParam!(dchar[])(0), s.getParam!(MDClosure)(1)); 
     747        return 0; 
     748    } 
     749     
     750    int functionEnvironment(MDState s, uint numParams) 
     751    { 
     752        MDClosure cl = s.getContext!(MDClosure); 
     753         
     754        s.push(cl.environment); 
     755 
     756        if(numParams > 0) 
     757            cl.environment = s.getParam!(MDNamespace)(0); 
     758 
    748759        return 1; 
    749760    } 
     
    11781189        lib.stringBufferClass = new BaseLib.MDStringBufferClass(); 
    11791190 
    1180         setGlobal("StringBuffer"d,  lib.stringBufferClass)
    1181         setGlobal("assert"d,        newClosure(&lib.mdassert,              "assert")); 
    1182         setGlobal("getTraceback"d,  newClosure(&lib.getTraceback,          "getTraceback")); 
    1183         setGlobal("typeof"d,        newClosure(&lib.mdtypeof,              "typeof")); 
    1184         setGlobal("fieldsOf"d,      newClosure(&lib.fieldsOf,              "fieldsOf")); 
    1185         setGlobal("methodsOf"d,     newClosure(&lib.methodsOf,             "methodsOf")); 
    1186         setGlobal("toString"d,      newClosure(&lib.mdtoString,            "toString")); 
    1187         setGlobal("rawToString"d,   newClosure(&lib.rawToString,           "rawToString")); 
    1188         setGlobal("toInt"d,         newClosure(&lib.toInt,                 "toInt")); 
    1189         setGlobal("toFloat"d,       newClosure(&lib.toFloat,               "toFloat")); 
    1190         setGlobal("toChar"d,        newClosure(&lib.toChar,                "toChar")); 
    1191         setGlobal("format"d,        newClosure(&lib.mdformat,              "format")); 
    1192         setGlobal("writefln"d,      newClosure(&lib.mdwritefln,            "writefln")); 
    1193         setGlobal("writef"d,        newClosure(&lib.mdwritef,              "writef")); 
    1194         setGlobal("writeln"d,       newClosure(&lib.writeln,               "writeln")); 
    1195         setGlobal("write"d,         newClosure(&lib.write,                 "write")); 
    1196         setGlobal("readf"d,         newClosure(&lib.readf,                 "readf")); 
    1197         setGlobal("isNull"d,        newClosure(&lib.isParam!("null"),      "isNull")); 
    1198         setGlobal("isBool"d,        newClosure(&lib.isParam!("bool"),      "isBool")); 
    1199         setGlobal("isInt"d,         newClosure(&lib.isParam!("int"),       "isInt")); 
    1200         setGlobal("isFloat"d,       newClosure(&lib.isParam!("float"),     "isFloat")); 
    1201         setGlobal("isChar"d,        newClosure(&lib.isParam!("char"),      "isChar")); 
    1202         setGlobal("isString"d,      newClosure(&lib.isParam!("string"),    "isString")); 
    1203         setGlobal("isTable"d,       newClosure(&lib.isParam!("table"),     "isTable")); 
    1204         setGlobal("isArray"d,       newClosure(&lib.isParam!("array"),     "isArray")); 
    1205         setGlobal("isFunction"d,    newClosure(&lib.isParam!("function"),  "isFunction")); 
    1206         setGlobal("isClass"d,       newClosure(&lib.isParam!("class"),     "isClass")); 
    1207         setGlobal("isInstance"d,    newClosure(&lib.isParam!("instance"),  "isInstance")); 
    1208         setGlobal("isNamespace"d,   newClosure(&lib.isParam!("namespace"), "isNamespace")); 
    1209         setGlobal("isThread"d,      newClosure(&lib.isParam!("thread"),    "isThread")); 
    1210         setGlobal("currentThread"d, newClosure(&lib.currentThread,         "currentThread")); 
    1211         setGlobal("curry"d,         newClosure(&lib.curry,                 "curry")); 
    1212         setGlobal("require"d,       newClosure(&lib.require,               "require")); 
    1213         setGlobal("loadString"d,    newClosure(&lib.loadString,            "loadString")); 
    1214         setGlobal("eval"d,          newClosure(&lib.eval,                  "eval")); 
    1215         setGlobal("loadJSON"d,      newClosure(&lib.loadJSON,              "loadJSON")); 
     1191        globals["StringBuffer"d] =    lib.stringBufferClass
     1192        globals["assert"d] =          newClosure(&lib.mdassert,              "assert"); 
     1193        globals["getTraceback"d] =    newClosure(&lib.getTraceback,          "getTraceback"); 
     1194        globals["typeof"d] =          newClosure(&lib.mdtypeof,              "typeof"); 
     1195        globals["fieldsOf"d] =        newClosure(&lib.fieldsOf,              "fieldsOf"); 
     1196        globals["methodsOf"d] =       newClosure(&lib.methodsOf,             "methodsOf"); 
     1197        globals["toString"d] =        newClosure(&lib.mdtoString,            "toString"); 
     1198        globals["rawToString"d] =     newClosure(&lib.rawToString,           "rawToString"); 
     1199        globals["toInt"d] =           newClosure(&lib.toInt,                 "toInt"); 
     1200        globals["toFloat"d] =         newClosure(&lib.toFloat,               "toFloat"); 
     1201        globals["toChar"d] =          newClosure(&lib.toChar,                "toChar"); 
     1202        globals["format"d] =          newClosure(&lib.mdformat,              "format"); 
     1203        globals["writefln"d] =        newClosure(&lib.mdwritefln,            "writefln"); 
     1204        globals["writef"d] =          newClosure(&lib.mdwritef,              "writef"); 
     1205        globals["writeln"d] =         newClosure(&lib.writeln,               "writeln"); 
     1206        globals["write"d] =           newClosure(&lib.write,                 "write"); 
     1207        globals["readf"d] =           newClosure(&lib.readf,                 "readf"); 
     1208        globals["isNull"d] =          newClosure(&lib.isParam!("null"),      "isNull"); 
     1209        globals["isBool"d] =          newClosure(&lib.isParam!("bool"),      "isBool"); 
     1210        globals["isInt"d] =           newClosure(&lib.isParam!("int"),       "isInt"); 
     1211        globals["isFloat"d] =         newClosure(&lib.isParam!("float"),     "isFloat"); 
     1212        globals["isChar"d] =          newClosure(&lib.isParam!("char"),      "isChar"); 
     1213        globals["isString"d] =        newClosure(&lib.isParam!("string"),    "isString"); 
     1214        globals["isTable"d] =         newClosure(&lib.isParam!("table"),     "isTable"); 
     1215        globals["isArray"d] =         newClosure(&lib.isParam!("array"),     "isArray"); 
     1216        globals["isFunction"d] =      newClosure(&lib.isParam!("function"),  "isFunction"); 
     1217        globals["isClass"d] =         newClosure(&lib.isParam!("class"),     "isClass"); 
     1218        globals["isInstance"d] =      newClosure(&lib.isParam!("instance"),  "isInstance"); 
     1219        globals["isNamespace"d] =     newClosure(&lib.isParam!("namespace"), "isNamespace"); 
     1220        globals["isThread"d] =        newClosure(&lib.isParam!("thread"),    "isThread"); 
     1221        globals["currentThread"d] =   newClosure(&lib.currentThread,         "currentThread"); 
     1222        globals["curry"d] =           newClosure(&lib.curry,                 "curry"); 
     1223        globals["loadString"d] =      newClosure(&lib.loadString,            "loadString"); 
     1224        globals["eval"d] =            newClosure(&lib.eval,                  "eval"); 
     1225        globals["loadJSON"d] =        newClosure(&lib.loadJSON,              "loadJSON"); 
     1226        globals["setModuleLoader"d] = newClosure(&lib.setModuleLoader,       "setModuleLoader"); 
    12161227 
    12171228        MDNamespace namespace = MDNamespace.create 
    12181229        ( 
    1219             "namespace"d, globals
     1230            "namespace"d, globals.ns
    12201231            "opApply"d,             newClosure(&lib.namespaceApply,        "namespace.opApply") 
    12211232        ); 
     
    12251236        MDNamespace thread = MDNamespace.create 
    12261237        ( 
    1227             "thread"d, globals
     1238            "thread"d, globals.ns
    12281239            "state"d,               newClosure(&lib.threadState,           "thread.state"), 
    12291240            "isInitial"d,           newClosure(&lib.isInitial,             "thread.isInitial"), 
     
    12381249 
    12391250        setMetatable(MDValue.Type.Thread, thread); 
     1251         
     1252        MDNamespace func = MDNamespace.create 
     1253        ( 
     1254            "function"d, globals.ns, 
     1255            "environment"d,         newClosure(&lib.functionEnvironment,   "function.environment") 
     1256        ); 
     1257         
     1258        setMetatable(MDValue.Type.Function, func); 
    12401259    } 
    12411260} 
  • trunk/minid/charlib.d

    r148 r173  
    126126public void init() 
    127127{ 
    128     MDNamespace namespace = new MDNamespace("char"d, MDGlobalState().globals); 
     128    MDNamespace namespace = new MDNamespace("char"d, MDGlobalState().globals.ns); 
    129129    new CharLib(namespace); 
    130130    MDGlobalState().setMetatable(MDValue.Type.Char, namespace); 
  • trunk/minid/compiler.d

    r172 r173  
    140140        Local, 
    141141        Module, 
     142        Namespace, 
    142143        Null, 
    143144        Return, 
     
    237238        Type.Local: "local", 
    238239        Type.Module: "module", 
     240        Type.Namespace: "namespace", 
    239241        Type.Null: "null", 
    240242        Type.Return: "return", 
     
    336338        stringToType["local"] = Type.Local; 
    337339        stringToType["module"] = Type.Module; 
     340        stringToType["namespace"] = Type.Namespace; 
    338341        stringToType["null"] = Type.Null; 
    339342        stringToType["return"] = Type.Return; 
     
    462465 
    463466            mEncoding = Encoding.UTF8; 
    464             //mCharacter = firstChar; 
    465             //mCharacter = 0; 
    466467        } 
    467468        else 
     
    815816                switch(mCharacter) 
    816817                { 
    817                     case 'b'
     818                    case 'b', 'B'
    818819                        nextChar(); 
    819820 
    820                         if(!isBinaryDigit()
     821                        if(!isBinaryDigit() && mCharacter != '_'
    821822                            throw new MDCompileException(mLoc, "Binary digit expected, not '%s'", mCharacter); 
    822823 
     
    845846                        return true; 
    846847 
    847                     case 'c'
     848                    case 'c', 'C'
    848849                        nextChar(); 
    849850 
    850                         if(!isOctalDigit()
     851                        if(!isOctalDigit() && mCharacter != '_'
    851852                            throw new MDCompileException(mLoc, "Octal digit expected, not '%s'", mCharacter); 
    852853 
     
    875876                        return true; 
    876877 
    877                     case 'x'
     878                    case 'x', 'X'
    878879                        nextChar(); 
    879880 
     
    35503551} 
    35513552 
     3553class NamespaceDef 
     3554{ 
     3555    protected Location mLocation; 
     3556    protected Location mEndLocation; 
     3557    protected Identifier mName; 
     3558    protected Expression mParent; 
     3559     
     3560    struct Field 
     3561    { 
     3562        dchar[] name; 
     3563        Expression initializer; 
     3564    } 
     3565 
     3566    protected Field[] mFields; 
     3567 
     3568    public this(Location location, Location endLocation, Identifier name, Expression parent, Field[] fields) 
     3569    { 
     3570        mLocation = location; 
     3571        mEndLocation = endLocation; 
     3572        mName = name; 
     3573        mParent = parent; 
     3574        mFields = fields; 
     3575    } 
     3576 
     3577    public static NamespaceDef parse(ref Token* t) 
     3578    { 
     3579        Location location = t.location; 
     3580        t = t.expect(Token.Type.Namespace); 
     3581 
     3582        Identifier name = Identifier.parse(t); 
     3583        Expression parent; 
     3584 
     3585        if(t.type == Token.Type.Colon) 
     3586        { 
     3587            t = t.nextToken; 
     3588            parent = Expression.parse(t); 
     3589        } 
     3590        else 
     3591            parent = new NullExp(t.location); 
     3592 
     3593        t = t.expect(Token.Type.LBrace); 
     3594         
     3595        Expression[dchar[]] fields; 
     3596 
     3597        void addField(Identifier name, Expression v) 
     3598        { 
     3599            if(name.mName in fields) 
     3600                throw new MDCompileException(name.mLocation, "Redeclaration of member '%s'", name.mName); 
     3601 
     3602            fields[name.mName] = v; 
     3603        } 
     3604         
     3605        while(t.type != Token.Type.RBrace) 
     3606        { 
     3607            switch(t.type) 
     3608            { 
     3609                case Token.Type.Function: 
     3610                    FuncDef fd = FuncDef.parseSimple(t); 
     3611                    addField(fd.mName, new FuncLiteralExp(fd.mLocation, fd.mEndLocation, fd)); 
     3612                    break; 
     3613 
     3614                case Token.Type.Ident: 
     3615                    Identifier id = Identifier.parse(t); 
     3616 
     3617                    Expression v; 
     3618 
     3619                    if(t.type == Token.Type.Assign) 
     3620                    { 
     3621                        t = t.nextToken; 
     3622                        v = Expression.parse(t); 
     3623                    } 
     3624                    else 
     3625                        v = new NullExp(id.mLocation); 
     3626 
     3627                    t = t.expect(Token.Type.Semicolon); 
     3628                    addField(id, v); 
     3629                    break; 
     3630 
     3631                case Token.Type.EOF: 
     3632                    throw new MDCompileException(t.location, "Namespace at ", location.toString(), " is missing its closing brace"); 
     3633 
     3634                default: 
     3635                    throw new MDCompileException(t.location, "Namespace member expected, not '%s'", t.toString()); 
     3636            } 
     3637        } 
     3638 
     3639        Field[] fieldsArray = new Field[fields.length]; 
     3640 
     3641        uint i = 0; 
     3642 
     3643        foreach(name, initializer; fields) 
     3644        { 
     3645            fieldsArray[i].name = name; 
     3646            fieldsArray[i].initializer = initializer; 
     3647            i++; 
     3648        } 
     3649 
     3650        t.expect(Token.Type.RBrace); 
     3651        Location endLocation = t.location; 
     3652        t = t.nextToken; 
     3653         
     3654        return new NamespaceDef(location, endLocation, name, parent, fieldsArray); 
     3655    } 
     3656 
     3657    public void codeGen(FuncState s) 
     3658    { 
     3659        mParent.codeGen(s); 
     3660        Exp parent; 
     3661        s.popSource(mLocation.line, parent); 
     3662        s.freeExpTempRegs(&parent); 
     3663 
     3664        uint destReg = s.pushRegister(); 
     3665        uint nameConst = s.tagConst(s.codeStringConst(mName.mName)); 
     3666        s.codeR(mLocation.line, Op.Namespace, destReg, nameConst, parent.index); 
     3667 
     3668        foreach(field; mFields) 
     3669        { 
     3670            uint index = s.tagConst(s.codeStringConst(field.name)); 
     3671 
     3672            field.initializer.codeGen(s); 
     3673            Exp val; 
     3674            s.popSource(field.initializer.mEndLocation.line, val); 
     3675            s.codeR(field.initializer.mEndLocation.line, Op.IndexAssign, destReg, index, val.index); 
     3676            s.freeExpTempRegs(&val); 
     3677        } 
     3678 
     3679        s.pushTempReg(destReg); 
     3680    } 
     3681 
     3682    public NamespaceDef fold() 
     3683    { 
     3684        foreach(ref field; mFields) 
     3685            field.initializer = field.initializer.fold(); 
     3686 
     3687        return this; 
     3688    } 
     3689} 
     3690 
    35523691class Module 
    35533692{ 
     
    35553694    protected Location mEndLocation; 
    35563695    protected ModuleDeclaration mModDecl; 
    3557     protected dchar[][] mImports; 
    35583696    protected Statement[] mStatements; 
    35593697 
    3560     public this(Location location, Location endLocation, ModuleDeclaration modDecl, dchar[][] imports, Statement[] statements) 
     3698    public this(Location location, Location endLocation, ModuleDeclaration modDecl, Statement[] statements) 
    35613699    { 
    35623700        mLocation = location; 
    35633701        mEndLocation = endLocation; 
    35643702        mModDecl = modDecl; 
    3565         mImports = imports; 
    35663703        mStatements = statements; 
    35673704    } 
     
    35713708        Location location = t.location; 
    35723709        ModuleDeclaration modDecl = ModuleDeclaration.parse(t); 
    3573  
     3710         
    35743711        List!(Statement) statements; 
    35753712 
    3576         bool[dchar[]] imports; 
    3577  
    3578         void addImport(ImportStatement imp) 
    3579         { 
    3580             imports[Identifier.toLongString(imp.mNames)] = true; 
    3581             statements.add(imp); 
    3582         } 
    3583  
    35843713        while(t.type != Token.Type.EOF) 
    3585         { 
    3586             if(t.type == Token.Type.Import) 
    3587                 addImport(ImportStatement.parse(t)); 
    3588             else 
    3589                 statements.add(Statement.parse(t)); 
    3590         } 
     3714            statements.add(Statement.parse(t)); 
    35913715 
    35923716        t.expect(Token.Type.EOF); 
    35933717 
    3594         return new Module(location, t.location, modDecl, imports.keys.sort, statements.toArray()); 
     3718        return new Module(location, t.location, modDecl, statements.toArray()); 
    35953719    } 
    35963720 
     
    35983722    { 
    35993723        MDModuleDef def = new MDModuleDef(); 
    3600  
     3724         
    36013725        def.mName = Identifier.toLongString(mModDecl.mNames); 
    3602         def.mImports = mImports; 
    3603  
    3604         FuncState fs = new FuncState(mLocation, "module " ~ Identifier.toLongString(mModDecl.mNames)); 
     3726 
     3727        FuncState fs = new FuncState(mLocation, "module " ~ mModDecl.mNames[$ - 1].mName); 
    36053728        fs.mIsVararg = true; 
    36063729 
     
    36313754    { 
    36323755        writefln("module %s", Identifier.toLongString(mModDecl.mNames)); 
    3633  
    3634         foreach(name; mImports) 
    3635             writefln("import %s", name); 
    36363756    } 
    36373757} 
     
    36933813                return ExpressionStatement.parse(t); 
    36943814 
    3695             case Token.Type.Local, Token.Type.Global, Token.Type.Function, Token.Type.Class
     3815            case Token.Type.Local, Token.Type.Global, Token.Type.Function, Token.Type.Class, Token.Type.Namespace
    36963816                return DeclarationStatement.parse(t); 
     3817                 
     3818            case Token.Type.Import: 
     3819                return ImportStatement.parse(t); 
    36973820 
    36983821            case Token.Type.LBrace: 
     
    37213844            case Token.Type.Switch: 
    37223845                return SwitchStatement.parse(t); 
    3723                  
    3724             //case Token.Type.Case: 
    3725             //  return CaseStatement.parse(t); 
    3726  
    3727             //case Token.Type.Default: 
    3728             //  return DefaultStatement.parse(t); 
    37293846 
    37303847            case Token.Type.Continue: 
     
    37643881class ImportStatement : Statement 
    37653882{ 
    3766     protected Identifier[] mNames
     3883    protected Expression mExpr
    37673884    protected Identifier[] mSymbols; 
    37683885 
    3769     public this(Location location, Location endLocation, Identifier[] names, Identifier[] symbols) 
     3886    public this(Location location, Location endLocation, Expression expr, Identifier[] symbols) 
    37703887    { 
    37713888        super(location, endLocation); 
    3772  
    3773         mNames = names
     3889         
     3890        mExpr = expr
    37743891        mSymbols = symbols; 
    37753892    } 
     
    37803897 
    37813898        t = t.expect(Token.Type.Import); 
    3782  
    3783         Identifier[] names; 
    3784         names ~= Identifier.parse(t); 
    3785  
    3786         while(t.type == Token.Type.Dot) 
     3899         
     3900        Expression expr; 
     3901         
     3902        if(t.type == Token.Type.LParen) 
    37873903        { 
    37883904            t = t.nextToken; 
     3905            expr = Expression.parse(t); 
     3906            t = t.expect(Token.Type.RParen); 
     3907        } 
     3908        else 
     3909        { 
     3910            Identifier[] names; 
    37893911            names ~= Identifier.parse(t); 
    3790         } 
    3791          
     3912     
     3913            while(t.type == Token.Type.Dot) 
     3914            { 
     3915                t = t.nextToken; 
     3916                names ~= Identifier.parse(t); 
     3917            } 
     3918             
     3919            expr = new StringExp(location, Identifier.toLongString(names)); 
     3920        } 
     3921 
    37923922        Identifier[] symbols; 
    37933923 
     
    38083938        t = t.nextToken; 
    38093939 
    3810         return new ImportStatement(location, endLocation, names, symbols); 
    3811     } 
    3812      
    3813     public char[] toString() 
    3814     { 
    3815         return "import " ~ utf.toUTF8(Identifier.toLongString(mNames)) ~ ";"; 
     3940        return new ImportStatement(location, endLocation, expr, symbols); 
    38163941    } 
    38173942 
    38183943    public override void codeGen(FuncState s) 
    38193944    { 
    3820         if(mSymbols.length == 0) 
    3821             return; 
    3822  
    38233945        foreach(i, sym; mSymbols) 
    38243946        { 
     
    38323954            } 
    38333955        } 
    3834  
     3956         
     3957        uint firstReg = s.nextRegister(); 
     3958         
    38353959        foreach(sym; mSymbols) 
    3836         { 
    3837             uint destReg = s.nextRegister(); 
     3960            s.pushRegister(); 
     3961 
     3962        uint importReg = s.nextRegister(); 
     3963 
     3964        mExpr.codeGen(s); 
     3965        Exp src; 
     3966        s.popSource(mLocation.line, src); 
     3967 
     3968        assert(s.nextRegister() == importReg, "bad import regs"); 
     3969 
     3970        s.codeR(mLocation.line, Op.Import, importReg, src.index, 0); 
     3971         
     3972        for(int reg = firstReg + mSymbols.length - 1; reg >= firstReg; reg--) 
     3973            s.popRegister(reg); 
     3974 
     3975        foreach(i, sym; mSymbols) 
     3976        { 
     3977            s.codeR(mLocation.line, Op.Index, firstReg + i, importReg, s.tagConst(s.codeStringConst(sym.mName))); 
     3978 
     3979            /*uint destReg = s.nextRegister(); 
    38383980 
    38393981            s.pushVar(mNames[0]); 
    3840              
     3982 
    38413983            foreach(name; mNames[1 .. $]) 
    38423984            { 
     
    38483990            s.popField(mLocation.line, sym); 
    38493991 
    3850             s.popMoveTo(mLocation.line, destReg); 
     3992            s.popMoveTo(mLocation.line, destReg);*/ 
    38513993            s.insertLocal(sym); 
    38523994        } 
     
    39854127            else if(t.nextToken.type == Token.Type.Class) 
    39864128                return ClassDecl.parse(t); 
     4129            else if(t.nextToken.type == Token.Type.Namespace) 
     4130                return NamespaceDecl.parse(t); 
    39874131            else 
    39884132                throw new MDCompileException(location, "Illegal token '%s' after '%s'", t.nextToken.toString(), t.toString()); 
     
    39924136        else if(t.type == Token.Type.Class) 
    39934137            return ClassDecl.parse(t); 
     4138        else if(t.type == Token.Type.Namespace) 
     4139            return NamespaceDecl.parse(t); 
    39944140        else 
    39954141            throw new MDCompileException(location, "Declaration expected, not '%s'", t.toString()); 
     
    42514397    } 
    42524398 
     4399    public override void codeGen(FuncState s) 
     4400    { 
     4401        if(mProtection == Protection.Local) 
     4402        { 
     4403            s.insertLocal(mDef.mName); 
     4404            s.activateLocals(1); 
     4405            s.pushVar(mDef.mName); 
     4406        } 
     4407        else 
     4408        { 
     4409            assert(mProtection == Protection.Global); 
     4410            s.pushNewGlobal(mDef.mName); 
     4411        } 
     4412 
     4413        mDef.codeGen(s); 
     4414        s.popAssign(mEndLocation.line); 
     4415    } 
     4416     
     4417    public override Declaration fold() 
     4418    { 
     4419        mDef = mDef.fold(); 
     4420        return this; 
     4421    } 
     4422} 
     4423 
     4424class NamespaceDecl : Declaration 
     4425{ 
     4426    protected NamespaceDef mDef; 
     4427 
     4428    public this(Location location, Protection protection, NamespaceDef def) 
     4429    { 
     4430        super(location, def.mEndLocation, protection); 
     4431 
     4432        mDef = def; 
     4433    } 
     4434 
     4435    public static NamespaceDecl parse(ref Token* t) 
     4436    { 
     4437        Location location = t.location; 
     4438        Protection protection = Protection.Local; 
     4439         
     4440        if(t.type == Token.Type.Global) 
     4441        { 
     4442            protection = Protection.Global; 
     4443            t = t.nextToken; 
     4444        } 
     4445        else if(t.type == Token.Type.Local)