I'm currently toying with developing a scripting language, not to be used extensively, but as a learning experience. I'm currently stuck trying to decide how best to create a generic variable class.
Should I create:
A) a safe class that is limited in it's types and has a small footprint on the memory
or
B) An unsafe type that is unlimited in how it's implemented and has a relatively large footprint.
What do you guys think? I can do either easily, but it will effect how I make functions and objects work later.
In your language, what is the user capable of? Can they only use built-in types, or are they allowed to define their own types? How are these types created? Through a class mechanism or through a system similar to typedef?
class mechanism. the generic variable i'm talking about is more like the var keyword in some scripting languages. That can change data type based on the data given to it. for instance
void* GeneticVariable(int type)
{
void* v = 0;
switch(type)
{
case type_float:
v = (void*)newfloat;
break;
//...
}
return v;
}
you could do it with a struct that holds the pointer, and the type. Just remember to convert it to the right type when youre going to use it, and to delete itdelete (float*)v; when youre finished
I've never been comfortable with void pointers :O though that would take a much smaller amount of memory :\ I'll think about it..it's better Idea than my method :O just not as safe :P
That depends on your code lol, void pointers are perfectly safe if you use them correctly
Anyway, just pick the easiest, you probably wont have to worry about memory usage too much with a scripting language youre making for fun
Just get it to work first, then you can worry about memory usage and speed :P
No route is particularly difficult. as for memory usage? That's one of the things i'm trying to teach my self with this. optimizing my code for best performance. :D
Is the static type supposed to allow user types? If not, you can just use a string for everything. This how simple weakly-typed languages do it. Otherwise, you need to implement a metatype system. There's no need to resort to unsafe approaches.
Does using strings work for floating point numbers? I know you can convert integers to strings and vice versa, but how does that work for floats? You need some way of accessing real numbers...
Edit: He implicitly said he needs user-defined types:
NGen wrote:
Can they only use built-in types, or are they allowed to define their own types? How are these types created? Through a class mechanism or through a system similar to typedef?
Well, since you're writing the interpreter, can't you just take into account all defined classes and define your generic variable as a union of all of them? You could generate the overloaded operators needed to get assigning the variables possible, that is, assuming that your scripting language allows overloaded operators.
Overloading? heh I haven't gotten there yet. Incidentally I can't actually access my code at the moment, everything I own computer wise (minus my netbook) is packed away, I move into the dorms tomorrow. But I digress, the string idea should work well.