🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Tangent: Expressions

Published April 11, 2008
Advertisement
One of the key desires which evolved with my toy language Tangent was to allow fairly arbitrary operators. As long as a method met a certain signature, the language should allow it to be used as an operator.

This moves a lot of the expression parsing from the parser proper into the syntax checking portion of a traditional interpreter/compiler setup. It is also one of the most dubious parts of the design as far as me being actually able to implement it. Also it was one of those things that cried out to be one of those cases where something is a lot more difficult than it seems or a lot cooler in design than in use.

Anyways, I have at least the simple case completed; with the most daunting use case. Take a list of arbitrary identifiers, and figure out how they make sense. The test case is simply: c = a + b; where a,b and c are all strings. Assignment has a definition for void(string,string) and plus has a definition for string(string,string).

This is an interesting case since my original idea was going to be 'read from left to right, use parens otherwise'. That is a lot easier to implement, but would require things to be c = (a + b); which is annoying. So the parsing for this goes:


(c = a) + b; // oops, that makes no sense.
c = (a + b); -> (c = (a + b)); // good.


The trick to making the processing sane was to consider it just like any other parser. Do a simple recursive descent. The only differences are that type info is actually available, and the target needs to be continually adapted as the partial parsing attempts go through it.

and the standard screenshot:
c = a + b;Identifier Paths:---TestStatementLocalsc---assignment---TestStatementLocalsa---plus---TestStatementLocalsbassignment  c  plus    a    b


I'm not sure if I'll be able to stick with this sort of order of operations. I think with an IDE tool it will be okay. Otherwise the left to right strict style might be required.

Next on the list is expressions with method groups, parentheses, and turning this output into something usable.
Previous Entry Method Groups in Tangent.
Next Entry Tangent: Parsing
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement