ThakeeNathees / pocketlang

A lightweight, fast embeddable scripting language.
https://thakeenathees.github.io/pocketlang/
MIT License
1.52k stars 80 forks source link

[Enhancement] conditional expression #284

Open khchen opened 2 years ago

khchen commented 2 years ago

In many other language, ternary operator (:?) is the syntax for basic conditional expression. However, in some language (e.g. coffeescript), they use if expression as conditional expression.

This patch adds if expression as the conditional expressionf for pocketlang. I think it is more clear than ternary operator especially there is more then one condition (if ... elif ... else ...).

In the if expression, than can be omit, and end is not necessary (aslo not allowed). Example:

print(if true then 123 else 456)
print(if true 123 else 456)
print(if false 123 elif false 546 else 789)

a = if false
      123
    elif false
      456
    else
      789

print(a)

Output:

123
123
789
789