Changeset 167
- Timestamp:
- 05/20/07 14:55:46 (2 years ago)
- Files:
-
- trunk/mdcl.d (modified) (1 diff)
- trunk/mdcl.zip (modified) (previous)
- trunk/minid/baselib.d (modified) (5 diffs)
- trunk/minid/compiler.d (modified) (144 diffs)
- trunk/minid/iolib.d (modified) (4 diffs)
- trunk/minid/minid.d (modified) (6 diffs)
- trunk/minid/opcodes.d (modified) (9 diffs)
- trunk/minid/regexplib.d (modified) (4 diffs)
- trunk/minid/tablelib.d (modified) (2 diffs)
- trunk/minid/types.d (modified) (132 diffs)
- trunk/minid/utils.d (modified) (7 diffs)
- trunk/minid2.txt (modified) (2 diffs)
- trunk/samples/simple.md (modified) (1 diff)
- trunk/samples/speed.md (modified) (1 diff)
- trunk/test.d (modified) (1 diff)
- trunk/test.opt (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/mdcl.d
r163 r167 125 125 else if(inputFile.length >= 4 && inputFile[$ - 4 .. $] == ".mdm") 126 126 def = MDModuleDef.loadFromFile(inputFile); 127 128 MDClosure cl = MDGlobalState().initModule(def, false, state); 129 130 uint funcReg = state.push(cl); 131 132 state.push(cl.environment); 133 134 foreach(arg; scriptArgs) 135 state.push(arg); 127 else 128 { 129 char[] sourceName = inputFile ~ ".md"; 130 char[] moduleName = inputFile ~ ".mdm"; 131 132 if(file.exists(sourceName)) 133 { 134 if(file.exists(moduleName)) 135 { 136 long sourceTime; 137 long moduleTime; 138 long dummy; 139 140 file.getTimes(sourceName, dummy, dummy, sourceTime); 141 file.getTimes(moduleName, dummy, dummy, moduleTime); 142 143 if(sourceTime > moduleTime) 144 def = compileModule(sourceName); 145 else 146 def = MDModuleDef.loadFromFile(moduleName); 147 } 148 else 149 def = compileModule(sourceName); 150 } 151 else 152 def = MDModuleDef.loadFromFile(moduleName); 153 } 154 155 MDNamespace ns = MDGlobalState().registerModule(def, state); 156 157 MDValue[] params = new MDValue[scriptArgs.length]; 158 159 foreach(i, v; scriptArgs) 160 params[i] = v; 136 161 137 162 try 138 state.call(funcReg, scriptArgs.length + 1, 0);163 MDGlobalState().staticInitModule(def, ns, state, params); 139 164 catch(MDException e) 140 165 { trunk/minid/baselib.d
r163 r167 39 39 MDValue[] output; 40 40 41 void outputValue( inoutMDValue val)41 void outputValue(ref MDValue val) 42 42 { 43 43 output ~= val; … … 406 406 MDValue[] ret = s.safeCode(baseUnFormat(s, s.getParam!(dchar[])(0), din)); 407 407 408 foreach( inoutv; ret)408 foreach(ref v; ret) 409 409 s.push(v); 410 410 … … 711 711 int require(MDState s, uint numParams) 712 712 { 713 MDGlobalState().importModule(s.getParam!( char[])(0));713 MDGlobalState().importModule(s.getParam!(dchar[])(0)); 714 714 return 0; 715 715 } … … 824 824 { 825 825 i.append(other); 826 return 0;826 continue; 827 827 } 828 828 } … … 1139 1139 if(lo < 0 || lo > hi || hi >= mLength) 1140 1140 throw new MDException("Invalid indices: %d .. %d", lo, hi); 1141 1141 1142 1142 return mBuffer[lo .. hi]; 1143 1143 } trunk/minid/compiler.d
r163 r167 1 1 2 /****************************************************************************** 2 3 License: … … 157 158 Cat, 158 159 CatEq, 160 Cmp3, 159 161 Mul, 160 162 MulEq, … … 198 200 Semicolon, 199 201 Length, 202 Question, 200 203 201 204 Ident, … … 252 255 Type.Cat: "~", 253 256 Type.CatEq: "~=", 257 Type.Cmp3: "<=>", 254 258 Type.Mul: "*", 255 259 Type.MulEq: "*=", … … 293 297 Type.Semicolon: ";", 294 298 Type.Length: "#", 299 Type.Question: "?", 295 300 296 301 Type.Ident: "Identifier", … … 1394 1399 { 1395 1400 nextChar(); 1396 token.type = Token.Type.LE; 1401 1402 if(mCharacter == '>') 1403 { 1404 nextChar(); 1405 token.type = Token.Type.Cmp3; 1406 } 1407 else 1408 token.type = Token.Type.LE; 1397 1409 } 1398 1410 else if(mCharacter == '<') … … 1551 1563 case '?': 1552 1564 nextChar(); 1553 1554 if(mCharacter != '=') 1555 throw new MDCompileException(tokenLoc, "'?' expected to be followed by '='"); 1556 1557 nextChar(); 1558 1559 token.type = Token.Type.DefaultEq; 1565 1566 if(mCharacter == '=') 1567 { 1568 nextChar(); 1569 token.type = Token.Type.DefaultEq; 1570 } 1571 else 1572 token.type = Token.Type.Question; 1573 1560 1574 return token; 1561 1575 … … 1600 1614 int ival; 1601 1615 1602 bool type= readNumLiteral(false, fval, ival);1603 1604 if( type== false)1616 bool isInt = readNumLiteral(false, fval, ival); 1617 1618 if(isInt == false) 1605 1619 { 1606 1620 token.floatValue = fval; … … 1703 1717 char[] toString() 1704 1718 { 1705 char[] typename; 1706 1707 switch(type) 1708 { 1709 case ExpType.Null: typename = "Null"; break; 1710 case ExpType.True: typename = "True"; break; 1711 case ExpType.False: typename = "False"; break; 1712 case ExpType.Const: typename = "Const"; break; 1713 case ExpType.Var: typename = "Var"; break; 1714 case ExpType.NewGlobal: typename = "NewGlobal"; break; 1715 case ExpType.Indexed: typename = "Indexed"; break; 1716 case ExpType.Sliced: typename = "Sliced"; break; 1717 case ExpType.Vararg: typename = "Vararg"; break; 1718 case ExpType.Closure: typename = "Closure"; break; 1719 case ExpType.Call: typename = "Call"; break; 1720 case ExpType.Yield: typename = "Yield"; break; 1721 case ExpType.NeedsDest: typename = "NeedsDest"; break; 1722 case ExpType.Src: typename = "Src"; break; 1723 } 1724 1725 return string.format("%s (%d, %d, %d) : (%s, %s, %s)", typename, index, index2, index3, isTempReg, isTempReg2, isTempReg3); 1719 static const char[][] typeNames = 1720 [ 1721 ExpType.Null: "Null", 1722 ExpType.True: "True", 1723 ExpType.False: "False", 1724 ExpType.Const: "Const", 1725 ExpType.Var: "Var", 1726 ExpType.NewGlobal: "NewGlobal", 1727 ExpType.Indexed: "Indexed", 1728 ExpType.Sliced: "Sliced", 1729 ExpType.Vararg: "Vararg", 1730 ExpType.Closure: "Closure", 1731 ExpType.Call: "Call", 1732 ExpType.Yield: "Yield", 1733 ExpType.NeedsDest: "NeedsDest", 1734 ExpType.Src: "Src" 1735 ]; 1736 1737 return string.format("%s (%d, %d, %d) : (%s, %s, %s)", typeNames[cast(uint)type], index, index2, index3, isTempReg, isTempReg2, isTempReg3); 1726 1738 } 1727 1739 } … … 1932 1944 sd.switchPC = codeR(line, Op.Switch, 0, srcReg, 0); 1933 1945 sd.prev = mSwitch; 1934 1935 1946 mSwitch = sd; 1936 1947 } … … 1939 1950 { 1940 1951 SwitchDesc* desc = mSwitch; 1941 assert(desc , "endSwitch - no switch to end");1952 assert(desc !is null, "endSwitch - no switch to end"); 1942 1953 mSwitch = mSwitch.prev; 1943 1954 … … 1946 1957 } 1947 1958 1948 public void addNullCase(Location location) 1949 { 1950 assert(mSwitch !is null, "adding case outside of a switch"); 1951 int* oldOffset = (MDValue.nullValue in mSwitch.offsets); 1952 1953 if(oldOffset !is null) 1954 throw new MDCompileException(location, "Duplicate case value 'null'"); 1955 1956 mSwitch.offsets[MDValue.nullValue] = mCode.length - mSwitch.switchPC - 1; 1957 } 1958 1959 public void addCase(T)(Location location, T v) 1959 public int* addCase(Location location, Expression v) 1960 1960 { 1961 1961 if(mSwitch is null) 1962 1962 throw new MDCompileException(location, "Case statements may not exist outside of a switch"); 1963 1963 1964 MDValue val = v; 1964 MDValue val; 1965 1966 if(v.isNull()) 1967 val.setNull(); 1968 else if(v.isBool()) 1969 val = v.asBool(); 1970 else if(v.isInt()) 1971 val = v.asInt(); 1972 else if(v.isFloat()) 1973 val = v.asFloat(); 1974 else if(v.isChar()) 1975 val = v.asChar(); 1976 else if(v.isString()) 1977 val = v.asString(); 1978 else 1979 assert(false, "addCase invalid type: " ~ v.toString()); 1965 1980 1966 1981 int* oldOffset = (val in mSwitch.offsets); 1967 1982 1968 1983 if(oldOffset !is null) 1969 throw new MDCompileException(location, "Duplicate case value '%s'", v); 1970 1971 mSwitch.offsets[val] = mCode.length - mSwitch.switchPC - 1; 1984 throw new MDCompileException(location, "Duplicate case value '%s'", val); 1985 1986 mSwitch.offsets[val] = 0; 1987 return (val in mSwitch.offsets); 1972 1988 } 1973 1989 … … 2437 2453 codeI(line, Op.Closure, dest, src.index); 2438 2454 2439 foreach(inout UpvalDesc ud; mInnerFuncs[src.index].mUpvals) 2440 { 2441 if(ud.isUpvalue) 2442 codeR(line, Op.Move, 1, ud.index, 0); 2443 else 2444 codeR(line, Op.Move, 0, ud.index, 0); 2445 } 2455 foreach(ref UpvalDesc ud; mInnerFuncs[src.index].mUpvals) 2456 codeR(line, Op.Move, ud.isUpvalue ? 1 : 0, ud.index, 0); 2446 2457 2447 2458 break; … … 2510 2521 } 2511 2522 2512 public void popReflexOp(uint line, Op type, uint rd, uint rs )2523 public void popReflexOp(uint line, Op type, uint rd, uint rs, uint rt = 0) 2513 2524 { 2514 2525 Exp* dest = pushExp(); 2515 2526 dest.type = ExpType.NeedsDest; 2516 dest.index = codeR(line, type, rd, rs, 0);2527 dest.index = codeR(line, type, rd, rs, rt); 2517 2528 2518 2529 popAssign(line); … … 2683 2694 codeR(line, Op.Index, temp.index, e.index, e.index2); 2684 2695 break; 2685 2696 2686 2697 case ExpType.Sliced: 2687 2698 if(cleanup) … … 2709 2720 codeI(line, Op.Closure, temp.index, e.index); 2710 2721 2711 foreach(inout UpvalDesc ud; mInnerFuncs[e.index].mUpvals) 2712 { 2713 if(ud.isUpvalue) 2714 codeR(line, Op.Move, 1, ud.index, 0); 2715 else 2716 codeR(line, Op.Move, 0, ud.index, 0); 2717 } 2722 foreach(ref UpvalDesc ud; mInnerFuncs[e.index].mUpvals) 2723 codeR(line, Op.Move, ud.isUpvalue ? 1 : 0, ud.index, 0); 2718 2724 2719 2725 temp.isTempReg = true; … … 2751 2757 { 2752 2758 mCode[src.pc].imm = mCode.length - src.pc - 1; 2759 } 2760 2761 public void patchSwitchJumpToHere(int* offset) 2762 { 2763 assert(mSwitch !is null); 2764 *offset = mCode.length - mSwitch.switchPC - 1; 2753 2765 } 2754 2766 … … 3068 3080 } 3069 3081 3070 foreach(uint i, inoutSwitchDesc* t; mSwitchTables)3082 foreach(uint i, ref SwitchDesc* t; mSwitchTables) 3071 3083 { 3072 3084 writefln(string.repeat("\t", tab + 1), "Switch Table ", i); … … 3202 3214 } 3203 3215 3204 public static void parseBody(Location location, inoutToken* t, out FuncDef[] oMethods, out Field[] oFields, out Location oEndLocation)3216 public static void parseBody(Location location, ref Token* t, out FuncDef[] oMethods, out Field[] oFields, out Location oEndLocation) 3205 3217 { 3206 3218 t = t.expect(Token.Type.LBrace); … … 3292 3304 } 3293 3305 3294 public static Expression parseBaseClass( inoutToken* t)3306 public static Expression parseBaseClass(ref Token* t) 3295 3307 { 3296 3308 Expression baseClass; … … 3355 3367 mBaseClass = mBaseClass.fold(); 3356 3368 3357 foreach( inoutfield; mFields)3369 foreach(ref field; mFields) 3358 3370 field.initializer = field.initializer.fold(); 3359 3371 3360 foreach( inoutmethod; mMethods)3372 foreach(ref method; mMethods) 3361 3373 method = method.fold(); 3362 3374 … … 3396 3408 } 3397 3409 3398 public static FuncDef parseSimple( inoutToken* t)3410 public static FuncDef parseSimple(ref Token* t) 3399 3411 { 3400 3412 Location location = t.location; … … 3412 3424 } 3413 3425 3414 public static FuncDef parseLiteral( inoutToken* t)3426 public static FuncDef parseLiteral(ref Token* t) 3415 3427 { 3416 3428 Location location = t.location; … … 3438 3450 } 3439 3451 3440 public static Param[] parseParams( inoutToken* t, out bool isVararg)3452 public static Param[] parseParams(ref Token* t, out bool isVararg) 3441 3453 { 3442 3454 Param[] ret = new Param[1]; … … 3512 3524 public FuncDef fold() 3513 3525 { 3514 foreach( inoutp; mParams)3526 foreach(ref p; mParams) 3515 3527 if(p.defValue !is null) 3516 3528 p.defValue = p.defValue.fold(); … … 3526 3538 protected Location mEndLocation; 3527 3539 protected ModuleDeclaration mModDecl; 3528 protected dchar[][] []mImports;3540 protected dchar[][] mImports; 3529 3541 protected Statement[] mStatements; 3530 3542 3531 public this(Location location, Location endLocation, ModuleDeclaration modDecl, dchar[][] []imports, Statement[] statements)3543 public this(Location location, Location endLocation, ModuleDeclaration modDecl, dchar[][] imports, Statement[] statements) 3532 3544 { 3533 3545 mLocation = location; … … 3538 3550 } 3539 3551 3540 public static Module parse( inoutToken* t)3552 public static Module parse(ref Token* t) 3541 3553 { 3542 3554 Location location = t.location; … … 3545 3557 List!(Statement) statements; 3546 3558 3547 bool[dchar[] []] imports;3559 bool[dchar[]] imports; 3548 3560 3549 3561 void addImport(ImportStatement imp) 3550 3562 { 3551 imports[Identifier.to StringArray(imp.mNames)] = true;3563 imports[Identifier.toLongString(imp.mNames)] = true; 3552 3564 statements.add(imp); 3553 3565 } … … 3570 3582 MDModuleDef def = new MDModuleDef(); 3571 3583 3572 def.mName.length = mModDecl.mNames.length; 3573 3574 foreach(i, ident; mModDecl.mNames) 3575 def.mName[i] = ident.mName; 3576 3584 def.mName = Identifier.toLongString(mModDecl.mNames); 3577 3585 def.mImports = mImports; 3578 3586 … … 3582 3590 try 3583 3591 { 3584 foreach( inouts; mStatements)3592 foreach(ref s; mStatements) 3585 3593 { 3586 3594 s = s.fold(); … … 3608 3616 3609 3617 foreach(name; mImports) 3610 writefln("import %s", djoin(name, '.'));3618 writefln("import %s", name); 3611 3619 } 3612 3620 } … … 3621 3629 } 3622 3630 3623 public static ModuleDeclaration parse( inoutToken* t)3631 public static ModuleDeclaration parse(ref Token* t) 3624 3632 { 3625 3633 t = t.expect(Token.Type.Module); … … 3651 3659 } 3652 3660 3653 public static Statement parse( inoutToken* t)3661 public static Statement parse(ref Token* t) 3654 3662 { 3655 3663 Location location = t.location; … … 3693 3701 return SwitchStatement.parse(t); 3694 3702 3695 case Token.Type.Case:3696 return CaseStatement.parse(t);3697 3698 case Token.Type.Default:3699 return DefaultStatement.parse(t);3703 //case Token.Type.Case: 3704 // return CaseStatement.parse(t); 3705 3706 //case Token.Type.Default: 3707 // return DefaultStatement.parse(t); 3700 3708 3701 3709 case Token.Type.Continue: … … 3746 3754 } 3747 3755 3748 public static ImportStatement parse( inoutToken* t)3756 public static ImportStatement parse(ref Token* t) 3749 3757 { 3750 3758 Location location = t.location; … … 3861 3869 } 3862 3870 3863 public static ExpressionStatement parse( inoutToken* t)3871 public static ExpressionStatement parse(ref Token* t) 3864 3872 { 3865 3873 Location location = t.location; … … 3900 3908 } 3901 3909 3902 public static DeclarationStatement parse( inoutToken* t)3910 public static DeclarationStatement parse(ref Token* t) 3903 3911 { 3904 3912 Location location = t.location; … … 3938 3946 } 3939 3947 3940 public static Declaration parse( inoutToken* t)3948 public static Declaration parse(ref Token* t) 3941 3949 { 3942 3950 Location location = t.location; … … 3989 3997 } 3990 3998 3991 public static ClassDecl parse( inoutToken* t)3999 public static ClassDecl parse(ref Token* t) 3992 4000 { 3993 4001 Location location = t.location; … … 4058 4066 } 4059 4067 4060 public static VarDecl parse( inoutToken* t)4068 public static VarDecl parse(ref Token* t) 4061 4069 { 4062 4070 Location location = t.location; … … 4204 4212 } 4205 4213 4206 public static FuncDecl parse( inoutToken* t, bool simple = false)4214 public static FuncDecl parse(ref Token* t, bool simple = false) 4207 4215 { 4208 4216 Location location = t.location; … … 4266 4274 } 4267 4275 4268 public static Identifier parse( inoutToken* t)4276 public static Identifier parse(ref Token* t) 4269 4277 { 4270 4278 t.expect(Token.Type.Ident); … … 4312 4320 } 4313 4321 4314 public static CompoundStatement parse( inoutToken* t)4322 public static CompoundStatement parse(ref Token* t) 4315 4323 { 4316 4324 Location location = t.location; … … 4338 4346 public override CompoundStatement fold() 4339 4347 { 4340 foreach( inoutstatement; mStatements)4348 foreach(ref statement; mStatements) 4341 4349 statement = statement.fold(); 4342 4350 … … 4360 4368 } 4361 4369 4362 public static IfStatement parse( inoutToken* t)4370 public static IfStatement parse(ref Token* t) 4363 4371 { 4364 4372 Location location = t.location; … … 4460 4468 } 4461 4469 4462 public static WhileStatement parse( inoutToken* t)4470 public static WhileStatement parse(ref Token* t) 4463 4471 { 4464 4472 Location location = t.location; … … 4543 4551 } 4544 4552 4545 public static DoWhileStatement parse( inoutToken* t)4553 public static DoWhileStatement parse(ref Token* t) 4546 4554 { 4547 4555 Location location = t.location; … … 4645 4653 } 4646 4654 4647 public static Statement parse( inoutToken* t)4655 public static Statement parse(ref Token* t) 4648 4656 { 4649 4657 Location location = t.location; … … 4675 4683 4676 4684 Identifier index = Identifier.parse(t); 4677 4685 4678 4686 t = t.expect(Token.Type.Colon); 4679 4687 4680 4688 Expression lo = Expression.parse(t); 4681 4689 t = t.expect(Token.Type.DotDot); … … 4802 4810 public override Statement fold() 4803 4811 { 4804 foreach( inoutinit; mInit)4812 foreach(ref init; mInit) 4805 4813 { 4806 4814 if(init.isDecl) … … 4813 4821 mCondition = mCondition.fold(); 4814 4822 4815 foreach( inoutinc; mIncrement)4823 foreach(ref inc; mIncrement) 4816 4824 inc = inc.fold(); 4817 4825 … … 4927 4935 4928 4936 if(mLo.isConstant) 4929 { 4930 IntExp val = cast(IntExp)mLo; 4931 4932 if(val is null) 4937 if(!mLo.isInt) 4933 4938 throw new MDCompileException(mLo.mLocation, "Low value of a numeric for loop must be an integer"); 4934 }4935 4939 4936 4940 if(mHi.isConstant) 4937 { 4938 IntExp val = cast(IntExp)mHi; 4939 4940 if(val is null) 4941 if(!mHi.isInt) 4941 4942 throw new MDCompileException(mHi.mLocation, "High value of a numeric for loop must be an integer"); 4942 }4943 4943 4944 4944 if(mStep.isConstant) 4945 4945 { 4946 IntExp val = cast(IntExp)mStep; 4947 4948 if(val is null) 4946 if(!mStep.isInt) 4949 4947 throw new MDCompileException(mStep.mLocation, "Step value of a numeric for loop must be an integer"); 4950 4951 if( val.mValue== 0)4948 4949 if(mStep.asInt() == 0) 4952 4950 throw new MDCompileException(mStep.mLocation, "Step value of a numeric for loop may not be 0"); 4953 4951 } … … 4978 4976 } 4979 4977 4980 public static ForeachStatement parse( inoutToken* t)4978 public static ForeachStatement parse(ref Token* t) 4981 4979 { 4982 4980 Location location = t.location; … … 5126 5124 public override Statement fold() 5127 5125 { 5128 foreach( inoutc; mContainer)5126 foreach(ref c; mContainer) 5129 5127 c = c.fold(); 5130 5128 … … 5149 5147 } 5150 5148 5151 public static SwitchStatement parse( inoutToken* t)5149 public static SwitchStatement parse(ref Token* t) 5152 5150 { 5153 5151 Location location = t.location; … … 5160 5158 t = t.expect(Token.Type.RParen); 5161 5159 t = t.expect(Token.Type.LBrace); 5162 5160 5163 5161 List!(CaseStatement) cases; 5164 5162 … … 5183 5181 public override void codeGen(FuncState s) 5184 5182 { 5183 struct Case 5184 { 5185 Expression expr; 5186 CaseStatement stmt; 5187 } 5188 5189 List!(Case) constCases; 5190 List!(Case) dynCases; 5191 5192 foreach(caseStmt; mCases) 5193 { 5194 foreach(cond; caseStmt.mConditions) 5195 { 5196 if(cond.isConstant) 5197 constCases.add(Case(cond, caseStmt)); 5198 else 5199 dynCases.add(Case(cond, caseStmt)); 5200 } 5201 } 5202 5185 5203 s.pushScope(); 5186 5204 s.setBreakable(); … … 5189 5207 Exp src; 5190 5208 s.popSource(mLocation.line, src); 5191 5209 5210 foreach(c; dynCases) 5211 { 5212 c.expr.codeGen(s); 5213 Exp cond; 5214 s.popSource(mLocation.line, cond); 5215 5216 s.codeR(mLocation.line, Op.SwitchCmp, 0, src.index, cond.index); 5217 c.stmt.addDynJump(s.makeJump(mLocation.line, Op.Je, true)); 5218 s.freeExpTempRegs(&cond); 5219 } 5220 5192 5221 s.beginSwitch(mLocation.line, src.index); 5193 5194 5222 s.freeExpTempRegs(&src); 5195 5196 foreach(CaseStatement c; mCases) 5223 5224 foreach(c; constCases) 5225 c.stmt.addConstJump(s.addCase(c.expr.mLocation, c.expr)); 5226 5227 foreach(c; mCases) 5197 5228 c.codeGen(s); 5198 5229 … … 5210 5241 mCondition = mCondition.fold(); 5211 5242 5212 foreach( inoutc; mCases)5243 foreach(ref c; mCases) 5213 5244 c = c.fold(); 5214 5245 … … 5222 5253 class CaseStatement : Statement 5223 5254 { 5224 protected Expression mCondition;5255 protected Expression[] mConditions; 5225 5256 protected Statement mBody; 5226 5227 public this(Location location, Location endLocation, Expression condition, Statement caseBody) 5257 protected List!(InstRef*) mDynJumps; 5258 protected List!(int*) mConstJumps; 5259 5260 public this(Location location, Location endLocation, Expression[] conditions, Statement caseBody) 5228 5261 { 5229 5262 super(location, endLocation); 5230 mCondition = condition;5263 mConditions = conditions; 5231 5264 mBody = caseBody; 5232 5265 } 5233 5266 5234 public static CaseStatement parse( inoutToken* t)5267 public static CaseStatement parse(ref Token* t) 5235 5268 { 5236 5269 Location location = t.location; … … 5238 5271 t = t.expect(Token.Type.Case); 5239 5272 5240 List!(Expression) c ases;5241 c ases.add(Expression.parse(t));5273 List!(Expression) conditions; 5274 conditions.add(Expression.parse(t)); 5242 5275 5243 5276 while(t.type == Token.Type.Comma) 5244 5277 { 5245 5278 t = t.nextToken; 5246 c ases.add(Expression.parse(t));5247 } 5248 5279 conditions.add(Expression.parse(t)); 5280 } 5281 5249 5282 t = t.expect(Token.Type.Colon); 5250 5283 … … 5256 5289 Location endLocation = t.location; 5257 5290 5258 Statement ret = new CompoundStatement(location, endLocation, statements.toArray()); 5259 ret = new ScopeStatement(location, endLocation, ret); 5260 5261 for(int i = cases.length - 1; i >= 0; i--) 5262 ret = new CaseStatement(location, endLocation, cases[i], ret); 5263 5264 return cast(CaseStatement)ret; 5291 Statement caseBody = new CompoundStatement(location, endLocation, statements.toArray()); 5292 caseBody = new ScopeStatement(location, endLocation, caseBody); 5293 5294 return new CaseStatement(location, endLocation, conditions.toArray(), caseBody); 5295 } 5296 5297 public void addDynJump(InstRef* i) 5298 { 5299 mDynJumps.add(i); 5300 } 5301 5302 public void addConstJump(int* i) 5303 { 5304 mConstJumps.add(i); 5265 5305 } 5266 5306 5267 5307 public override void codeGen(FuncState s) 5268 5308 { 5269 if(!mCondition.isConstant) 5270 throw new MDCompileException(mLocation, "Case value is not constant"); 5271 5272 IntExp intExp = cast(IntExp)mCondition; 5309 foreach(ref j; mDynJumps) 5310 { 5311 s.patchJumpToHere(j); 5312 delete j; 5313 } 5273 5314 5274 if(intExp) 5275 s.addCase(mLocation, intExp.mValue); 5276 else 5277 { 5278 StringExp stringExp = cast(StringExp)mCondition; 5279 5280 if(stringExp) 5281 s.addCase(mLocation, stringExp.mValue); 5282 else 5283 { 5284 CharExp charExp = cast(CharExp)mCondition; 5285 5286 if(charExp) 5287 s.addCase(mLocation, charExp.mValue); 5288 else 5289 { 5290 FloatExp floatExp = cast(FloatExp)mCondition; 5291 5292 if(floatExp) 5293 s.addCase(mLocation, floatExp.mValue); 5294 else 5295 { 5296 BoolExp boolExp = cast(BoolExp)mCondition; 5297 5298 if(boolExp) 5299 s.addCase(mLocation, boolExp.mValue); 5300 else 5301 { 5302 NullExp nullExp = cast(NullExp)mCondition; 5303 5304 if(nullExp) 5305 s.addNullCase(mLocation); 5306 else 5307 throw new MDCompileException(mLocation, "Invalid case value"); 5308 } 5309 } 5310 } 5311 } 5315 foreach(ref j; mConstJumps) 5316 { 5317 s.patchSwitchJumpToHere(j); 5318 j = null; 5312 5319 } 5313 5320 … … 5317 5324 public override CaseStatement fold() 5318 5325 { 5319 mCondition = mCondition.fold(); 5326 foreach(ref cond; mConditions) 5327 cond = cond.fold(); 5328 5320 5329 mBody = mBody.fold(); 5321 5330 return this; … … 5333 5342 } 5334 5343 5335 public static DefaultStatement parse( inoutToken* t)5344 public static DefaultStatement parse(ref Token* t) 5336 5345 { 5337 5346 Location location = t.location; … … 5372 5381 } 5373 5382 5374 public static ContinueStatement parse( inoutToken* t)5383 public static ContinueStatement parse(ref Token* t) 5375 5384 { 5376 5385 Location location = t.location; … … 5395 5404 } 5396 5405 5397 public static BreakStatement parse( inoutToken* t)5406 public static BreakStatement parse(ref Token* t) 5398 5407 { 5399 5408 Location location = t.location; … … 5427 5436 } 5428 5437 5429 public static ReturnStatement parse( inoutToken* t)5438 public static ReturnStatement parse(ref Token* t) 5430 5439 { 5431 5440 Location location = t.location; … … 5487 5496 public override Statement fold() 5488 5497 { 5489 foreach( inoutexp; mExprs)5498 foreach(ref exp; mExprs) 5490 5499 exp = exp.fold(); 5491 5500 … … 5511 5520 } 5512 5521 5513 public static TryCatchStatement parse( inoutToken* t)5522 public static TryCatchStatement parse(ref Token* t) 5514 5523 { 5515 5524 Location location = t.location; … … 5661 5670 } 5662 5671 5663 public static ThrowStatement parse( inoutToken* t)5672 public static ThrowStatement parse(ref Token* t) 5664 5673 { 5665 5674 Location location = t.location; … … 5706 5715 } 5707 5716 5708 public static Expression parse( inoutToken* t)5709 { 5710 return BinaryExp.parse(t);5711 } 5712 5713 public static Expression parseStatement( inoutToken* t)5717 public static Expression parse(ref Token* t) 5718 { 5719 return CondExp.parse(t); 5720 } 5721 5722 public static Expression parseStatement(ref Token* t) 5714 5723 { 5715 5724 Location location = t.location; … … 5753 5762 } 5754 5763 5755 public static Expression[] parseArguments( inoutToken* t)5764 public static Expression[] parseArguments(ref Token* t) 5756 5765 { 5757 5766 Expression[] args = new Expression[5]; … … 5852 5861 } 5853 5862 5863 public bool isNull() 5864 { 5865 return false; 5866 } 5867 5868 public bool isBool() 5869 { 5870 return false; 5871 } 5872
