🎉 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!

Designing a scripting lanaguage

Started by
9 comments, last by Ironica 21 years ago
Hey guys, hehe.I wanna design a very basic scripting language (in C++) for a game. Now I started reading lines from a file, then used strstr to seperate arguements etc. I''m just kinda having problems implementing if... else, and stuff. I did get off to a pretty good start, could call a function with arguements and so far worked well. But then I got kinda confused over writing an if statement. I wanna keep it simple, so I''d want it look something like: @if boolean_function_1 5 7 // 5 and 7 being the arguements boolean_function_2 @then void_function_1 @else void_function_2 @endif Anyone point me in the right direction? To be honest, I''m pretty clueless, and am not even sure if reading the file line by line is a good way to do it. Since my if statement is on multiple lines, I''m not really sure how to go about doing this.
Advertisement
Check the boolean values and get a true or false out of it.

If the answer is true, you have to execute the ''then'' block so just continue to the line immediately following the ''then''.

If the answer is false, you have to execute the ''else'' block if one exists or skip the whole thing otherwise. This requires skipping over lines to find the relevant ''else'' or ''endif'' but keeping track of any nested ''if''s. In other words, skip lines, but if you see an ''if'', increment a counter, and if you see an ''endif'' decrement that counter. You''ve found where you want to be when you see an endif and the counter is back at the starting value.

In the course of executing any code that is in a conditional, you may come across an ''else'' statement, in which case you do the line skipping thing again, being careful to count ''if'' statements as above, and resume after the matching endif.

The trick is in managing nested if blocks and the little counter is the key to that. Once you start having other blocks like whiles or fors, you may want to consider having 1 counter that handles all these different scopes and instead of initialising it newly each time, just skip lines looking for whatever the current value is.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Ok, you''re going for interpreted language, but nevertheless check out Let''s Write A Compiler, which will give you quite a few pointers on parsing constructs.

2DNow - Yesteryears technology at your fingertips! - REDESIGNED!
Ahh thanks guys, that was pretty helpful. One other thing I was having trouble with is ignoring whitespaces, without using fscanf (I''m using ifstream). But I''ll read that link you posted llyod, and maybe I will learn some better ways to do it.
If you use the insertion operators, whitespace is ignored by default. Otherwise, use this manipulator to explicitly remove all following whitespace:

mystream << ws; // skips whitespace on ''mystream''
Yeah I did think of that. Only I would have to keep calling:
mystream.getline(NULL, 0);
to make it move to the next line.
Hehe, I found it a lot easier to use getline, rather than >>. Because I want to parse a line, and then go to the next line etc. And sometimes >> would read from the next line, when I didn''t want it to. I wrote a great, but simple function to help me with this:

void emit_space(char **p){
while(**p == '' '')
(*p)++;
}

And can easily be edited to pass tab spaces etc. I had fun practicing what you said, keeping track of nested statements. But I have somehow come up with something much easier, and it works well. Basically, I wrote my parse function to just parse one "block", and keep parsing until it reached a value, which is passed as an arguement. Initially, the value would be the end of file. But once it finds an if statement, it calls the same function (recursion), only this time the value passed as an arguement is "@EndIF" =) The getline function was called a few times within the recursed function, so when it returned, the parser had already passed it''s @EndIf, and didn''t need to keep track of how many times it was recursed =)

Thanks for the help. I learned a lot, and got the result I wanted!
quote: Original post by Ironica
Hey guys, hehe.I wanna design a very basic scripting language (in C++) for a game. Now I started reading lines from a file, then used strstr to seperate arguements etc.


http://dinosaur.compilertools.net/

SpiffGQ
Btw, ''emit'' means to create or give out. You mean ''omit''.

Yeah, recursion may seem a more natural way to handle the if-blocks. Not sure how that would work with breaks, continues, and the like (assuming you wanted any of that).
Ohh well, emit, omit, same thing, same thing, hehe.

Actually, this is gonna be so basic, I''m not even sure if I want loops yet, let alone break and continue. I don''t think they''d be hard to implement though.

This topic is closed to new replies.

Advertisement