Lartu / ldpl

COBOL-like programming language that compiles to C++. With serious dinosaurs with neckties and briefcases 🦕💼
https://www.ldpl-lang.org/
Apache License 2.0
158 stars 24 forks source link

FOR statement #115

Closed dgarroDC closed 5 years ago

dgarroDC commented 5 years ago

This adds a FOR statement, following this syntax:

FOR i FROM x TO y STEP z DO
    #FOR body
NEXT

where i is a number variable and x, y and z are number variables or literals.

First, it assigns x to i and starts an iteration. At the start of an iteration, the condition i <= y is evaluated if x < y and i >= y otherwise. If the condition passes, the FOR body is executed, if it fails the loop ends. After the body is executed, i is incremented by z and a new iteration is started (checking the condition and so on).

The BREAK and CONTINUE statements can be using inside FOR loops.

I used NEXT to mark the end of a FOR loop, but it could be REPEAT like in the WHILE loop without any trouble, if you prefer it.

Examples:

DATA:
i is number
PROCEDURE:
for i from 0 to 10 step 2 do
    if i is greater than 7 then
        break
    end if
    display i " "
next

outputs 0 2 4 6.

DATA:
i is number
PROCEDURE:
for i from 5 to 0 step -1 do
    if i is equal to 3 then
        continue
    end if
    display i " "
next

outputs 5 4 2 1 0.

dgarroDC commented 5 years ago

I think the STEP could be optional, and increment the variable by 1 or -1 depending on x < y or not, what do you think?

arielf212 commented 5 years ago

maybe from X to Y step Z store in <variable> do would be more readable? but then we lose the for part, which is a problem. the step part shouldn't be optional imo, as that goes against the "rigidness" of the language, anyways, I think that this is an awesome addition!

Lartu commented 5 years ago

This adds a FOR statement, following this syntax [...] where i is a number variable and x, y and z are number variables or literals.

I love it! Thank you so very much for this wonderful contribution!

At the start of an iteration, the condition i <= y is evaluated if x < y and i >= y otherwise.

This is what I like the most! Wonderful!

I used NEXT to mark the end of a FOR loop, but it could be REPEAT like in the WHILE loop without any trouble, if you prefer it.

I like NEXT, I would leave it like that. Maybe, just maybe, I'd allow REPEAT as well, but next is alright with me. I mean, when someone says FOR you immediately think of FOR-NEXT, so I think that's half the battle won right there.

I think the STEP could be optional, and increment the variable by 1 or -1 depending on x < y or not, what do you think?

I agree with @fireasembler, it shouldn't be optional, it gives more clarity to the matter. Just what they said.

Then firesaembler said:

maybe from X to Y step Z store in do would be more readable? but then we lose the for part, which is a problem.

I do think losing the for part is a problem. When you need a for loop in a language you search 'how to write a for loop in X language', renaming it would make it harder to find (and, for people with any programming background) to understand.

I love this, thanks a lot to @dgarroDC and thanks to you fireasembler for your valuable input! I'm merging this right now!

Lartu commented 5 years ago

Please @dgarroDC document this new feature in the docs and add for loop tests to the ldpl test battery. If you can't do it, please tell me and I'll try to do it myself. Thank you!

arielf212 commented 5 years ago

@Lartu I suggested changing it to from X to Y step Z store in <var> do because imo the for loop syntax is best used as the syntax of an iteration loop (think python for X in Y)

Lartu commented 5 years ago

@fireasembler I understand, and I do agree with you. I like the from syntax more, but it could be confusing for people not used to the language. In python they still keep the for keyword, it's somewhat universal, I wouldn't dare replacing it, even though your syntax is clearer.

dgarroDC commented 5 years ago

While I was writing the tests, I found a bug when x was equal to y, it looped forever when the step was positive instead of doing just one iteration. To fix it, the condition is based on the step, (i <= y if z >= 0). This actually makes more sense (and it is what VB does), because yo mark the intended direction of the for with the step, not the range. In fact, it's useful when you iterate lists, because if it's empty you have a FOR from 0 to -1 with step 1 and you don't want to iterate anything in this case, instead of looping forever.

Lartu commented 5 years ago

Haha, that's true. Wonderful, thank you very much for this and for uploading tests to the test battery!