Trying out a useless operator #79
qwertie
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Submitting issues is easier than writing blog posts, so here we are. I am kind of hoping that discussing how to add features might be useful to people who might want to hack on this someday.
A long time ago I defined a funny prefix operator in LES, called simply
|
. It has lower precedence than any other operator, and I figured it might come in handy for code that looks like Nemerle:[Edit: this operator will morph to a new form - see #86]
This operator has a "precedence override effect": any time it appears in an expression, it gobbles up everything to its right, regardless of what operators appear on the left. For example,
is parsed as
x * (| (a && (b = c)))
despite the high precedence of*
. (Note: this example highlights another quirk of LES;a && b = c && d
is parsed asa && (b = (c && d))
because the standard way of parsing it,(a && b) = (c && d)
, is almost certainly not useful.)So today I thought to myself, wait... why stop there? Couldn't we have a suffix operator that works the same way? I wasn't sure if the generic code I had written for handling arbitrary operators would handle a low-precedence suffix operator, so I did an experiment and confirmed that yes, I could add one successfully. The operator I added is called
:|
, and to support it I made the following changes:Add unit test in
Les3PrinterAndParserTests.PrecedenceOverrideEffect
:Add a new entry for the operator in
Les2PrecedenceMap.PredefinedSuffixPrecedence
(the precedence on the left side of:|
its precedence is -10, lower than any other operator, and on the right, the it's 111, higher than any other operator; the latter number has no effect on the parser but it helps the printer print correctly. The final two numbers-10, -10
indicate that the precedence should be treated as-10
for miscibility checking):Change the lexer (
Les3Lexer.GetOpNameAndType
) to treat the new operator (and derived operators such as:%|
) as aPreOrSufOp
instead of aNormalOp
(a normal op can be a prefix operator or a binary operator, but not a suffix operator):Happily, it worked perfectly, except that the printer doesn't want to add a space before
:|
(probably since none of the existing suffix ops need a space there.)However, is there any value in adding an operator like this? Perhaps not, because yesterday I just added two new arrowlike operators with very low precedence:
|>
and<|
. These operators can be used to similar effect:So I think I'll just rip out these changes.
Beta Was this translation helpful? Give feedback.
All reactions