Changeset 390
- Timestamp:
- 10/10/08 14:10:05 (2 months ago)
- Files:
-
- branches/v2new/itertools.md (modified) (7 diffs)
- branches/v2new/minid/baselib.d (modified) (1 diff)
- branches/v2new/minid/bind.d (modified) (7 diffs)
- branches/v2new/minid/commandline.d (modified) (2 diffs)
- branches/v2new/minid/ex.d (modified) (1 diff)
- branches/v2new/minid/gc.d (modified) (2 diffs)
- branches/v2new/minid/interpreter.d (modified) (2 diffs)
- branches/v2new/minid/iolib.d (modified) (11 diffs)
- branches/v2new/minid/moduleslib.d (modified) (9 diffs)
- branches/v2new/minid/stringlib.d (modified) (1 diff)
- branches/v2new/minid/types.d (modified) (1 diff)
- branches/v2new/minid/vm.d (modified) (1 diff)
- branches/v2new/samples/simple.md (modified) (14 diffs)
- branches/v2new/test.d (modified) (3 diffs)
- branches/v2new/test.opt (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/v2new/itertools.md
r271 r390 30 30 { 31 31 if(#vararg == 0) 32 return function() =null32 return \-> null 33 33 34 34 local args = [vararg] 35 35 local n = #args 36 local lengths = args.map( function(x) =#x)36 local lengths = args.map(\x -> #x) 37 37 local temp = array.new(n) 38 38 … … 44 44 { 45 45 if(index >= lengths[i]) 46 return ;46 return 47 47 48 48 temp[i] = args[i][index] 49 49 } 50 50 51 51 return index, temp.expand() 52 52 } 53 53 54 54 return iterator, null, -1 55 55 } … … 65 65 { 66 66 if(idx0 is null) 67 return ;67 return 68 68 69 69 yield(idx0, vararg) … … 85 85 86 86 if(index >= times) 87 return ;87 return 88 88 89 89 return index, x … … 98 98 { 99 99 index++ 100 100 101 101 local ret = callable() 102 102 103 103 if(ret is sentinel) 104 return ;104 return 105 105 106 106 return index, ret … … 112 112 function imap(func, vararg) 113 113 { 114 local iterables = [vararg].apply(generator).each( function(i, v)v())114 local iterables = [vararg].apply(generator).each(\i, v -> v()) 115 115 local args = array.new(#iterables) 116 116 … … 124 124 125 125 if(iterables[i].isDead()) 126 return ;126 return 127 127 } 128 128 branches/v2new/minid/baselib.d
r383 r390 266 266 267 267 setUpval(t, 0); 268 pushInt(t, -1);268 pushInt(t, 0); 269 269 setUpval(t, 1); 270 270 pop(t, 2); branches/v2new/minid/bind.d
r379 r390 92 92 else static if(is(typeof(member.isStruct))) 93 93 { 94 95 96 dchar[] name = utf.toUtf32(member.Name); 97 ns[name] = MDValue(new member.Class()); 98 } 99 else 94 member.init(t); 95 fielda(t, -2, member.Name); 96 } 97 else static if(isModule) 100 98 static assert(false, "Invalid member type '" ~ member.stringof ~ "' in wrapped module '" ~ name ~ "'"); 99 else 100 static assert(false, "Invalid member type '" ~ member.stringof ~ "' in wrapped namespace '" ~ name ~ "'"); 101 101 } 102 102 } … … 191 191 static if(is(ReturnTypeOf!(funcType) == void)) 192 192 { 193 func();193 safeCode(t, func()); 194 194 return 0; 195 195 } 196 196 else 197 197 { 198 superPush(t, func());198 superPush(t, safeCode(t, func())); 199 199 return 1; 200 200 } 201 201 } 202 202 } 203 203 204 204 foreach(i, arg; args) 205 205 { 206 206 const argNum = i + 1; 207 207 208 208 if(i < numParams) 209 209 getParameter(t, argNum, args[i]); … … 215 215 static if(is(ReturnTypeOf!(funcType) == void)) 216 216 { 217 safeCode(t, func(args[0 .. argNum]));217 safeCode(t, safeCode(t, func(args[0 .. argNum]))); 218 218 return 0; 219 219 } … … 599 599 ns[k] = v; 600 600 } 601 601 602 602 return 0; 603 603 } 604 604 } 605 605 606 606 private Loader* loader; 607 607 608 608 /** 609 609 Creates an instance of this struct so you can start wrapping a module. 610 610 611 611 Params: 612 612 name = The name of the module to expose to MiniD. This can be a multi-part name, with dots (like "foo.bar"). 613 613 context = The MiniD context to load this module into. 614 614 615 615 Returns: 616 616 An instance of this struct ready to have members added. … … 632 632 /** 633 633 Wrap a function and insert it into this module's namespace. This must be a non-member D function. 634 634 635 635 Params: 636 636 f = An alias to the function you want to wrap. … … 638 638 funcType = The type of the function to wrap. This defaults to typeof(f), but you'll need to specify this 639 639 explicitly if you're wrapping an overloaded function, in order to select the proper overload. 640 640 641 641 Returns: 642 642 A chaining reference to this module. … … 656 656 const name32 = ToUTF32!(name); 657 657 loader.namespace[name32] = MDValue(new MDClosure(loader.namespace, &WrappedFunc!(f, name, funcType), name32)); 658 658 659 659 return this; 660 660 } branches/v2new/minid/commandline.d
r389 r390 219 219 word reg; 220 220 221 if(!inputFile.endsWith(".md") && !inputFile.endsWith(".mdm")) 222 reg = importModule(t, inputFile); 223 else 224 { 225 if(inputFile.endsWith(".md")) 226 { 227 scope c = new Compiler(t); 228 c.compileModule(inputFile); 229 } 221 try 222 { 223 if(!inputFile.endsWith(".md") && !inputFile.endsWith(".mdm")) 224 reg = importModule(t, inputFile); 230 225 else 231 226 { 232 scope fc = new FileConduit(inputFile, FileConduit.ReadExisting); 233 scope r = new Reader(fc); 234 deserializeModule(t, r); 235 } 236 237 lookup(t, "modules.initModule"); 238 swap(t); 227 if(inputFile.endsWith(".md")) 228 { 229 scope c = new Compiler(t); 230 c.compileModule(inputFile); 231 } 232 else 233 { 234 scope fc = new FileConduit(inputFile, FileConduit.ReadExisting); 235 scope r = new Reader(fc); 236 deserializeModule(t, r); 237 } 238 239 lookup(t, "modules.initModule"); 240 swap(t); 241 pushNull(t); 242 swap(t); 243 pushString(t, funcName(t, -1)); 244 rawCall(t, -4, 1); 245 reg = stackSize(t) - 1; 246 } 247 239 248 pushNull(t); 240 swap(t); 241 pushString(t, funcName(t, -1)); 242 rawCall(t, -4, 1); 243 reg = stackSize(t) - 1; 244 } 245 246 pushNull(t); 247 lookup(t, "modules.runMain"); 248 swap(t, -3); 249 250 foreach(a; args) 251 pushString(t, a); 252 253 rawCall(t, reg, 0); 249 lookup(t, "modules.runMain"); 250 swap(t, -3); 251 252 foreach(a; args) 253 pushString(t, a); 254 255 rawCall(t, reg, 0); 256 } 257 catch(MDException e) 258 { 259 catchException(t); 260 pop(t); 261 262 mOutput.formatln("Error: {}", e); 263 264 auto tb = getTraceback(t); 265 mOutput.formatln("{}", getString(t, tb)); 266 pop(t); 267 mOutput.newline; 268 } 254 269 } 255 270 … … 407 422 408 423 mOutput.formatln("Error: {}", e); 409 // mOutput.formatln("{}", ctx.getTracebackString()); 424 425 auto tb = getTraceback(t); 426 mOutput.formatln("{}", getString(t, tb)); 427 pop(t); 410 428 mOutput.newline; 411 429 } branches/v2new/minid/ex.d
r389 r390 529 529 } 530 530 531 /** 532 Pushes the variable that is stored in the registry with the given name onto the stack. An error will be thrown if the variable 533 does not exist in the registry. 534 535 Returns: 536 The stack index of the newly-pushed value. 537 */ 538 public word getRegistryVar(MDThread* t, char[] name) 539 { 540 pushRegistry(t); 541 field(t, -1, name); 542 insertAndPop(t, -2); 543 return stackSize(t) - 1; 544 } 545 546 /** 547 Pops the value off the top of the stack and sets it into the given registry variable. 548 */ 549 public void setRegistryVar(MDThread* t, char[] name) 550 { 551 pushRegistry(t); 552 swap(t); 553 fielda(t, -2, name); 554 pop(t); 555 } 556 531 557 // ================================================================================================================================================ 532 558 // Private branches/v2new/minid/gc.d
r387 r390 52 52 foreach(s; vm.metaStrings) 53 53 markObj(vm, s); 54 54 55 55 foreach(ref l; vm.traceback) 56 56 markObj(vm, l.file); … … 58 58 markObj(vm, vm.globals); 59 59 markObj(vm, vm.mainThread); 60 markObj(vm, vm.registry); 60 61 61 62 if(vm.isThrowing) branches/v2new/minid/interpreter.d
r389 r390 3182 3182 Params: 3183 3183 idx = The stack index of the weak reference object to dereference. 3184 3184 3185 3185 Returns: 3186 3186 The stack index of the newly-pushed value. … … 3378 3378 return 0; 3379 3379 } 3380 } 3381 3382 /** 3383 Pushes the VM's registry namespace onto the stack. The registry is sort of a hidden global namespace only accessible 3384 from native code and which native code may use for any purpose. 3385 3386 Returns: 3387 The stack index of the newly-pushed namespace. 3388 */ 3389 word pushRegistry(MDThread* t) 3390 { 3391 return pushNamespace(t, t.vm.registry); 3380 3392 } 3381 3393 branches/v2new/minid/iolib.d
r379 r390 397 397 checkObjParam(t, 0, "InputStream"); 398 398 pushExtraVal(t, 0, Members.reader); 399 auto ret = cast(Reader) getNativeObj(t, -1);399 auto ret = cast(Reader)cast(void*)getNativeObj(t, -1); 400 400 pop(t); 401 401 return ret; … … 406 406 checkObjParam(t, 0, "InputStream"); 407 407 pushExtraVal(t, 0, Members.lines); 408 auto ret = cast(LineIterator!(char)) getNativeObj(t, -1);408 auto ret = cast(LineIterator!(char))cast(void*)getNativeObj(t, -1); 409 409 pop(t); 410 410 return ret; … … 586 586 checkObjParam(t, 0, "OutputStream"); 587 587 pushExtraVal(t, 0, Members.writer); 588 auto ret = cast(Writer) getNativeObj(t, -1);588 auto ret = cast(Writer)cast(void*)getNativeObj(t, -1); 589 589 pop(t); 590 590 return ret; … … 595 595 checkObjParam(t, 0, "OutputStream"); 596 596 pushExtraVal(t, 0, Members.print); 597 auto ret = cast(Print!(char)) getNativeObj(t, -1);597 auto ret = cast(Print!(char))cast(void*)getNativeObj(t, -1); 598 598 pop(t); 599 599 return ret; … … 604 604 checkObjParam(t, 0, "OutputStream"); 605 605 pushExtraVal(t, 0, Members.output); 606 auto ret = cast(OutputStream) getNativeObj(t, -1);606 auto ret = cast(OutputStream)cast(void*)getNativeObj(t, -1); 607 607 pop(t); 608 608 return ret; … … 740 740 pop(t); 741 741 pushExtraVal(t, 1, InputStreamObj.Members.input); 742 stream = cast(InputStream) getNativeObj(t, -1);742 stream = cast(InputStream)cast(void*)getNativeObj(t, -1); 743 743 pop(t); 744 744 } … … 753 753 pushExtraVal(t, 1, StreamObj.Members.input); 754 754 pushExtraVal(t, -1, InputStreamObj.Members.input); 755 stream = cast(InputStream) getNativeObj(t, -1);755 stream = cast(InputStream)cast(void*)getNativeObj(t, -1); 756 756 pop(t, 2); 757 757 } … … 886 886 throwException(t, "Stream is not seekable."); 887 887 888 auto ret = cast(IConduit.Seek) getNativeObj(t, -1);888 auto ret = cast(IConduit.Seek)cast(void*)getNativeObj(t, -1); 889 889 pop(t); 890 890 return ret; 891 891 } 892 892 893 893 IConduit getConduit(MDThread* t) 894 894 { 895 895 checkObjParam(t, 0, "Stream"); 896 896 pushExtraVal(t, 0, Members.conduit); 897 auto ret = cast(IConduit) getNativeObj(t, -1);897 auto ret = cast(IConduit)cast(void*)getNativeObj(t, -1); 898 898 pop(t); 899 899 return ret; … … 1052 1052 pushExtraVal(t, 0, Members.input); 1053 1053 pushExtraVal(t, -1, InputStreamObj.Members.input); 1054 (cast(InputStream) getNativeObj(t, -1)).clear();1054 (cast(InputStream)cast(void*)getNativeObj(t, -1)).clear(); 1055 1055 1056 1056 return 0; … … 1071 1071 pushExtraVal(t, 0, Members.input); 1072 1072 pushExtraVal(t, -1, InputStreamObj.Members.input); 1073 (cast(InputStream) getNativeObj(t, -1)).clear();1073 (cast(InputStream)cast(void*)getNativeObj(t, -1)).clear(); 1074 1074 return 0; 1075 1075 } … … 1092 1092 pushOutput(t); 1093 1093 pushExtraVal(t, -1, OutputStreamObj.Members.output); 1094 try (cast(OutputStream) getNativeObj(t, -1)).flush(); catch{}1094 try (cast(OutputStream)cast(void*)getNativeObj(t, -1)).flush(); catch{} 1095 1095 getConduit(t).close(); 1096 1096 return 0; branches/v2new/minid/moduleslib.d
r380 r390 40 40 public void init(MDThread* t) 41 41 { 42 newTable(t); setRegistryVar(t, "modules.loading"); 43 42 44 auto ns = newNamespace(t, "modules"); 43 45 pushString(t, "."); fielda(t, ns, "path"); 44 newTable(t); fielda(t, ns, "loading");45 46 newTable(t); fielda(t, ns, "customLoaders"); 46 47 dup(t, ns); newFunctionWithEnv(t, &load, "load"); fielda(t, ns, "load"); … … 57 58 58 59 pushString(t, "loaders"); 59 dup(t, ns); newFunctionWithEnv(t, &checkCircular, "checkCircular");60 60 dup(t, ns); newFunctionWithEnv(t, &customLoad, "customLoad"); 61 61 dup(t, ns); newFunctionWithEnv(t, &checkTaken, "checkTaken"); 62 62 dup(t, ns); newFunctionWithEnv(t, &loadFiles, "loadFiles"); 63 63 64 64 version(MDDynLibs) 65 65 { 66 66 dup(t, ns); newFunctionWithEnv(t, &loadDynlib, "loadDynlib"); 67 newArrayFromStack(t, 5);67 newArrayFromStack(t, 4); 68 68 } 69 69 else 70 newArrayFromStack(t, 4);70 newArrayFromStack(t, 3); 71 71 fielda(t, ns); 72 72 newGlobal(t, "modules"); 73 73 } 74 74 75 75 package uword load(MDThread* t, uword numParams) 76 76 { … … 102 102 return commonLoad(t, name); 103 103 } 104 104 105 105 package uword commonLoad(MDThread* t, char[] name) 106 106 { 107 checkCircular(t, name); 108 109 scope(exit) 110 { 111 getRegistryVar(t, "modules.loading"); 112 pushNull(t); 113 fielda(t, -2, name); 114 pop(t); 115 } 116 107 117 auto loaders = pushGlobal(t, "loaders"); 108 118 auto num = len(t, -1); 109 119 110 120 for(uword i = 0; i < num; i++) 111 121 { … … 115 125 pushString(t, name); 116 126 rawCall(t, reg, 1); 117 127 118 128 if(isFunction(t, -1)) 119 129 { … … 148 158 assert(false); 149 159 } 150 160 151 161 package uword initModule(MDThread* t, uword numParams) 152 162 { … … 220 230 } 221 231 222 package uword checkCircular(MDThread* t, uword numParams)223 {224 checkStringParam(t, 1);225 pushGlobal(t, "loading");226 dup(t, 1);227 idx(t, -2);228 229 if(!isNull(t, -1))230 throwException(t, "Attempting to import module \"{}\" while it's in the process of being imported; is it being circularly imported?", getString(t, 1));231 232 return 0;233 }234 235 232 package uword customLoad(MDThread* t, uword numParams) 236 233 { … … 239 236 dup(t, 1); 240 237 idx(t, -2); 241 238 242 239 if(isFunction(t, -1) || isNamespace(t, -1)) 243 240 return 1; … … 245 242 return 0; 246 243 } 247 244 248 245 package uword checkTaken(MDThread* t, uword numParams) 249 246 { … … 350 347 return 0; 351 348 } 349 350 private void checkCircular(MDThread* t, char[] name) 351 { 352 getRegistryVar(t, "modules.loading"); 353 field(t, -1, name); 354 355 if(!isNull(t, -1)) 356 throwException(t, "Attempting to import module \"{}\" while it's in the process of being imported; is it being circularly imported?", name); 357 358 pop(t); 359 pushBool(t, true); 360 fielda(t, -2, name); 361 pop(t); 362 } 363 352 364 } branches/v2new/minid/stringlib.d
r389 r390 371 371 if(numParams > 0) 372 372 { 373 foreach(piece; src. delimiters(checkStringParam(t, 1)))373 foreach(piece; src.patterns(checkStringParam(t, 1))) 374 374 { 375 375 pushString(t, piece); branches/v2new/minid/types.d
r387 r390 705 705 package MDThread* curThread; 706 706 package Hash!(MDBaseObject*, MDWeakRef*) weakRefTab; 707 package MDNamespace* registry; 707 708 708 709 // The following members point into the D heap. branches/v2new/minid/vm.d
r379 r390 87 87 vm.curThread = vm.mainThread; 88 88 vm.globals = namespace.create(vm.alloc, string.create(vm, "")); 89 vm.registry = namespace.create(vm.alloc, string.create(vm, "<registry>")); 89 90 vm.formatter = new Layout!(char)(); 90 91 branches/v2new/samples/simple.md
r387 r390 1 1 module samples.simple 2 2 3 function freep(x: int) 4 { 5 if(x == 0) 6 throw "hi" 7 8 return freep(x - 1)9 } 10 11 freep(5) 12 13 /+ 14 function Enum(name: string, vararg) 15 { 16 17 } 18 19 namespace NodeType 20 { 21 Add = 022 Mul = 123 Var = 224 Num = 325 } 3 4 5 /* 6 function enum(name: string, vararg) 7 { 8 local sb = StringBuffer.clone() 9 10 sb.append("namespace ", name, "\n{\n") 11 12 for(i: 0 .. #vararg) 13 sb.append("\t", vararg[i], " = ", i, "\n") 14 15 sb.append("}") 16 17 loadString(sb.toString())() 18 } 19 20 enum("NodeType", 21 "Add", 22 "Mul", 23 "Var", 24 "Num" 25 ) 26 26 27 27 object ExpNode … … 34 34 } 35 35 36 function opCall(vararg) = :clone(vararg) 36 37 function opAdd(other: ExpNode) = AddNode(this, other) 37 38 function opMul(other: ExpNode) = MulNode(this, other) … … 43 44 { 44 45 local ret = super.clone(NodeType.Var) 45 ret.type = NodeType.Var46 46 ret.name = name 47 47 return ret … … 51 51 } 52 52 53 Var.opCall = Var.clone54 55 53 object Num : ExpNode 56 54 { … … 58 56 { 59 57 local ret = super.clone(NodeType.Num) 60 ret.type = NodeType.Num61 58 ret.val = toFloat(val) 62 59 return ret … … 66 63 } 67 64 68 Num.opCall = Num.clone 69 70 object AddNode : ExpNode 71 { 72 function clone(left: ExpNode, right: ExpNode) 73 { 74 local ret = super.clone(NodeType.Add) 65 object BinNode : ExpNode 66 { 67 function clone(type: int, left: ExpNode, right: ExpNode) 68 { 69 local ret = super.clone(type) 75 70 ret.left = left 76 71 ret.right = right 77 72 return ret 78 73 } 79 74 } 75 76 object AddNode : BinNode 77 { 78 function clone(left: ExpNode, right: ExpNode) = super.clone(NodeType.Add, left, right) 80 79 function toString() = format("({} + {})", :left, :right) 81 80 } 82 81 83 AddNode.opCall = AddNode.clone 84 85 object MulNode : ExpNode 86 { 87 function clone(left: ExpNode, right: ExpNode) 88 { 89 local ret = super.clone(NodeType.Mul) 90 ret.left = left 91 ret.right = right 92 return ret 93 } 94 82 object MulNode : BinNode 83 { 84 function clone(left: ExpNode, right: ExpNode) = super.clone(NodeType.Mul, left, right) 95 85 function toString() = format("({} * {})", :left, :right) 96 86 } 97 98 MulNode.opCall = MulNode.clone99 87 100 88 function searchVars(n: ExpNode, vars: table) … … 121 109 searchVars(root, vars) 122 110 local params = string.join(vars.keys().sort(), ", ") 123 return loadString("return \\" ~ params ~ " -> " ~ root.toString())()111 return eval("\\" ~ params ~ " -> " ~ root.toString()) 124 112 } 125 113 … … 129 117 local f = compileExp(exp) 130 118 writeln(f(3, 4)) 119 120 */ 131 121 132 122 // object Foo … … 149 139 // writefln("Foo: {}, {}", :x, :y) 150 140 // } 151 // 141 // 152 142 // function opAdd(other: Foo) 153 143 // { … … 156 146 // } 157 147 // } 158 // 148 // 159 149 // object Bar : Foo 160 150 // { … … 162 152 // 163 153 // function clone() = super.clone(-1, -10) 164 // 154 // 165 155 // function write() 166 156 // { … … 169 159 // } 170 160 // } 171 // 161 // 172 162 // local f = Foo.clone(5, 10) 173 163 // f.write(); … … 966 956 foreach(k, v; "hello") 967 957 writefln("str[", k, "] = ", v) 968 958 969 959 writefln() 970 960 … … 1159 1149 } 1160 1150 } 1161 }+/+/ +/1151 }+/+/ branches/v2new/test.d
r387 r390 5 5 6 6 import minid.api; 7 8 // version = TestArc; 7 import minid.bind; 8 9 version = TestArc; 9 10 10 11 void main() … … 31 32 auto ex = catchException(t); 32 33 Stdout.formatln("Error: {}", e); 33 34 34 35 auto tb = getTraceback(t); 35 36 Stdout.formatln("{}", getString(t, tb)); … … 148 149 ) 149 150 )(t); 150 151 151 152 WrapModule! 152 153 (
