I am adding "standard" binary and unary logical and arithmetic operators to the template language. The question: "what is the precedence of the operators?"
In line with other languages, my own preferences, and the characteristics of the existing language; the current choice from highest to lowest (binding first to last) is:
- Function calls, constants, parenthesized expressions, statement expressions, assignment expressions. Remember that in the template language, 'if', 'for', and assignment return a value.
- Unary plus (+) and minus (-). Although I can't see why it matters, these operators evaluate right to left. These and the other arithmetic operators return integers if the expression doesn't produce a fractional part.
- Multiply (*) and divide (/). These operators are associative and evaluate left to right. Use parentheses if you want to change the order of evaluation.
- Add (+) and subtract (-). These operators are associative and evaluate left to right.
- Numeric and string comparisons (these already exist). These operators return '1' if the comparison is True, otherwise ''. Comparisons are not associative:
a < b < c produces a syntax error. Comparisons return '1' if True and '' if False.
- Unary logical not (!). This operator returns '1' if the expression is False (evaluates to the empty string), otherwise ''.
- Logical and (&&). This operator returns '1' if both the left-hand and right-hand expressions are True, the empty string '' if either is False, is associative, evaluates left to right, and does short-circuiting.
Spoiler:
Regarding short-circuiting: for example this program produces the answer '4'. Because of short-circuiting the right-hand expression, the assignment, is evaluated because the left-hand expression is True. The assignment is done.
Code:
program:
a = 5;
'a' && (a = 4);
a
This program produces '5' because the the left-hand expression is False so because of short-circuiting the right-hand expression is not evaluated. The assignment is not done.
Code:
program:
a = 5;
'' && (a = 4);
a
- Logical or (||). This operator returns '1' if either the left-hand or right-hand expression is True, '' if both are False, is associative, evaluates left to right, and does short-circuiting. It does an inclusive or, returning '1' if both the left- and right-hand expressions are True.