@@ -1184,10 +1184,22 @@ private StatementBlock ParseCommandBlock(Scope scope, MethodDeclaration method)
1184
1184
1185
1185
ExpectToken ( "(" ) ;
1186
1186
1187
- ExpectToken ( "local" ) ;
1187
+ bool mustExist ;
1188
+
1189
+ var next = FetchToken ( ) ;
1190
+ if ( next . value == "local" )
1191
+ {
1192
+ mustExist = false ;
1193
+ }
1194
+ else
1195
+ {
1196
+ mustExist = true ;
1197
+ Rewind ( ) ;
1198
+ }
1188
1199
1189
1200
AssignStatement initCmd ;
1190
- forCommand . loopVar = ParseVariableDeclaration ( scope , out initCmd ) ;
1201
+ forCommand . loopVar = ParseVariableDeclaration ( scope , out initCmd , mustExist ) ;
1202
+
1191
1203
if ( initCmd == null )
1192
1204
{
1193
1205
throw new CompilerException ( "variable missing initialization statement in for loop" ) ;
@@ -1212,7 +1224,7 @@ private StatementBlock ParseCommandBlock(Scope scope, MethodDeclaration method)
1212
1224
throw new CompilerException ( $ "expected variable { varName } (temporary compiler limitation, no complex for statements supported!)") ;
1213
1225
}
1214
1226
1215
- var next = FetchToken ( ) ;
1227
+ next = FetchToken ( ) ;
1216
1228
1217
1229
if ( next . kind == TokenKind . Postfix )
1218
1230
{
@@ -1457,7 +1469,7 @@ private StatementBlock ParseCommandBlock(Scope scope, MethodDeclaration method)
1457
1469
}
1458
1470
}
1459
1471
1460
- private VarDeclaration ParseVariableDeclaration ( Scope scope , out AssignStatement assignment )
1472
+ private VarDeclaration ParseVariableDeclaration ( Scope scope , out AssignStatement assignment , bool mustExist = false )
1461
1473
{
1462
1474
var varName = ExpectIdentifier ( ) ;
1463
1475
@@ -1475,9 +1487,17 @@ private VarDeclaration ParseVariableDeclaration(Scope scope, out AssignStatement
1475
1487
1476
1488
Expression initExpr = ParseVariableInitialization ( scope , ref type ) ;
1477
1489
1478
- var varDecl = new VarDeclaration ( scope , varName , type , VarStorage . Local ) ;
1490
+ VarDeclaration varDecl ;
1479
1491
1480
- scope . AddVariable ( varDecl ) ;
1492
+ if ( mustExist )
1493
+ {
1494
+ varDecl = scope . FindVariable ( varName ) ;
1495
+ }
1496
+ else
1497
+ {
1498
+ varDecl = new VarDeclaration ( scope , varName , type , VarStorage . Local ) ;
1499
+ scope . AddVariable ( varDecl ) ;
1500
+ }
1481
1501
1482
1502
if ( initExpr != null )
1483
1503
{
0 commit comments