c3d / xl

A minimalist, general-purpose programming language based on meta-programming and parse tree rewrites
GNU General Public License v3.0
270 stars 15 forks source link

Imperative flavour: continue & break in loops #44

Open dumblob opened 3 years ago

dumblob commented 3 years ago

Because XL is fundamentally kind of functional, it seems uneasy to simulate continue from imperative loops. Same goes for break (one could use return instead, but that'd work only without nesting I suppose which renders it completely unusable).

Any plans to provide break is builtin jmp_right_right_after_nearest_loop and continue is builtin jmp_to_the_nearest_loop_label?

c3d commented 3 years ago

On 11 Jun 2021, at 09:12, dumblob @.***> wrote:

Because XL is fundamentally kind of functional, it seems uneasy to simulate continue from imperative loops.

XL is not fundamentally functional, it is fundamentally concept-oriented. This means that being able to implement a functional dialect is as important as being able to implement other dialects, but not more important.

Older more imperative-looking variants of XL had "restart" for "continue" (but I did not like that name much), see https://github.com/c3d/xl/blob/cb1d9b3fd9d18f5131c85fe5d6fcec8ec3fde3c8/xl2/native/xl.semantics.declarations.xl#L487.

I know it may seem a bit unconventional, but the parse tree that you see in these source codes is the same as what you see in the "functional" examples. See the discussions of "syntactic sugar" in the doc.

function NonSourceType(tp : any_type) return any_type is
    result := tp
    loop
        stp : result as source_type
        exit if stp = nil
        result := stp.base

Could just as well be written with the shorter form

NonSourceType tp:any_type as any_type is ...

function here is syntactic sugar, you can add it (and you can even give it a meaning, namely "I do not expect side effects here").

Same goes for break (one could use return instead, but that'd work only without nesting I suppose which renders it completely unusable).

XL2 used "exit" for "break", with a conditional "exit if" variant, see https://github.com/c3d/xl/blob/cb1d9b3fd9d18f5131c85fe5d6fcec8ec3fde3c8/xl2/native/xl.semantics.types.xl#L263.

Any plans to provide break is builtin jmp_right_right_after_nearest_loop and continue is builtin jmp_to_the_nearest_loop_label?

Even plans to implement "goto" at some point. It's very hard to write a low-level code generator if the target language does not have "goto". All CPUs today have variants of goto (jump, branch, whatever).

Again, XL is concept-oriented. This means that I must be able to represent the concepts of functional programming with the same ease as the concepts of assembly language. Or Prolog. Or mathematics.

c3d commented 3 years ago

Interesting. It looks like email replies do not support markdown in GitHub. Weird.

dumblob commented 3 years ago

Just to clarify, with kind of functional I meant pattern matching and its "purity" making it difficult to express continue without resorting to "hacks" like builtin :wink: (though in a concept language one could argue that there can't be any hack by definition :wink:).

Btw. I didn't even know there is function defined somewhere in XL - with kind of functional I was just referring to the pattern matching with the given set of rewriting rules.

Anyway thanks for the goto information - that answers my question! Hopefully I'll find some time to actually do something with XL sooner or later.

c3d commented 3 years ago

There is much to be done. Nothing really works right now, I broke everything in my last big refactoring :-)