mirror of
https://github.com/irmen/prog8.git
synced 2025-02-18 05:30:34 +00:00
docs about 'when' statement
This commit is contained in:
parent
14cabde5cf
commit
a85743f241
@ -416,15 +416,26 @@ So ``if_cc goto target`` will directly translate into the single CPU instruction
|
|||||||
Maybe in the future this will be a separate nested scope, but for now, that is
|
Maybe in the future this will be a separate nested scope, but for now, that is
|
||||||
only possible when defining a subroutine.
|
only possible when defining a subroutine.
|
||||||
|
|
||||||
when statement (jumptable)
|
when statement ('jump table')
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
.. attention::
|
Instead of writing a bunch of sequential if-elseif statements, it is more readable to
|
||||||
TODO: docs for this this must still be written.
|
use a ``when`` statement. (It will also result in greatly improved assembly code generation)
|
||||||
TODO: the code generator for this is not yet working.
|
Use a ``when`` statement if you have a set of fixed choices that each should result in a certain
|
||||||
|
action. It is possible to combine several choices to result in the same action::
|
||||||
|
|
||||||
Use a ``when`` statement if you have a set of choices that each should result in a certain
|
when value {
|
||||||
action. It's more readable (and results in faster code) than using a lot of if / else statements.
|
4 -> c64scr.print("four")
|
||||||
|
5 -> c64scr.print("five")
|
||||||
|
10,20,30 -> {
|
||||||
|
c64scr.print("ten or twenty or thirty")
|
||||||
|
}
|
||||||
|
else -> c64scr.print("don't know")
|
||||||
|
}
|
||||||
|
|
||||||
|
The when-*value* can be any expression but the choice values have to evaluate to
|
||||||
|
compile-time constant integers (bytes or words). They also have to be the same
|
||||||
|
datatype as the when-value, otherwise no efficient comparison can be done.
|
||||||
|
|
||||||
|
|
||||||
Assignments
|
Assignments
|
||||||
|
@ -610,28 +610,28 @@ The XX corresponds to one of the eigth branching instructions so the possibiliti
|
|||||||
``if_cs``, ``if_cc``, ``if_eq``, ``if_ne``, ``if_pl``, ``if_mi``, ``if_vs`` and ``if_vc``.
|
``if_cs``, ``if_cc``, ``if_eq``, ``if_ne``, ``if_pl``, ``if_mi``, ``if_vs`` and ``if_vc``.
|
||||||
It can also be one of the four aliases that are easier to read: ``if_z``, ``if_nz``, ``if_pos`` and ``if_neg``.
|
It can also be one of the four aliases that are easier to read: ``if_z``, ``if_nz``, ``if_pos`` and ``if_neg``.
|
||||||
|
|
||||||
when statement (jumptable)
|
when statement ('jump table')
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
.. attention::
|
The structure of a when statement is like this::
|
||||||
TODO: docs for this this must still be written.
|
|
||||||
TODO: the code generator for this is not yet working.
|
|
||||||
|
|
||||||
The condition value can only be an integer datatype.
|
when <expression> {
|
||||||
The choice values must be constant integer values.
|
<value(s)> -> <statement(s)>
|
||||||
|
<value(s)> -> <statement(s)>
|
||||||
|
...
|
||||||
|
[ else -> <statement(s)> ]
|
||||||
|
}
|
||||||
|
|
||||||
code example::
|
The when-*value* can be any expression but the choice values have to evaluate to
|
||||||
|
compile-time constant integers (bytes or words).
|
||||||
|
The else part is optional.
|
||||||
|
Choices can result in a single statement or a block of multiple statements in which
|
||||||
|
case you have to use { } to enclose them::
|
||||||
|
|
||||||
when 4+A+Y {
|
when value {
|
||||||
10 -> {
|
4 -> c64scr.print("four")
|
||||||
c64scr.print("ten")
|
5 -> c64scr.print("five")
|
||||||
}
|
10,20,30 -> {
|
||||||
5 -> c64scr.print("five")
|
c64scr.print("ten or twenty or thirty")
|
||||||
30 -> c64scr.print("thirty")
|
|
||||||
99 -> c64scr.print("nn")
|
|
||||||
55 -> {
|
|
||||||
; will be optimized away
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
c64scr.print("!??!\n")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else -> c64scr.print("don't know")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user