Merge changes 4101,4102

* changes:
  Allow local variables to be declared anywhere in a block.
  Support variable initialization.
This commit is contained in:
Android (Google) Code Review 2009-06-12 14:43:42 -07:00
commit ba4dea8120
1 changed files with 42 additions and 18 deletions

View File

@ -2254,8 +2254,9 @@ class Compiler : public ErrorSink {
next();
} else if (t == EOF ) {
error("Unexpected EOF.");
} else if (t < TOK_UNDEFINED_SYMBOL) {
error("Unexpected symbol or keyword");
} else if (!checkSymbol(t, &tString)) {
// Don't have to do anything special here, the error
// message was printed by checkSymbol() above.
} else {
if (t == TOK_UNDEFINED_SYMBOL) {
t = (intptr_t) mSymbolTable.addGlobal(
@ -2370,7 +2371,10 @@ class Compiler : public ErrorSink {
void block(intptr_t l, bool outermostFunctionBlock) {
intptr_t a, n, t;
if (tok == TOK_IF) {
if (tok == TOK_INT || tok == TOK_CHAR) {
/* declarations */
localDeclarations();
} else if (tok == TOK_IF) {
next();
skip('(');
a = test_expr();
@ -2418,8 +2422,6 @@ class Compiler : public ErrorSink {
mSymbolTable.pushLevel();
}
next();
/* declarations */
localDeclarations();
while (tok != '}' && tok != EOF)
block(l, false);
skip('}');
@ -2524,14 +2526,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();
}
@ -2540,27 +2550,30 @@ class Compiler : public ErrorSink {
}
bool checkSymbol() {
bool result = isSymbol();
return checkSymbol(tok, &mTokenString);
}
bool checkSymbol(int token, String* pText) {
bool result = token < EOF || token >= TOK_UNDEFINED_SYMBOL;
if (!result) {
String temp;
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());
if (token == EOF ) {
temp.printf("EOF");
} else if (token == TOK_NUM) {
temp.printf("numeric constant");
} else if (token >= 0 && token < 256) {
temp.printf("char \'%c\'", token);
} else if (token >= TOK_KEYWORD && token < TOK_UNSUPPORTED_KEYWORD) {
temp.printf("keyword \"%s\"", pText->getUnwrapped());
} else {
temp.printf("reserved keyword \"%s\"",
mTokenString.getUnwrapped());
pText->getUnwrapped());
}
error("Expected symbol. Got %s", temp.getUnwrapped());
}
return result;
}
/* Is a possibly undefined symbol */
bool isSymbol() {
return tok < EOF || tok >= TOK_UNDEFINED_SYMBOL;
}
void globalDeclarations() {
while (tok != EOF) {
Type base;
@ -2579,12 +2592,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;
}