Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CLanguage/Parser/CParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1825,25 +1825,25 @@ internal Object yyparse (yyParser.yyInput yyLex)
case 292:
#line 1115 "CParser.jay"
{
yyVal = new ForStatement((Statement)yyVals[-3+yyTop], ((ExpressionStatement)yyVals[-2+yyTop]).Expression, ((Statement)yyVals[0+yyTop]).ToBlock ());
yyVal = new ForStatement((Statement)yyVals[-3+yyTop], (yyVals[-2+yyTop] as ExpressionStatement)?.Expression, ((Statement)yyVals[0+yyTop]).ToBlock ());
}
break;
case 293:
#line 1119 "CParser.jay"
{
yyVal = new ForStatement((Statement)yyVals[-4+yyTop], ((ExpressionStatement)yyVals[-3+yyTop]).Expression, (Expression)yyVals[-2+yyTop], ((Statement)yyVals[0+yyTop]).ToBlock ());
yyVal = new ForStatement((Statement)yyVals[-4+yyTop], (yyVals[-3+yyTop] as ExpressionStatement)?.Expression, (Expression)yyVals[-2+yyTop], ((Statement)yyVals[0+yyTop]).ToBlock ());
}
break;
case 294:
#line 1123 "CParser.jay"
{
yyVal = new ForStatement((Statement)yyVals[-3+yyTop], ((ExpressionStatement)yyVals[-2+yyTop]).Expression, ((Statement)yyVals[0+yyTop]).ToBlock ());
yyVal = new ForStatement((Statement)yyVals[-3+yyTop], (yyVals[-2+yyTop] as ExpressionStatement)?.Expression, ((Statement)yyVals[0+yyTop]).ToBlock ());
}
break;
case 295:
#line 1127 "CParser.jay"
{
yyVal = new ForStatement((Statement)yyVals[-4+yyTop], ((ExpressionStatement)yyVals[-3+yyTop]).Expression, (Expression)yyVals[-2+yyTop], ((Statement)yyVals[0+yyTop]).ToBlock ());
yyVal = new ForStatement((Statement)yyVals[-4+yyTop], (yyVals[-3+yyTop] as ExpressionStatement)?.Expression, (Expression)yyVals[-2+yyTop], ((Statement)yyVals[0+yyTop]).ToBlock ());
}
break;
case 297:
Expand Down
8 changes: 4 additions & 4 deletions CLanguage/Parser/CParser.jay
Original file line number Diff line number Diff line change
Expand Up @@ -1111,19 +1111,19 @@ iteration_statement
}
| FOR '(' expression_statement expression_statement ')' statement
{
$$ = new ForStatement((Statement)$3, ((ExpressionStatement)$4).Expression, ((Statement)$6).ToBlock ());
$$ = new ForStatement((Statement)$3, ($4 as ExpressionStatement)?.Expression, ((Statement)$6).ToBlock ());
}
| FOR '(' expression_statement expression_statement expression ')' statement
{
$$ = new ForStatement((Statement)$3, ((ExpressionStatement)$4).Expression, (Expression)$5, ((Statement)$7).ToBlock ());
$$ = new ForStatement((Statement)$3, ($4 as ExpressionStatement)?.Expression, (Expression)$5, ((Statement)$7).ToBlock ());
}
| FOR '(' declaration expression_statement ')' statement
{
$$ = new ForStatement((Statement)$3, ((ExpressionStatement)$4).Expression, ((Statement)$6).ToBlock ());
$$ = new ForStatement((Statement)$3, ($4 as ExpressionStatement)?.Expression, ((Statement)$6).ToBlock ());
}
| FOR '(' declaration expression_statement expression ')' statement
{
$$ = new ForStatement((Statement)$3, ((ExpressionStatement)$4).Expression, (Expression)$5, ((Statement)$7).ToBlock ());
$$ = new ForStatement((Statement)$3, ($4 as ExpressionStatement)?.Expression, (Expression)$5, ((Statement)$7).ToBlock ());
}
;

Expand Down
14 changes: 8 additions & 6 deletions CLanguage/Syntax/ForStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace CLanguage.Syntax
public class ForStatement : Statement
{
public Block InitBlock { get; private set; }
public Expression ContinueExpression { get; private set; }
public Expression? ContinueExpression { get; private set; }
public Expression? NextExpression { get; private set; }
public Block LoopBody { get; private set; }

public ForStatement (Statement initStatement, Expression continueExpr, Block body)
public ForStatement (Statement initStatement, Expression? continueExpr, Block body)
{
InitBlock = new Block (VariableScope.Local);
if (initStatement != null) {
Expand All @@ -25,7 +25,7 @@ public ForStatement (Statement initStatement, Expression continueExpr, Block bod
LoopBody = body;
}

public ForStatement (Statement initStatement, Expression continueExpr, Expression nextExpr, Block body)
public ForStatement (Statement initStatement, Expression? continueExpr, Expression nextExpr, Block body)
{
InitBlock = new Block (VariableScope.Local);
if (initStatement != null) {
Expand Down Expand Up @@ -63,9 +63,11 @@ protected override void DoEmit (EmitContext initialContext)
//
var conditionLabel = ec.DefineLabel ();
ec.EmitLabel (conditionLabel);
ContinueExpression.Emit (ec);
ec.EmitCastToBoolean (ContinueExpression.GetEvaluatedCType (ec));
ec.Emit (OpCode.BranchIfFalse, endLabel);
if (ContinueExpression != null) {
ContinueExpression.Emit (ec);
ec.EmitCastToBoolean (ContinueExpression.GetEvaluatedCType (ec));
ec.Emit (OpCode.BranchIfFalse, endLabel);
}

//
// Fall through to the loop body
Expand Down
52 changes: 52 additions & 0 deletions CLanguageTests/InterpreterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
Assert.IsTrue (hit[0]);
Assert.IsFalse (hit[1]);
Assert.IsFalse (hit[2]);
it.Step ();

Check warning on line 39 in CLanguageTests/InterpreterTests.cs

View workflow job for this annotation

GitHub Actions / build

'CInterpreter.Step()' is obsolete: 'Please use Run() which Steps for 1 machine second.'
Assert.IsTrue (hit[0]);
Assert.IsTrue (hit[1]);
Assert.IsFalse (hit[2]);
it.Step ();

Check warning on line 43 in CLanguageTests/InterpreterTests.cs

View workflow job for this annotation

GitHub Actions / build

'CInterpreter.Step()' is obsolete: 'Please use Run() which Steps for 1 machine second.'
Assert.IsTrue (hit[0]);
Assert.IsTrue (hit[1]);
Assert.IsTrue (hit[2]);
Expand Down Expand Up @@ -246,6 +246,58 @@
}");
}

[TestMethod]
public void ForLoopInfinite ()
{
Run (@"
void main () {
int i = 0;
for (;;) {
i++;
if (i >= 10) break;
}
assertAreEqual (10, i);
}");
}

[TestMethod]
public void ForLoopEmptyCondition ()
{
Run (@"
void main () {
int i;
for (i = 0; ; i++) {
if (i >= 5) break;
}
assertAreEqual (5, i);
}");
}

[TestMethod]
public void ForLoopEmptyIncrement ()
{
Run (@"
void main () {
int i;
for (i = 0; i < 10;) {
i++;
}
assertAreEqual (10, i);
}");
}

[TestMethod]
public void ForLoopEmptyInit ()
{
Run (@"
void main () {
int i = 0;
for (; i < 5; i++) {
}
assertAreEqual (5, i);
}");
}

[TestMethod]
public void WhileLoopWithBreak ()
{
Expand Down
Loading