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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
# Built-in expression parser
Turing has a built-in mathematical expression parser, which is used when the program is in pseudocode mode.
The language is case-insensitive for keywords, i.e. `FALSE`, `false` and `FaLsE` are the same thing.
## Types
### Number
Stored using a Python `float`, automatically converted to `int` when needed.
A point (`.`) shall be used to separate the integer part from the fractional part.
#### Example
- `42`
- `3.1415`
### Boolean
Stored using a Python `bool`, automatically converted to Number when needed (only if strict mode is disabled).
#### Syntax
The following keywords are available:
- `TRUE` / `VRAI` : True value
- `FALSE` / `FAUX` : False value
### String
Stored using a Python `str` (Unicode).
A string is enclosed between double quotes (`"`). It is not possible yet to escape characters inside a string literal.
### List
Stored using a Python `list`, indices start at 0 (like in any real programming language. *Lua*, you should feel ashamed of yourself).
#### Operators
Certain operators can be applied on operands of type List.
##### `+` (Plus)
Concatenates two lists.
###### Examples
- `[1, 2, 3] + [3, 4, 5] == [1, 2, 3, 3, 4, 5]`
##### `-` (Binary minus)
Returns all items of A that are not in B.
###### Examples
- `[1, 2, 3] - [3, 4, 5] == [1, 2]`
##### `-` (Unary minus)
Returns a reversed copy of the list.
###### Examples
- `-[1, 2, 3] == [3, 2, 1]`
##### `*` (Times)
*Must be used on a List and a Number together.*
Duplicates the list the specified amount of times.
###### Examples
- `[1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]`
##### `&` / `ET` (Intersection)
Returns all items of A that are also in B.
###### Examples
- `[1, 2, 3] & [2, 3, 4] == [2, 3]`
##### `|` / `OU` (Union)
Returns an ordered list of all items of A and all items of B, without duplicates.
###### Examples
- `[1, 2, 3] | [2, 3, 4] == [1, 2, 3, 4]`
##### `XOR` (exclusive Or)
Returns an ordered list of all items that are present either in A or B but not both.
###### Examples
- `[1, 2, 3] XOR [3, 4, 5] == [1, 2, 4, 5]`
### Function / lambda
Standard Python function.
#### Calling
A function call consists of the function followed by an argument list enclosed in parentheses. The function can be an identifier or any expression returning a function.
someFunc(arg1, arg2, arg3)
The argument list can be passed as a list object, for example for dynamic calls, using the `*` expanding operator in front of the list object.
gcd(*[248, 4584]) == gcd(248, 4584)
#### Lambda/inline function syntax
Comma-separated list of parameters enclosed in braces, followed by expression enclosed in parentheses. The function can be called immediately. There is no limit to the amount of parameters. A parameter is made of an identifier, no less, no more.
Varargs are not available yet.
##### Examples
- `{x, y}(2 * x + y)(3, 8) == 14`
- `map({a}(2 * a), [2, 3, 4]) == [4, 6, 8]`
## Operators
### Binary operators
| Symbol | Operator | Types |
|--------|----------|-------|
| `+` | Plus, Concatenate | Number, String, List |
| `-` | Minus, Reverse | Number, List |
| `*` | Times, Repeat | Number, List |
| `/` | Divide | Number |
| `%` | Modulus | Number |
| `^` | Power | Number |
| `<=` | Less than or equal | Number |
| `<` | Strictly less than | Number |
| `>` | Strictly greater than | Number |
| `>=` | Greater than or equal | Number |
| `==` | Equals | All |
| `!=` | Not equals | All |
| `&` / `ET` | AND / Intersection | Number, Boolean, List |
| <code>|</code> / `OU` | OR / Union | Number, Boolean, List |
| `XOR` | Exclusive OR / Exclusive union | Number, Boolean, List |
### Unary operators
| Symbol | Operator | Types |
|--------|----------|-------|
| `-` | Negate | Number, List |
| `NON` | Invert | Boolean |
The numbers and booleans are treated the same way as in **plain Python**. In other words, booleans can be treated as numbers (`False` becomes 0 and `True` becomes 1), and numbers can be treated as booleans (0 becomes `False` and everything else becomes `True).
If **strict mode** is enabled, operators can only be used with operands of **identical type**, and **implicit number-boolean casts** are disabled. The `c_bool` and `c_num` functions can then be used for that purpose.
## Function library
The engine provides many functions that can be used with almost all value types.
In the following table, a type name followed by a star (*) means that the function accepts a variable argument list of the specified self.
#### Example
The `average` function accepts either List(Number) or Number*. Thus, it can be used either with a list object: `average(myList)` or with varargs: `average(1, 2, 3)`.
| Name | Parameters | Description |
|------|------------|-------------|
{{{funcdoc}}}
Useful (?) constants are also provided, with the maximum supported precision.
| Name | Approximated value | Description |
|------|-------------------:|-------------|
{{{constdoc}}}
|