aboutsummaryrefslogtreecommitdiff
path: root/notes/goto_improved.txt
blob: d1def4bbad5661c7dfe3952e98ea9b014498aedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

goto but improved
=================

* Use `section l` instead of `l:`
* Disallow implicit fallthrough.
  Could have an explicit `fallthrough` keyword
  (also for `case` blocks)
* Allow only goto to within the same or a containing block.
* Sections have access to the variables of the "main" part of the
  block that it belongs to, but variables that aren't guaranteed to be
  initialized at all `goto` or `fallthrough` statements.
* Allow goto within the nearest `switch` block.
  Perhaps with the syntax `goto case c`
* Should `section`s be allowed in any block, or only
  at the outer `code` block?
    - won't work with `else`
    - won't work with `loopend`/`loopempty`
    - won't work with `switch (and somewhat redundant anyway)
    - also, it could be confusing.
      better break up the code in multiple functions?

Syntax test:

    func example
        int x
    code
        if x >= 100
            goto section fail
        end
        # or, maybe:
        require x <= 99 else goto section fail

        switch x
        case 1
            do_stuff "A"
            fallthrough
        case 2
            do_stuff "B"
            goto case 1
        case 3
            do_stuff "C"
            goto default
        case 4
            do_stuff "D"
            fallthrough
        default
            do_stuff "other"
        end
        return true
        # Implicit fallthrough not allowed!
    section fail
        error "Oops"
        return false
    end

Syntax test with assertion failure section:

    func example
        int x
    code
        assert x <= 99

        ...
        return true
    section assert_fail
        error "Oops"
        return false
    end