Support variable initialization.

Global variables can only be initialized to integer constants.

Local variables can be initialized to arbitrary expressions.
This commit is contained in:
Jack Palevich 2009-06-12 14:26:58 -07:00
parent f1728bec74
commit d7461a7342
1 changed files with 26 additions and 3 deletions

View File

@ -2524,14 +2524,22 @@ class Compiler : public ErrorSink {
while (acceptType(base)) {
while (tok != ';' && tok != EOF) {
Type t = acceptPointerDeclaration(t);
int variableAddress = 0;
if (checkSymbol()) {
addLocalSymbol();
if (tok) {
loc = loc + 4;
*(int *) tok = -loc;
variableAddress = -loc;
((VariableInfo*) tok)->pAddress = (void*) variableAddress;
}
}
next();
if (tok == '=') {
/* assignment */
next();
expr();
pGen->storeR0(variableAddress);
}
if (tok == ',')
next();
}
@ -2543,7 +2551,11 @@ class Compiler : public ErrorSink {
bool result = isSymbol();
if (!result) {
String temp;
if (tok >= 0 && tok < 256) {
if (tok == EOF ) {
temp.printf("EOF");
} else if (tok == TOK_NUM) {
temp.printf("numeric constant");
} else if (tok >= 0 && tok < 256) {
temp.printf("char \'%c\'", tok);
} else if (tok >= TOK_KEYWORD && tok < TOK_UNSUPPORTED_KEYWORD) {
temp.printf("keyword \"%s\"", mTokenString.getUnwrapped());
@ -2579,12 +2591,23 @@ class Compiler : public ErrorSink {
mTokenString.getUnwrapped());
}
next();
if (tok == ',' || tok == ';') {
if (tok == ',' || tok == ';' || tok == '=') {
// it's a variable declaration
for(;;) {
if (name) {
name->pAddress = (int*) allocGlobalSpace(4);
}
if (tok == '=') {
next();
if (tok == TOK_NUM) {
if (name) {
* (int*) name->pAddress = tokc;
}
next();
} else {
error("Expected an integer constant");
}
}
if (tok != ',') {
break;
}