SE250:lab-9:jsmi233
Jump to navigation
Jump to search
I had a go at Option I. I got the equality comparison function down:
bool eqToken( Token a, Token b ) {
if (a.type != b.type)
return false;
switch (a.type) {
case T_SYMBOL:
case T_IDENT:
return a.val.symval == b.val.symval;
case T_STRING:
return a.val.strval == b.val.strval;
case T_INTEGER:
return a.val.intval == b.val.intval;
case T_FLOAT:
return a.val.fltval == b.val.fltval;
}
//For all other types, value comparison is not applicable
return true;
}
The initialize function i guess is supposed to turn a string into a sequence of tokens. I got half way through this, so I'm not sure if I'm on the right track. This is my code:
void init_TokenStream( TokenStream* tokens, char* input ) {
int pos = 0;
int start;
char cur;
bool dotSeen;
while (pos < (int)strlen(input)) {
if (isalpha(input[pos])) {
start = pos;
while (isalpha(input[pos]) || isdigit(input[pos]))
pos++;
//We have a string =D
} else if (isdigit(input[pos])) {
dotSeen = false;
start = pos;
while (true) {
if (input[pos] == '.') {
if (dotSeen)
errorAndDie("Too many dots in numeric expression.");
else
dotSeen = true;
} else if (!isdigit(input[pos]))
break;
}
//We have a number =D
} else if (issymbol(input[pos])) {
//We have a symbol =D
} else if (iswhite(input[pos])) {
//We have whitespace!
} else if (input[pos]=='"') {
start = pos;
pos++;
while (input[pos] != '"' || (input[pos] == '"' && input[pos - 1] != '\\')
pos++;
//We have a string
}
}
}