Show Changes Show Changes
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Administration Page 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 List all versions

RSS feed for the TestJwd namespace

Wiki Talk Operator Discussion
.
Summary

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:

Arithmatic, comparison and boolean operators.

 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.

Note
Note
Right now the parser knows all of the operator tokens and has an enum for each of them. I've thought about whether this list could be generated dynamically at load, by scanning all classes that have an ExposedOperator attribute and pulling the operator out of the attribute. However there's a lot of collision detection that would need to be done with the tokenizer to ensure that someone didn't try to define 4 as an operator since that also parses as an integer. Also the parser has no knowledge of the type system, so it couldn't intelligently decide whether an operator would apply to the lvalue or not.

More importantly it would just be overkill. While the ExposedMethod system is very flexible, i don't think that level of flexibility is really necessary for operators.

Parenthetical expressions

 (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?

Comments/Questions

Not logged in. Log in

The wiki for all things Objective Design Solutions

This is FlexWiki, an open source wiki engine.

This site supports the new NoFollow anti-spam initiative.
Change Style

Recent Topics