
![]() |
Show Changes |
![]() |
|
![]() |
Recent Changes |
![]() |
Subscriptions |
![]() |
Lost and Found |
![]() |
Find References |
![]() |
Rename |
![]() |
Administration Page |
| Search |
History
| 9/18/2008 5:27:37 PM |
| 66.78.111.231 |
| 7/15/2008 9:08:41 PM |
| 209.150.59.190 |
| 9/12/2007 3:17:48 PM |
| -10.10.192.22 |
| 8/19/2007 6:55:02 PM |
| -66.78.124.101 |
![]() |
List all versions |
Related Topics
As an exercise to learn the WikiTalk code i decided to go through and add support for operators. The support is fairly simplistic, due in part to my inexperience with parsers, but I think it's a useful addition and one we can build on.
In this work i added the following things to the parser:
1 + 2 1 * 2 1 - 2 4 / 2 4 > 2 4 >= 2 2 < 4 2 <= 4 4 == 4 true && false true || false
All the standard ones are there. Operators can be applied to "elements". An element is a literal, a method invocation chain, or a parenthesized expression.
Operators have their own parse tree node that contains the token for the operator and the left & right hand expressions. They're evaluated in the same way as methods & properties. The ExposedMethod attribute's constructor can take an "operator type" value and will apply that to the corresponding method.
When evaluated the operator parse node evaluates the left-hand parse tree, then invokes the ExposedMethod on the resulting object that maps to the operator type. In this way operators are simply shorthand for instance methods.
This lets the class decide whether it will implement an operator at all, and what the operator will mean. It also allows the class to provide the functionality as a named method. So + can map to String.Append and Integer.Plus depending on the type of the left-hand expression.
(5 + 5) / 2
Originally i added these in so one could explictily control precedence where necessary. However i hit some problems getting the C precedence rules to work well in the flexwiki parser (probably my own inexperience here), so they're now a requirement.
I'm actually not upset about them being a requirement though. It saves us from needing to decide on a set of precedence rules, and from arguing about what language they should match.
In my current implementation
2 + 3
is valid wikitalk, but
2 + 3 + 4
is not - instead you would have to write
(2 + 3) + 4
or
2 + (3 + 4)
It would simplify things even more to require ()'s around any expression that includes an operator .. how do people feel about that?