r/ProgrammingLanguages • u/Mean-Decision-3502 DQ • Sep 03 '26
Unambiguous Operator Specification for Programming Languages
https://nvitya.github.io/pluops/As I changed recently the operators in my programming language I've created this specification:
https://nvitya.github.io/pluops/
I did not wanted to overload the operators like the C does with the / or Pascal does with theand/or/not. Neither re-use the operator symbols for some very different purpose, like C does with * and & so the code becomes more readable. I was orienting for existing solutions so this is what I came up with. The specification contains the symbol usages and operator precedence too.
If you are developing a new programming language, it would be nice to follow some standard, so at least the expressions would be portable between the languages.
I'm open for debates or suggestions.
View all comments
Show parent comment
2
u/EggplantExtra4946 Sep 15 '26
It has nothing to do with optimizations nor with operator precedence for that matter.
If you change the precedence but don't correct the expressions using parentheses, you now have 2 different expressions:
if
*and/have the same precedence and are left-associative:and if
/has a higher precedence than*:In the 2nd case the compiler can fold the division down to a constant. The reason why the compiler can't do that in the 1st case is that for this to happen, first it would have to rewrite the 1st case into the 2nd case, but it can't do that without changing the semantics of the expression because floating pointing multipliciation and division aren't associative with each other. If you don't care about numeric accuracy, the 1st line can be optimized into the 2nd with -ffast-math but again the optimized expression won't be exactly equivalent to the original.
No matter what the operator precedence is, it's on you to put parentheses around subexpressions to make the parser produce the correct AST shape that you want.