Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scriptcomp: Allow statements in for loops #50

Open
mtijanic opened this issue Jul 7, 2023 · 2 comments
Open

scriptcomp: Allow statements in for loops #50

mtijanic opened this issue Jul 7, 2023 · 2 comments

Comments

@mtijanic
Copy link
Collaborator

mtijanic commented Jul 7, 2023

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++) {...}
}
@ELadner
Copy link

ELadner commented Jul 8, 2023

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?

@mtijanic
Copy link
Collaborator Author

mtijanic commented Jul 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants