Alright I got it worked out to where it accepts strings or numbers. Originally I had:
// lexer rules{STR} {StringConstant (); /* string constant: copy contents */ return STRING;}// yacc token definitions%token <str> ID STRING// yacc rule type definitions%type <symbol> identifier string// yacc grammar rulessimple_expression : identifier {$$ = new TreeNode (IDENT_EXPR); $$->symbol = $1;} | string {$$ = new TreeNode (STR_EXPR); $$->symbol = $1;}
(with a definition for "identifier" and "string", as per the flipcode tutorials.)
I modified this to be:
// lexer rules{STR} {StringConstant (); /* string constant: copy contents */ return STRING;}{NUM} {NumValue (); /* number: copy value */ return NUMBER;}// yacc token definitions%token <str> ID STRING NUMBER// yacc rule type definitions%type <symbol> identifier string number// yacc grammar rulessimple_expression : identifier {$$ = new TreeNode (IDENT_EXPR); $$->symbol = $1;} | string {$$ = new TreeNode (STR_EXPR); $$->symbol = $1;} | number {$$ = new TreeNode (NUM_EXPR); $$->symbol = $1;}
(with an additional definition for "number")
This works; I can now accept numbers as well as strings.
Now, what I'm noticing, is that my virtual machine will have to have variables that can be either strings or numbers. For example, if the script is like this:
a = "hello";b = 42;
I'll get symbols for a, b, "hello", and 42. The virtual machine will get virtual assembly like this:
1: OP_NOP 2: OP_PUSH strconst1 3: OP_GETTOP a 4: OP_DISCARD 5: OP_PUSH numvar1 6: OP_GETTOP b 7: OP_DISCARD
So at runtime, whenever I OP_GETTOP, I'd have to figure out the type of the variable and then assign it to a or b as such. This is fairly inefficient for my purposes; I'd like to know from the get-go what type of values the variables can hold. Essentially, I want to do type checking, so that my script will have to be as such:
string a = "hello";int b = 42;
But I'm struggling with getting that to work. Right now, I could have a script like this:
a;
And it would happily recognize 'a' as a symbol. Instead, I want to have to specify a type for a so that I know what it can be assigned.
... any help on
that?
[Edited by - Funkymunky on March 12, 2010 12:30:21 PM]