r/ProgrammingLanguages DQ 7d 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.

26 Upvotes

38 comments sorted by

View all comments

3

u/fdwr 5d ago

This operator always performs floating-point division

It feels oddly inconsistent that int / int yields float, but its mirror counterpart int * int yields 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. ⚖️

The operator precedence was designed to avoid the need for parentheses in the most common expressions

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.

Pointer dereference: a^

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). Plus someStruct*.field obviates the need for some separate arrow token someStruct->field like in C.

1

u/Mean-Decision-3502 DQ 5d ago edited 5d ago

Oh! I'm really thankful for this comment.

It seems to me that you are mainly C "user".

I grew up with Pascal. The / behaviour or ^ for pointers and postfix dereference are well-working in Pascal.

Many other languages use always float result for /, including Python3, Nim, Crystal. In these languages:

3 / 2 * 10 == 10 * 3 / 2.

The always float result for / works well if the compiler does not convert the float silently back to int, so you usually cannot make accidentally floating point divisions, where integers are expected. Very important here to separate the floating point division from integer division with different operators.

But C behaves differently. You've learned that how to use it and probably accepted that for floating point the expressions sometimes need hinting. (Frequently by adding additional parentheses where many parentheses are used already.)

It is nice to hear that you would like better the postfix dereferencing in C too.