Changeset 173 for trunk/minid
- Timestamp:
- 05/27/07 16:59:34 (2 years ago)
- Files:
-
- 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)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
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) 4446 t = t.nextToken; 4447 4448 NamespaceDef def = NamespaceDef.parse(t); 4449 4450 return new NamespaceDecl(location, protection, def); 4451 } 4452 4253 4453 public override void codeGen(FuncState s) 4254 4454 { … … 6050 6250 _commonParse: 6051 6251 t = t.nextToken; 6052 exp2 = OrOrExp.parse(t);6252 exp2 = Expression.parse(t); 6053 6253 exp1 = new OpEqExp(location, exp2.mEndLocation, type, exp1, exp2); 6054 6254 break; … … 6056 6256 case Token.Type.CatEq: 6057 6257 t = t.nextToken; 6058 exp2 = OrOrExp.parse(t);6258 exp2 = Expression.parse(t); 6059 6259 exp1 = new CatEqExp(location, exp2.mEndLocation, exp1, exp2); 6060 6260 break; … … 7689 7889 s.freeExpTempRegs(&src); 7690 7890 assert(s.nextRegister() == funcReg); 7691 //assert(src.index < funcReg + 2);7692 7891 7693 7892 s.pushRegister(); … … 7933 8132 break; 7934 8133 8134 case Token.Type.Namespace: 8135 exp = NamespaceCtorExp.parse(t); 8136 break; 8137 7935 8138 case Token.Type.Yield: 7936 8139 exp = YieldExp.parse(t); … … 8527 8730 { 8528 8731 super(location, endLocation); 8529 8732 8530 8733 if(fields.length > 0) 8531 8734 mEndLocation = fields[$ - 1][1].mEndLocation; … … 8832 9035 } 8833 9036 9037 class NamespaceCtorExp : PrimaryExp 9038 { 9039 protected NamespaceDef mDef; 9040 9041 public this(Location location, Location endLocation, NamespaceDef def) 9042 { 9043 super(location, endLocation); 9044 9045 mDef = def; 9046 } 9047 9048 public static NamespaceCtorExp parse(ref Token* t) 9049 { 9050 Location location = t.location; 9051 NamespaceDef def = NamespaceDef.parse(t); 9052 9053 return new NamespaceCtorExp(location, def.mEndLocation, def); 9054 } 9055 9056 public override void codeGen(FuncState s) 9057 { 9058 mDef.codeGen(s); 9059 } 9060 9061 public InstRef* codeCondition(FuncState s) 9062 { 9063 throw new MDCompileException(mLocation, "Cannot use namespace constructor as a condition"); 9064 } 9065 9066 public override Expression fold() 9067 { 9068 mDef = mDef.fold(); 9069 return this; 9070 } 9071 } 9072 8834 9073 class YieldExp : PrimaryExp 8835 9074 { trunk/minid/iolib.d
r170 r173 557 557 public void init() 558 558 { 559 MDNamespace namespace = new MDNamespace("io"d, MDGlobalState().globals );559 MDNamespace namespace = new MDNamespace("io"d, MDGlobalState().globals.ns); 560 560 new IOLib(namespace); 561 MDGlobalState(). setGlobal("io"d, namespace);561 MDGlobalState().globals["io"d] = namespace; 562 562 } trunk/minid/mathlib.d
r170 r173 289 289 public void init() 290 290 { 291 MDNamespace namespace = new MDNamespace("math"d, MDGlobalState().globals );291 MDNamespace namespace = new MDNamespace("math"d, MDGlobalState().globals.ns); 292 292 new MathLib(namespace); 293 MDGlobalState(). setGlobal("math"d, namespace);293 MDGlobalState().globals["math"d] = namespace; 294 294 } trunk/minid/minid.d
r167 r173 24 24 module minid.minid; 25 25 26 public import minid.compiler; 26 27 public import minid.types; 27 public import minid.compiler;28 28 public import minid.utils; 29 29 30 import arraylib = minid.arraylib; 30 31 import baselib = minid.baselib; 31 import stringlib = minid.stringlib;32 import arraylib = minid.arraylib;33 import tablelib = minid.tablelib;34 import mathlib = minid.mathlib;35 32 import charlib = minid.charlib; 36 33 import iolib = minid.iolib; 34 import mathlib = minid.mathlib; 37 35 import oslib = minid.oslib; 38 36 import regexplib = minid.regexplib; 39 37 import stringlib = minid.stringlib; 38 import tablelib = minid.tablelib; 39 40 import file = std.file; 40 41 import path = std.path; 41 import file = std.file;42 import std.string; 42 43 import utf = std.utf; 43 import std.string;44 44 45 45 /** … … 95 95 libs = An ORing together of any standard libraries you want to load (see the MDStdlib enum). 96 96 Defaults to MDStdlib.All. 97 97 98 98 Returns: 99 99 The main thread state associated with the global state. … … 104 104 { 105 105 MDGlobalState(); 106 MDGlobalState().tryPath = &tryPath; 107 version(MDNoDynLibs){}else MDGlobalState().tryDynLibPath = &tryDynLibPath; 106 108 107 109 baselib.init(); … … 118 120 if(libs & MDStdlib.Math) 119 121 mathlib.init(); 122 123 if(libs & MDStdlib.OS) 124 oslib.init(); 125 126 if(libs & MDStdlib.Regexp) 127 regexplib.init(); 120 128 121 129 if(libs & MDStdlib.String) … … 124 132 if(libs & MDStdlib.Table) 125 133 tablelib.init(); 126 127 if(libs & MDStdlib.OS)128 oslib.init();129 130 if(libs & MDStdlib.Regexp)131 regexplib.init();132 133 MDGlobalState().registerModuleLoader(&MDFileLoader().load);134 134 } 135 135 … … 137 137 } 138 138 139 /** 140 The default module loader for MiniD. It will load modules from the filesystem based on their 141 name and given search paths. 142 */ 143 class MDFileLoader 144 { 145 private static MDFileLoader instance; 146 private bool[char[]] mPaths; 147 148 /// This class is a singleton, and this static opCall overload will return the instance. 149 public static MDFileLoader opCall() 150 { 151 if(instance is null) 152 instance = new MDFileLoader(); 153 154 return instance; 155 } 156 157 private this() 158 { 159 160 } 161 162 /// Adds a search path to the list of search paths. 163 public void addPath(char[] path) 164 { 165 mPaths[path] = true; 166 } 167 168 private bool load(MDState s, dchar[] name, dchar[] fromModule) 169 { 170 char[][] elements = split(utf.toUTF8(name), "."); 171 172 MDModuleDef def = tryPath(file.getcwd(), elements); 173 174 if(def is null) 175 { 176 foreach(customPath, dummy; mPaths) 177 { 178 def = tryPath(customPath, elements); 179 180 if(def !is null) 181 break; 182 } 183 } 184 185 if(def is null) 186 return false; 187
