struct PROP
{
int number_value;
char * string_value;
OBJECT * obj_value;
};
struct prop
{ int type;
void * value;
}
switch(p->type)
{ case TYPE_INT: i_value = (int)(*value); break;
}
struct prop
{ union
{ int number;
char * string;
OBJECT *object;
} value;
int type;
};
if ch.race == "borg" then
os.output("hi!")
end
– so the token list would be
"if" "ch" "." "race" "==" ""borg"" "then" "os" "." "output" "(" ""hi!"" ")" "end"
– then the linked list of operations might look something like this
- if
-> ch.race == "Borg"
-> then
-> os.output("Hi!")
-> end
enum TYPE_TAG {
TYPE_VOID,
TYPE_NUMBER,
TYPE_STRING,
TYPE_OBJECT
};
struct OBJECT {
enum TYPE_TAG tag;
union {
int number;
char* string;
OBJECT* object;
} d;
};
/* parse a file, which is a series of math expressions */
int parse_file(void) {
/* loop until we hit the end of the file */
while (!token_accept(TOKEN_EOF)) {
/* if we have an empty line, that's just fine */
if (token_accept(TOKEN_NEWLINE)) {
/* nothing to do */
}
/* we expect a valid expression, error if we don't get one */
if (!parse_expr()) {
return 1;
}
}
/* parsed fine */
return 0;
}
/* parse an expression */
int parse_expr(void) {
/* expect a terminal value */
if (!parse_terminal()) {
return 1;
}
/* expect zero or more binary operators */
while (token_accept(TOKEN_ADD) || token_accept(TOKEN_SUBTRACT)) {
/* need another terminal after each operator */
if (!parse_terminal()) {
return 1;
}
}
return 0;
}
I realize that the result may not be anything useful, other than a learning experience.
So far I've thought about designing a tiny structure of a language. I want it to be able to
add classes and objects with variables and methods. So far I have decided to go with C for speed, maybe
C++. One of the first things I've thought about is creating primative data types as Objects similar to Ruby.
I dont see much complexity in using some sort of generic style datatype call it OBJECT for now. OBJECT
can be any of the primative types: (string, number, bool, etc), but it can also be any user defined datatype. Each object would have a linked list of PROP *properties, and FUNC *functions.
One of the issues I have is figuring out how I want to store the values of the class / instance variables. Because we are restricted with C's static typing, I had the idea of storing each type as a char* which
would be converted to integer in case of numeric operations, or it would store the ID for the OBJECT
that it was linked to. The alternative I suppose would be for each PROP to have multiple value properties:
Obviously, I have a long way to go. More or less, I would mainly like to have a higher understanding of
how this SHOULD be done.