nsensfel / tonkadur

Narrative scripting/programming tool. Write stories in your favorite editor using a feature-rich language, compile them into a very small and simple language to easily integrate them into your game.
https://tonkadur.of.tacticians.online
Apache License 2.0
3 stars 0 forks source link

Add a sanity check on sequence termination #4

Closed nsensfel closed 4 years ago

nsensfel commented 4 years ago

Currently, it is possible to write something like:

(define_sequence demo
   (switch (var my_var)
        (0
            (
                (set my_var (+ 3 (var my_var)))
                (sequence demo)
            )
        )
        (1
            (
                (set my_var (- 2 (var my_var)))
                That's a miss
            )
        )
        (
            (set my_var (+ 2 (var my_var)))
            (sequence demo)
        )
   )
)

If (var my_var) is equal to one, the sequence ends without either a jump to another sequence, or an (end) instruction. The behavior in this case is undefined (Wyrd will keep on executing code from whatever is next in the code base without even realizing a sequence ended).

The compiler should raise an error whenever there is a way to exit the sequence without either (sequence ...) or (end).

One way to go about this is to check on the last instruction, and see if its branches all end in either of these instructions.

Some cases will require the user to add dead code to fulfill this constraint, however:


(define_sequence demo
    (while (true)
        (if_else (var has_won)
            (sequence victory_lap)
            (
                Head or tail?
                (player_choice
                    (( Head ) ( ))
                    (( Tail ) ( ))
                )
                (set has_won (= (rand 0 1) 0))
            )
        )
    )
)

This is also something programming languages require when defining functions that return a value, and can thus be considered an acceptable compromise.

nsensfel commented 4 years ago

https://github.com/nsensfel/tonkadur/issues/2 is changing this a bit: sequences can now be returned from.

nsensfel commented 4 years ago

The new implementation of sequences removes the issue of going past the sequence's definition. Thus, the lack of explicit sequence termination is no longer an issue.