Parsing/compiling simple scripts..

Started by
6 comments, last by 3dModelMan 21 years, 3 months ago
I''ve recently finished a nice little particle system that allows for scripted loading of particle behaviour. So far it''s limited to simple behavior but it suits my needs atm. However, it did get me wondering.. How could I approach parsing more complex behavior, say I had; XPOS=sin(TIME*20.0)+cos(TIME*20.0) YPOS=YSTART-(9.8*(TIME*TIME)) ZPOS=cos(TIME*20.0)-sin(TIME*20.0) (I''m not sure that does anything too useful ) This is similar, for exmaple, to the way WinAmp visualisations are defined. I''ve no problem parsing it, but what sort of options do I have for compiling it? Dynamically generating some assembly? Or creating an an array of function pointers, that when traversed, perform the required sequence of ops. It''s something I''ll look into in the future, not something I''m desperate to implement right now, but any ideas would be most welcome. Cheers!
Advertisement
Hi,

I think that the easiest way to do this would be some kind of abstract syntax tree, builded when parsing your tree. For exemple,
10 + (2-5) would give something like this :

	+       / \           /   -          10   |               |  \            2   5   


To get the final result, evaluate left and right branch, apply the op and return the result.
+ :eval left  branch : 10eval right branch :  	-	eval l : 2	eval r : 5	return -3;return 7   

Just put in a few more operation (cos, sin), constant (TIME...) and you're done.


Generating assembly code is also possible, althought harder. There was an COTD on flipcode discussing it.
I wrote a little compiler that directly emits machine code, and I must admit that creating it was quite funny, altought it took me *lots of time* to get it working correctly.



(Two edits to get a correct looking tree, i have to sleep more :D )




[edited by - chrisbk on February 9, 2003 8:54:33 AM]
Jack Crenshaw''s tutorial is perfect for this. Check out http://compilers.iecc.com. The site not only has all the archives for the comp.compilers newsgroup, but also features an excellent tutorial on building a simple compiler.
If you want to make something even vaguely complex I''d suggest looking into the use of parser construction tools, such as ANTLR (which I happen to like) - with this you could actually just parse the script at loadtime, then for run time you could just evaluate the tree - this would be similar to the ''calc'' example supplied in the package. I will admit tho that it can take a little time to get your head around.
quote:Original post by chrisbk
Hi,

I think that the easiest way to do this would be some kind of abstract syntax tree, builded when parsing your tree. For exemple,
10 + (2-5) would give something like this :

	+       / \           /   -          10   |               |  \            2   5    


To get the final result, evaluate left and right branch, apply the op and return the result.
+ :eval left  branch : 10eval right branch :  	-	eval l : 2	eval r : 5	return -3;return 7    

Just put in a few more operation (cos, sin), constant (TIME...) and you''re done.


[edited by - chrisbk on February 9, 2003 8:54:33 AM]


You''re not reinventing the wheel with that. Its called The Polish Notation and processors use it to process mathematical expressions. Look it up.
-----------------Always look on the bright side of Life!
quote:Original post by A Guy from CRO


You''re not reinventing the wheel with that. Its called The Polish Notation and processors use it to process mathematical expressions. Look it up.


Who said anything about reinventing the wheel? Whether you call it Polish Notation or syntax tree doesn''t matter.

Right off the main page...
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
quote:Original post by Thunder_Hawk
Right off the main page...


Oops! My favourites short-cut brings me right onto the forums, but I must admit to the cardinal sin of not checking the resource section here before posting. I almost didn''t want to bump this thread, but just wanted to say thanks for the help

This topic is closed to new replies.

Advertisement