Advertisement

Design Idea

Started by July 20, 2004 11:49 AM
5 comments, last by choffstein 20 years, 4 months ago
Hey everyone. I have decided that I wanted to try my hands at writing my own scripting language. Firstly, please dont ask "why. this will just be YASL (yet another scripting language)." I know. Its called "a personal challenege." I enjoy them. Thats that. So now onto design. I decided right off the bat that I didn't want to compile the script. If I am going to have to compile something into bytecode, I might as well just use a crap load of #defines and typedefs in my C++ code and just write a "script" in a .cpp file. So basically, here is my idea. Please, tell me if it is really infeasable. Basically, upon loading the script I pass it through a lexer then a parser. Instead of creating a parse tree, I instead translate it into C. So if my code said "if player at 200,400 then print 'you cant go there'; fi", I would translate it into "if(player.x == 200 && player.y == 400){ printf("you cant go there!"); }" With each parse, the C code would be sprintf'd to a char *. Then, whenever I want to call the script, I just use a function pointer. Here is where I am having a bit of trouble. Is there some way to implement a function pointer so that it can be bound to any type of function? So if I have one function that returns a char* and takes 4 arguments, and another function that returns an int and takes only one? I want to be able to use the same function pointer prototype. Am I making any sense? So where are the big holes in this besides the time it would take to load a big script? I dont want to start implementing this until I think it through completely, and I figured someone here may be able to help... Now, someone may ask why I dont just write it in C or C++ and load the file in real time instead of doing the whole parser thing. And my answer is because the reason you do a script is to have a more legible language to code in. Plus, this is just a personal challenge and for fun. So any flaws? Thanks.
Wait, why convert the code into C?

I don't see what the point of that is at all.
Not giving is not stealing.
Advertisement
Err...I think what I meant to say is that I would translate it into C->asm -> hex.

So basically, it would be like running shellcode.

Yeah.
No offense indended, but you seem to skim over the important details as if they were trivial and generally seem to not know what you're talking about.

For example, how are you going to execute the code? sprintf doesn't compile anything, and you can't just execute a string as code (without generally bad results anyways, unless you're typing in EICAR or something).

It sounds like you want to make a compiler (despite the fact that you said you don't want compiled script), so you'd need to do some kind of c->machine code translation before you could execute anything, and you'll need to make sure you can use the API of whatever OS you're using to mark the in-memory results as executable.

Either way you'll pretty much have to make a parse tree to make a proper language, because that is generally an important part in language translations (for any remotely complex language anyways).

If you'd like some good references on making a compiler, see my reply here
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Sorry, I typed it far to quickly and didn't type all of what I meant. I appreicate the link to your other thread.

But basically, what I wanted to do was translate the script in realtime from the scripting language, to C, to ASM, to hex. Then use a function pointer to execute it like shellcode. That way you can execute a string as code...

char script[] = "\x ..."; //some "shellcode"void (*go)() = (void *)script;go();


Maybe I am missing something huge. But basically, I just really dont want to have to compile code, but want to parse it in real time. If I wanted to compile something, I would just code it in C and, as I said, use typedefs and defines and shit to make it more legible, then just #include the file in my code. Then I could just recompile the file and load it as a DLL or something.

I dont want to have to do anything but change what is in the file.

Am I making any sense at all? If I am not, just flame the crap out of me please, and enlighten me as to where I am erring.

Thanks a lot for putting up with me :)
'compiling' just means translating from one langauge to another, it doesn't neccessarily mean you have to be like a C compiler where you parse it once and only use the result. In fact, Lisp compilers are actually interactive where they can compile each statement as you finish typing it (depending on the interface, it might be when you press enter, or a special hotkey for an IDE, etc).

If you want to compile your scripting language to machine code, I would suggest you use the SoftWire library or something like it, because that way you can compile it without having to look up all the machine codes etc. You will need to know assembly though, and you'll still have to generate a parse tree (most likely, unless your language is excessively simple or is based on assembler).

If you mean you want to execute it as you parse it(like read one line and then run that one line), well that is a bad idea. It is MUCH better to compile it when you load your game (or level or press a key or whatever) and store the compiled version in memory. You can still reload your script in realtime (like if you want to make changes and test them right away without restarting the game), but if you interpret it line-by-line as you execute it, you'll be constantly recompiling the script which will cause a huge performance hit.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Advertisement
ah, thank you. compiling in real time isn't something I had thought about before. That makes a lot more sense...I had assumed that you either parsed and ran in realtime, or precompiled. Compiling in realtime is exactly what I want.

Thank you.

This topic is closed to new replies.

Advertisement