Skip to content

Named blocks #10

@liquidev

Description

@liquidev

Because the language lacks goto, breaking out of a nested loop is quite unreadable:

var done = false
for y in 1..10
   for x in 1..10
      if my_array[x + y * 10] == 0
         done = true
         break
   if done
      break

Therefore, I think that named blocks should be introduced:

block loops
   for y in 1..10
      for x in 1..10
         if my_array[x + y * 10] == 0
            break @loops

Reasoning behind the syntax:

  • Prefixed declarations are better than infixed, because they're easier to parse and don't require backtracking.
    • Thus, block <name> and break @<name> instead of <name>: and break <name>
  • block should be an expression, thus one should be able to return a value out of a block.
    • I'm still not sure about the syntax here. break @loops -1 looks maybe just a little weird? Especially when combined with some more complex expression like break @loops x + y * 2
      • Maybe introduce a with keyword between the label and the result expression: break @loops with x + y * 2
      • On the other hand, the @label already separates the label name from the expression, especially if it's highlighted differently from normal expressions in editors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureThis issue is related to implementing a major feature of the compiler.language featureAdding a new feature into the language.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions