Add arithmetic expressions
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# Arithmetic Expression
|
||||
|
||||
Decodal supports arithmetic over concrete numeric values.
|
||||
|
||||
```dcdl
|
||||
{
|
||||
workers = 2 + 2;
|
||||
timeout = 30.0 / 2;
|
||||
port = 8000 + 80;
|
||||
negative = -1;
|
||||
}
|
||||
```
|
||||
|
||||
## Operators
|
||||
|
||||
- `+` addition
|
||||
- `-` subtraction
|
||||
- `*` multiplication
|
||||
- `/` division
|
||||
- unary `-` negation
|
||||
|
||||
`*` and `/` bind tighter than `+` and `-`.
|
||||
Parentheses can be used to make grouping explicit.
|
||||
|
||||
```dcdl
|
||||
2 + 3 * 4 # 14
|
||||
(2 + 3) * 4 # 20
|
||||
```
|
||||
|
||||
## Numeric behavior
|
||||
|
||||
Arithmetic requires concrete `Int` or `Float` operands.
|
||||
`Int + Int`, `Int - Int`, and `Int * Int` produce `Int` when no overflow occurs.
|
||||
Mixed `Int` / `Float` arithmetic produces `Float`.
|
||||
Division always produces `Float`.
|
||||
|
||||
Division by zero and integer overflow are evaluation errors.
|
||||
|
||||
Arithmetic expressions can be used anywhere a concrete numeric expression is expected, including defaults and numeric constraints.
|
||||
|
||||
```dcdl
|
||||
port = Int & > 4000 + 42 default 8080;
|
||||
```
|
||||
Reference in New Issue
Block a user