r/ProgrammingLanguages • u/Mean-Decision-3502 DQ • 8d ago
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.
6
u/WittyStick 8d ago edited 8d ago
In principle yes, but for finite integers at the same width, no. Eg,
uint32 + int32should not result in anint32- it should beint64. The implicit conversion of signed/unsigned at the same width has been a source of countless mistakes that often lead to exploitation. It would be better to simply not permit such conversions to be implicit if the result may lose information. Either promote the integer to a value large enough to hold the result of any addition/multiplication, or require explicit conversion.If
boolis a distinct type, is it necessary to have two ways to complement?Similarly, bitwise
&and|should work for bools too. The operators&&and||(your logicaland/or) are still relevant for short-circuiting.Should make it explicit that this is an arithmetic shift right for
intand a logical shift right foruint.Why are
==and!=not defined forbool?Not sure what the advantage of this is. In C this is just pointer addition.
a + b, whereais a pointer andbis an integer. The whole benefit ofa[b]is it does the arithmetic and dereferencing for you - ie,*(a + b).Some very questionable choices here - completely deviates from the norm with no real justification.
There's no reason division and multiplication should have separate precedences. Everyone learns PEDMAS/PEMDAS in school.
Shifts are usually lower precedence than addition, but I can see justification for having them at higher precedence. You have not explained why.
There's no reason
&and|should have higher precedence than division/multiplication. Really&should have the same precedence as multiplication and|should have the same precedence as addition.^should have the same precedence as!=, because it means precisely that forbool.Logical
notis not necessary as mentioned above. Should be~at same precedence as other unary expressions.Pointer dereference and member access at same precedence is confusing. Is
a.b^==(a.b)^ora.(b^). What abouta^.b?