-
Notifications
You must be signed in to change notification settings - Fork 0
Description
What is the feature about?
Add operator precedence and the ability to modify precedence using parentheses ().
Precedence could mimic C precedence: (associativity in parentheses: LR = left-to-right, RL = right-to-left)
- Function calls and accession (LR)
- Unary operations (logical NOT, unary plus, and unary minus) (RL)
- Multiplication, division, and remainder (LR)
- Addition and subtraction (LR)
- Relational operations: >, >=, <, <= (LR)
- Relational operations: ==, != (LR)
- Logical AND (LR)
- Logical OR (LR)
- Assignment (RL)
- Commas (LR)
Precedence should not affect the lexer (unless the relational and logical operators are added too).
Precedence is resolved in the parser, and thus will heavily affect the parser.
For the RL-associative operations:
- logical NOT should be fairly easy to implement, as it only concerns the token after itself (presuming that no whitespace is allowed).
- assignment will be more difficult if we want to allow chained assignment, as the right-most RHS needs to be evaluated first, then the result needs to be carried back and each identifier in the chain made its own element in the environment.
- if chained assignment would also be allowed between environments and non-environments, things get even more complicated. This could perhaps be left for a later date?
Why is the feature necessary?
Envlang does not currently support multiple binary operations on one line. Even if support for it was hacked together, it still would not have any reasonable notion of precedence (except for whatever order the parser currently follows).
Operator precedence allows for real mathematical calculations and Envlang usage matching real-world problems.
Could the feature currently be done with Envlang?
Precedence can be imitated using multiple assignments. For instance, the fictional line let x = (5 + 3) / 2 could be implemented as:
let temp = 5 + 3;
let x = temp / 2;