Changeset 173
- Timestamp:
- 05/27/07 16:59:34 (2 years ago)
- Files:
-
- trunk/lib.brf (modified) (1 diff)
- trunk/mdcl.brf (modified) (1 diff)
- trunk/mdcl.d (modified) (8 diffs)
- trunk/mdcl.zip (modified) (previous)
- trunk/minid/arraylib.d (modified) (2 diffs)
- trunk/minid/baselib.d (modified) (8 diffs)
- trunk/minid/charlib.d (modified) (1 diff)
- trunk/minid/compiler.d (modified) (28 diffs)
- trunk/minid/iolib.d (modified) (1 diff)
- trunk/minid/mathlib.d (modified) (1 diff)
- trunk/minid/minid.d (modified) (7 diffs)
- trunk/minid/opcodes.d (modified) (8 diffs)
- trunk/minid/oslib.d (modified) (1 diff)
- trunk/minid/regexplib.d (modified) (1 diff)
- trunk/minid/stringlib.d (modified) (2 diffs)
- trunk/minid/tablelib.d (modified) (3 diffs)
- trunk/minid/types.d (modified) (28 diffs)
- trunk/minid/utils.d (modified) (1 diff)
- trunk/minid2.txt (modified) (11 diffs)
- trunk/minidc.brf (modified) (1 diff)
- trunk/samples/arrays.md (modified) (1 diff)
- trunk/samples/simple.md (modified) (2 diffs)
- trunk/samples/test.md (added)
- trunk/test.brf (modified) (1 diff)
- trunk/test.d (modified) (3 diffs)
- trunk/test.opt (modified) (previous)
- trunk/tests/base.md (modified) (2 diffs)
- trunk/tests/compiler/compiler.md (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib.brf
r161 r173 1 minid/arraylib.d 2 minid/baselib.d 3 minid/charlib.d 4 minid/compiler.d 5 minid/iolib.d 6 minid/mathlib.d 7 minid/minid.d 8 minid/opcodes.d 9 minid/oslib.d 10 minid/regexplib.d 11 minid/stringlib.d 12 minid/tablelib.d 1 13 minid/types.d 2 minid/compiler.d3 minid/opcodes.d4 14 minid/utils.d 5 minid/stringlib.d6 minid/arraylib.d7 minid/tablelib.d8 minid/baselib.d9 minid/mathlib.d10 minid/charlib.d11 minid/iolib.d12 -clean13 15 -Tminid.lib 14 16 -release 15 -op 16 -lib 17 -clean 18 -version=MDNoDynLibs 19 -od. trunk/mdcl.brf
r164 r173 3 3 -release 4 4 -O 5 -inline6 5 -op 6 -version=MDNoDynLibs trunk/mdcl.d
r170 r173 45 45 writefln(); 46 46 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."); 50 51 writefln(); 51 52 writefln("If mdcl is called without any arguments, it will be as if you passed it"); 52 53 writefln("the -v and -i arguments (it will print the version and enter interactive"); 53 54 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."); 54 59 writefln(); 55 60 writefln("When passing a filename followed by args, all the args will be available"); … … 81 86 char[] inputFile; 82 87 char[][] scriptArgs; 88 char[][] importPaths; 83 89 84 90 if(args.length == 1) … … 107 113 printUsage(); 108 114 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; 109 128 110 129 default: … … 123 142 124 143 MDState state = MDInitialize(); 144 145 foreach(path; importPaths) 146 MDGlobalState().addImportPath(path); 125 147 126 148 if(inputFile.length > 0) … … 132 154 else if(inputFile.length >= 4 && inputFile[$ - 4 .. $] == ".mdm") 133 155 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 } 134 175 else 135 176 { 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 } 175 184 } 176 185 } … … 181 190 bool run = true; 182 191 183 MDGlobalState(). setGlobal("exit"d,MDGlobalState().newClosure192 MDGlobalState().globals["exit"d] = MDGlobalState().newClosure 184 193 ( 185 194 (MDState s, uint numParams) … … 188 197 return 0; 189 198 }, "exit" 190 ) );199 ); 191 200 192 201 version(Windows) … … 233 242 { 234 243 scope closure = MDGlobalState().newClosure(def); 235 state.easyCall(closure, 0, MDValue(MDGlobalState().globals ));244 state.easyCall(closure, 0, MDValue(MDGlobalState().globals.ns)); 236 245 } 237 246 catch(MDException e) trunk/minid/arraylib.d
r170 r173 54 54 ); 55 55 56 MDGlobalState(). setGlobal("array"d,MDNamespace.create56 MDGlobalState().globals["array"d] = MDNamespace.create 57 57 ( 58 "array"d, MDGlobalState().globals ,58 "array"d, MDGlobalState().globals.ns, 59 59 "new"d, new MDClosure(namespace, &newArray, "array.new"), 60 60 "range"d, new MDClosure(namespace, &range, "array.range") 61 ) );61 ); 62 62 } 63 63 … … 446 446 public void init() 447 447 { 448 MDNamespace namespace = new MDNamespace("array"d, MDGlobalState().globals );448 MDNamespace namespace = new MDNamespace("array"d, MDGlobalState().globals.ns); 449 449 new ArrayLib(namespace); 450 450 MDGlobalState().setMetatable(MDValue.Type.Array, namespace); trunk/minid/baselib.d
r170 r173 574 574 { 575 575 s.push(makeNamespaceIterator(s.getContext!(MDNamespace))); 576 577 576 return 1; 578 577 } … … 581 580 { 582 581 if(s.isParam!("class")(0)) 583 s.push( makeNamespaceIterator(s.getParam!(MDClass)(0).fields));582 s.push(s.getParam!(MDClass)(0).fields); 584 583 else if(s.isParam!("instance")(0)) 585 s.push( makeNamespaceIterator(s.getParam!(MDInstance)(0).fields));584 s.push(s.getParam!(MDInstance)(0).fields); 586 585 else 587 586 s.throwRuntimeException("Expected class or instance, not '%s'", s.getParam(0u).typeString()); … … 593 592 { 594 593 if(s.isParam!("class")(0)) 595 s.push( makeNamespaceIterator(s.getParam!(MDClass)(0).methods));594 s.push(s.getParam!(MDClass)(0).methods); 596 595 else if(s.isParam!("instance")(0)) 597 s.push( makeNamespaceIterator(s.getParam!(MDInstance)(0).methods));596 s.push(s.getParam!(MDInstance)(0).methods); 598 597 else 599 598 s.throwRuntimeException("Expected class or instance, not '%s'", s.getParam(0u).typeString()); … … 704 703 s.push(new MDClosure(upvalues[0].as!(MDClosure).environment, &curryClosure, "curryClosure", upvalues)); 705 704 return 1; 706 }707 708 int require(MDState s, uint numParams)709 {710 MDGlobalState().importModule(s.getParam!(dchar[])(0));711 return 0;712 705 } 713 706 … … 746 739 MDNamespace env = s.environment(1); 747 740 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 748 759 return 1; 749 760 } … … 1178 1189 lib.stringBufferClass = new BaseLib.MDStringBufferClass(); 1179 1190 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"); 1216 1227 1217 1228 MDNamespace namespace = MDNamespace.create 1218 1229 ( 1219 "namespace"d, globals ,1230 "namespace"d, globals.ns, 1220 1231 "opApply"d, newClosure(&lib.namespaceApply, "namespace.opApply") 1221 1232 ); … … 1225 1236 MDNamespace thread = MDNamespace.create 1226 1237 ( 1227 "thread"d, globals ,1238 "thread"d, globals.ns, 1228 1239 "state"d, newClosure(&lib.threadState, "thread.state"), 1229 1240 "isInitial"d, newClosure(&lib.isInitial, "thread.isInitial"), … … 1238 1249 1239 1250 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); 1240 1259 } 1241 1260 } trunk/minid/charlib.d
r148 r173 126 126 public void init() 127 127 { 128 MDNamespace namespace = new MDNamespace("char"d, MDGlobalState().globals );128 MDNamespace namespace = new MDNamespace("char"d, MDGlobalState().globals.ns); 129 129 new CharLib(namespace); 130 130 MDGlobalState().setMetatable(MDValue.Type.Char, namespace); trunk/minid/compiler.d
r172 r173 140 140 Local, 141 141 Module, 142 Namespace, 142 143 Null, 143 144 Return, … … 237 238 Type.Local: "local", 238 239 Type.Module: "module", 240 Type.Namespace: "namespace", 239 241 Type.Null: "null", 240 242 Type.Return: "return", … … 336 338 stringToType["local"] = Type.Local; 337 339 stringToType["module"] = Type.Module; 340 stringToType["namespace"] = Type.Namespace; 338 341 stringToType["null"] = Type.Null; 339 342 stringToType["return"] = Type.Return; … … 462 465 463 466 mEncoding = Encoding.UTF8; 464 //mCharacter = firstChar;465 //mCharacter = 0;466 467 } 467 468 else … … 815 816 switch(mCharacter) 816 817 { 817 case 'b' :818 case 'b', 'B': 818 819 nextChar(); 819 820 820 if(!isBinaryDigit() )821 if(!isBinaryDigit() && mCharacter != '_') 821 822 throw new MDCompileException(mLoc, "Binary digit expected, not '%s'", mCharacter); 822 823 … … 845 846 return true; 846 847 847 case 'c' :848 case 'c', 'C': 848 849 nextChar(); 849 850 850 if(!isOctalDigit() )851 if(!isOctalDigit() && mCharacter != '_') 851 852 throw new MDCompileException(mLoc, "Octal digit expected, not '%s'", mCharacter); 852 853 … … 875 876 return true; 876 877 877 case 'x' :878 case 'x', 'X': 878 879 nextChar(); 879 880 … … 3550 3551 } 3551 3552 3553 class 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 3552 3691 class Module 3553 3692 { … … 3555 3694 protected Location mEndLocation; 3556 3695 protected ModuleDeclaration mModDecl; 3557 protected dchar[][] mImports;3558 3696 protected Statement[] mStatements; 3559 3697 3560 public this(Location location, Location endLocation, ModuleDeclaration modDecl, dchar[][] imports,Statement[] statements)3698 public this(Location location, Location endLocation, ModuleDeclaration modDecl, Statement[] statements) 3561 3699 { 3562 3700 mLocation = location; 3563 3701 mEndLocation = endLocation; 3564 3702 mModDecl = modDecl; 3565 mImports = imports;3566 3703 mStatements = statements; 3567 3704 } … … 3571 3708 Location location = t.location; 3572 3709 ModuleDeclaration modDecl = ModuleDeclaration.parse(t); 3573 3710 3574 3711 List!(Statement) statements; 3575 3712 3576 bool[dchar[]] imports;3577 3578 void addImport(ImportStatement imp)3579 {3580 imports[Identifier.toLongString(imp.mNames)] = true;3581 statements.add(imp);3582 }3583 3584 3713 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)); 3591 3715 3592 3716 t.expect(Token.Type.EOF); 3593 3717 3594 return new Module(location, t.location, modDecl, imports.keys.sort,statements.toArray());3718 return new Module(location, t.location, modDecl, statements.toArray()); 3595 3719 } 3596 3720 … … 3598 3722 { 3599 3723 MDModuleDef def = new MDModuleDef(); 3600 3724 3601 3725 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); 3605 3728 fs.mIsVararg = true; 3606 3729 … … 3631 3754 { 3632 3755 writefln("module %s", Identifier.toLongString(mModDecl.mNames)); 3633 3634 foreach(name; mImports)3635 writefln("import %s", name);3636 3756 } 3637 3757 } … … 3693 3813 return ExpressionStatement.parse(t); 3694 3814 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: 3696 3816 return DeclarationStatement.parse(t); 3817 3818 case Token.Type.Import: 3819 return ImportStatement.parse(t); 3697 3820 3698 3821 case Token.Type.LBrace: … … 3721 3844 case Token.Type.Switch: 3722 3845 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);3729 3846 3730 3847 case Token.Type.Continue: … … 3764 3881 class ImportStatement : Statement 3765 3882 { 3766 protected Identifier[] mNames;3883 protected Expression mExpr; 3767 3884 protected Identifier[] mSymbols; 3768 3885 3769 public this(Location location, Location endLocation, Identifier[] names, Identifier[] symbols)3886 public this(Location location, Location endLocation, Expression expr, Identifier[] symbols) 3770 3887 { 3771 3888 super(location, endLocation); 3772 3773 m Names = names;3889 3890 mExpr = expr; 3774 3891 mSymbols = symbols; 3775 3892 } … … 3780 3897 3781 3898 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) 3787 3903 { 3788 3904 t = t.nextToken; 3905 expr = Expression.parse(t); 3906 t = t.expect(Token.Type.RParen); 3907 } 3908 else 3909 { 3910 Identifier[] names; 3789 3911 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 3792 3922 Identifier[] symbols; 3793 3923 … … 3808 3938 t = t.nextToken; 3809 3939 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); 3816 3941 } 3817 3942 3818 3943 public override void codeGen(FuncState s) 3819 3944 { 3820 if(mSymbols.length == 0)3821 return;3822 3823 3945 foreach(i, sym; mSymbols) 3824 3946 { … … 3832 3954 } 3833 3955 } 3834 3956 3957 uint firstReg = s.nextRegister(); 3958 3835 3959 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(); 3838 3980 3839 3981 s.pushVar(mNames[0]); 3840 3982 3841 3983 foreach(name; mNames[1 .. $]) 3842 3984 { … … 3848 3990 s.popField(mLocation.line, sym); 3849 3991 3850 s.popMoveTo(mLocation.line, destReg); 3992 s.popMoveTo(mLocation.line, destReg);*/ 3851 3993 s.insertLocal(sym); 3852 3994 } … … 3985 4127 else if(t.nextToken.type == Token.Type.Class) 3986 4128 return ClassDecl.parse(t); 4129 else if(t.nextToken.type == Token.Type.Namespace) 4130 return NamespaceDecl.parse(t); 3987 4131 else 3988 4132 throw new MDCompileException(location, "Illegal token '%s' after '%s'", t.nextToken.toString(), t.toString()); … … 3992 4136 else if(t.type == Token.Type.Class) 3993 4137 return ClassDecl.parse(t); 4138 else if(t.type == Token.Type.Namespace) 4139 return NamespaceDecl.parse(t); 3994 4140 else 3995 4141 throw new MDCompileException(location, "Declaration expected, not '%s'", t.toString()); … … 4251 4397 } 4252 4398 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 4424 class 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)
