Files
MagicSetEditor2/doc/script/control_structures.txt
T
2020-05-09 18:41:30 +02:00

108 lines
3.6 KiB
Plaintext

Control structures
MSE Script has two types of control structures.
--Conditional statement (@if then else@)--
To switch between two options use:
> if condition then a else b
If the condition evaluates to @true@,
then the expression evaluates to @a@, otherwise it evaluates to @b@.
The else part is optional.
For example:
> if 1 + 1 == 2 then "yes sir!" else "something is wrong"
Will evaluate to @"yes sir!"@.
Note that if-then-else is an ''expression'', it can be used almost everywhere:
> 1 + (if card.color == "red" then 1 else 2)
> color := (if card.color == "red" then "r") +
> (if card.color == "green" then "g")
Multiple conditions can be checked by using @else if@:
> if card.color == "white" then "W"
> else if card.color == "red" then "R"
> else if card.color == "blue" then "U"
> else "something else"
The @then@ and @else@ parts can also contain assignments and other control structures.
> if card.color == "red" then
> filter := filter + "r"
To use multiple statements in the then or else branches you must use parentheses:
> # WRONG
> if 1 + 1 == 2 then
> x := y
> y := z
> # RIGHT
> if 1 + 1 == 2 then (
> x := y
> y := z
> )
--Case analysis (@case of@)--
To compare multiple alternatives, you can use a case expression
> case thing of alt1: result1, alt2: result2, alt3: result3, else: otherwise
The expression @thing@ is compared against all the alternatives, and if it is equal to one then the corresponding result is returned
For example:
> case 1+1 of
> 1: "one",
> 2: "two",
> 3: "three",
> else: "big"
Will evaluate to @"two"@.
Like if-then-else, case-of is an expression, and it can be used almost everywhere.
To use multiple statements in the casess you must use parentheses.
--Loop statement (@for each@)--
To iterate over all elements in a [[type:list]] the @for each@ construct can be used
> for each variable in list do expression
If list is a list of items, for example set.cards, the expression is evaluated for each item in that list.
The variable becomes set to that each item in succession.
The results of the expression are combined using the @+@ [[script:operators|operator]]:
> for each x in ["a","b","c"] do x == "abc"
> for each x in ["a","b","c"] do [x+x] == ["aa","bb","cc"]
When iterating over a [[type:map]] the key is also available:
>> for each key:variable in list do expression
For example:
> (for each k:v in [name:"Thing", rank:"High"] do
> "Its {k} is {v}. "
> ) == "Its name is Thing. Its rank is High. "
When iterating over a list, the key is the index in the list, starting with @0@:
> (for each k:v in ["a","b","c"] do
> ["element {k} is {v}"]
> ) == ["element 0 is a","element 1 is b","element 2 is c"]
It is also possible to iterate over a range of values:
> for variable from begin to end do expression
The expression is evaluated for each number from begin to end (including begin, not including end). The variable becomes set to that each number in succession. Again, the results of the expression are combined using +.
--Summary--
! Syntax Description
| @if a then b else c@ If @a@ is @true@ evaluates to @b@, otherwise evaluates to @c@.
| @for each x in list do something@ Does @something@ for each element in a list, adding the results
| @for each k:v in list do something@ Does @something@ for each element in a map using the key/index in the map.
| @for x from 1 to 100 do something@ Does @something@ for all numbers from 1 to 100.