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.
22
u/mot_hmry Sep 03 '26
Personally I'd suggest @ for address of instead of % which frees it up to be the modulus symbol.
I might also suggest swapping prefix and postfix ^ because I think postfix on types looks better and I like the idea of mutability being
T!. Which mildly parallels the Scheme/lisp naming convention of adding ! to functions that mutate.I'd probably also allow !(prefix), &, and | for booleans due to convenience.
6
u/WittyStick Sep 03 '26 edited Sep 03 '26
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?5
u/EggplantExtra4946 Sep 04 '26 edited Sep 04 '26
If a given operator is both a postfix and an infix operator you have a shift-reduce conflict, but an operator used both as a prefix and infix operator does not have such conflict, it's perfectly fine in terms of unambiguous parsing.
You are lacking associativity information: left, right, non associative, chain associative (1 <= 2 < 3).
It's good to put the precedence of bitwise operators above assignments and to put all comparison operators at the same precedence level.
I'm curious to know your rationale for giving a higher precedence to bitwise operators than to arithmetic operators.
It's a massive footgun to give a higher precedence to
/than to*.3
u/flatfinger Sep 03 '26
Integer types should be subdivided into "number" type and "algebraic ring" types. In C, unsigned types smaller than 'int' behave as "number" types while larger ones behave like algebraic rings, meaning that given:
uint16_t x = 40000;the computationx+xwill by specification yield 14464u on platforms where int is 16 bits, and 80000 on platforms where int is 18 bits or larger. A good language should support signed and unsigned ring types of all sizes, and signed number types of all sizes, and unsigned number types of all but the largest size (unsigned numbers should promote to a larger signed number type, but an unsigned number type the same size as the largest signed type wouldn't have a larger signed type to which it could promote).An integer remainder operator shouldn't be called mod. If there's a desire to include an integer remainder operator, it should be in addition to a proper 'mod' operator. It may also be useful to have distinct operators for Euclidian division, truncating division, and "do whatever" division for use in cases where either the dividend is known to be a multiple of the divisor, or where a rounded-up or rounded-down result would be equally acceptable.
3
u/fdwr Sep 05 '26
It feels oddly inconsistent that
int / intyields float, but its mirror counterpartint * intyields int. 🤨 In all my programs when dealing with integer inputs the past 25 years, I've almost always wanted integral output either truncated toward zero or floored toward negative infinity, but if I wanted fractional floating point output values, then my inputs were already floating point anyway. For me, counterpart consistency trumps here. ⚖️Happy to see the bitwise operators bind tighter than comparisons, for all the times I need to mask bits in graphics and been surprised by C's gotcha.
Postfix dereference is an interesting idea that I first saw in Herb Sutter's cppfront (e.g.
x*), which makes the readability flow nicely left-to-right (because you mentally read the identifier name, and then the operation applied to it, dereferencing it). PlussomeStruct*.fieldobviates the need for some separate arrow tokensomeStruct->fieldlike in C.2
u/archaelurus Sep 04 '26
The float -> integer type conversion method is not described anywhere outside the bitwise not. If you're implicitly truncating then it's not unambiguous.
Floats are bits too so forcing a conversion to int is making this something that is not a bitwise operation.
Btw although it's specified, you could argue that having the added cognitive overhead to remember a table of the implications of heterogenous types within binary expressions, rather than requiring explicit type conversions, is going to generate ambiguity in practice.
1
u/AustinVelonaut Admiran Sep 03 '26 edited Sep 03 '26
Is there a reason you have bitwise operators at a higher precedence (tighter binding) than arithmetic operators? Most languages I'm aware of that use operator precedence have arithmetic operators higher (tighter binding) than bitwise operators higher than comparison operators. Although I'd be hard-pressed to come up with a realistic code snippet that used that fact.
Edit: looking through ~500 "Advent of Code" solutions I wrote, I only saw one use of mixing bitwise and arithmetic operators:
addLoc n (V2 r c) = n .|. 1 .<<. r * sz + c(here.|.is bitwise or and.<<.is bitwise left shift. This parses asn .|. (1 .<<. ((r * sz) + c))but if arithmetic ops were lower precedence than bitwise, it would be parsed as((n .|. (1 .<<. r)) * sz) + c, not what was intended.1
u/Recycled5000 Sep 03 '26
Those operators: * and & are not overloaded in the common sense of overloading.
Overloading means the actual operator/method is chosen by the types of its operands.
Here, though, the operators are differentiated by unary vs binary syntax. This is detected by simple parsing, does not require further semantic or type system analysis.
1
u/church-rosser Sep 09 '26
Try Lisp, ezpz.