Sign in to follow this  
Aternus

Few questions about making a scripting language...

Recommended Posts

What do you mean "way out of my league"? I'll never learn unless I try. Anyways, I've decided to make my homebrew GUI for my program completely based off of a scripting language, so whoever might use it would be able to freely edit the GUI and add what they want. I figured I could use someting like XUL, LUA, or whatever... but then I figured "Hey, it'd be fun to try and make my own for this purpose!". So here I am... just a couple questions on the basic theory. First off, I'm going to make it so it basically loads all the scripts and stores them in a struct or whatever when the program starts up. This way, it wouldn't need to open and parse through a file each time the script is called. This a good idea, or not? I'm thinking of making it very basic, in which you can basically define some things... call some functions... do a loop or two... and I guess a few if's and whatnot. The basic syntax would be something like:
# data, my_int = "500"

# begin script
doFunction(500,"Hello") ,
if(my_int > 250) ,
doNotherFunction(100,750) ,
# end script


Pretty peculiar, and maybe inefficient... but I wanna try and at least go for something original looking (though I bet there's similar out there). Okay, now how I'd go about parsing this is... well I'd have a basic struct along the lines of:
typedef struct _script {
std::vector<char*> data; //Stores all defined stuff
std::vector<void (*)(...)> functions; //Stores functions to be iterated through later
};


I assume that's what the dummy type of a function pointer looks like, anyways. Also I'm curious about the "...", would this be the most efficient way to store functions and then use a va_list or whatever later? I was thinking that you register the function ahead of time (sort of like LUA), and they get stored in a seperate std::map by char* name and the pointer. If you'd like an example of how I could use this for my GUI... let's say we have a folder called "gui", inside it we would find the following files: main.script
# data, ID = "gui-id"
# data, bX = "100"
# data, bY = "100"
# data, bH = "20"
# data, bW = "50"

# begin script
makeButton(ID, bX, bY, bH, bW) ,
setButtonCallback(ID, "button.script") ,
# end script


button.script
# begin script
printMessage("y HELO THAR") ,
# end script


That's a pretty vague idea, but I'm still in the planning stages of this. I only have a very short header and a small library working right now. The final product would be much better... that is if I get to making a final product. But anyways, we're talking about the scripting language here, not how I'd utilize it to work with my GUI. Anyways... I hope that made sense. My basic question is: how does this plan sound? What could I improve upon? And how far do you think I'll make it into this project before my brain explodes? Thanks to anyone who takes the time to tediously help me out here! -Aternus Edit: Blah. Well after doing some more thinking on the subject I figured out why my va_list idea wouldn't work. Registering a void (*)(char*) to a void(*)(...) vector just wouldn't fit. So maybe a push in the right direction as to how I should go about doing this would be very nice. Edit: Blah twice. I'm messing around with void pointers to see if I can get the function passed and now my virus protection of all things won't let the program run! Confusing! I'm *not* trying to hack my own computer here! >_< Here's what I've got so far:
#include <conio.h>
#include <stdio.h>

typedef void (*funcType) (void *);

#define register1(n, f)  register2(n, f)

void register2(char* name, void* func)
{
    void (*func2)(void*);
    func2 = *(funcType*)func;

    func2((void*)"Hello");
    //func2("Hello");
}
void printage(char* msg)
{
   printf("%s",msg);
}

int main(int argc, char** argv)
{
    register2("printage", (void*)printage);
    //callBack("Test2");
    getchar();
    return EXIT_SUCCESS;
}
Yes it's very messy... but that's how I flunk around. I'll obviously clean everything up once I get it working. Somebody mind helping me figure out how I can get this working without setting off my virus guard? [Edited by - Aternus on November 6, 2006 1:32:30 AM]

Share this post


Link to post
Share on other sites
Actually making a script language is way to much work. But if you are interested in something like that you should look for some examples on net, like examples of how to make BASIC language interpreter. It is not so simple as it might seem.
Check out Parsers at Code Project, and look at Bee Basic for BASIC language interpreter example, which is nothing more than simple script language. It is open source.
From these you could learn much.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this