neverwinter.nim icon indicating copy to clipboard operation
neverwinter.nim copied to clipboard

scriptcomp: Allow statements in `for` loops

Open mtijanic opened this issue 1 year ago • 2 comments

Currently, the for loop syntax is

for (expression_opt; expression_opt; expression_opt) statement

, but it should be

for (statement; expression_opt; expression_opt) statement

Specifically, this should be possible for (int i = 0; i < N; i++) {...}

The scope of the first statement is the scope of the entire block. In other words, the above is equivalent to

{
   int i;
   for (i = 0; i < N; i++) {...}
}

mtijanic avatar Jul 07 '23 11:07 mtijanic

Consider the following:

for ( object oPC = GetFirstPC() ; GetIsObjectValid(oPC) ; oPC = GetNextPC() ) { ... }

The third entry would also have to be a statement to support that, correct?

ELadner avatar Jul 08 '23 19:07 ELadner

Consider the following:

for ( object oPC = GetFirstPC() ; GetIsObjectValid(oPC) ; oPC = GetNextPC() ) { ... }

The third entry would also have to be a statement to support that, correct?

No, in C-family languages, assignment is an operator. It has two operands (left and right, like + or any other operator) and it returns a value (the value that was assigned), and just has the side effect of changing the left operand. This is why you can do a = b = c;. Since the grouping for binary operators of same priority is right-to-left, that is equivalent to b = c; a = b;

Anything with an operator is an expression. Statements are a superset of that that also allow things like variable declarations.

mtijanic avatar Jul 08 '23 19:07 mtijanic