system/corennnnn
リビジョン | a1804ddebaf8f54f922716dcfe83d9726dd18493 (tree) |
---|---|
日時 | 2009-06-13 06:40:04 |
作者 | Jack Palevich <jackpal@goog...> |
コミッター | Jack Palevich |
Allow local variables to be declared anywhere in a block.
@@ -2254,8 +2254,9 @@ class Compiler : public ErrorSink { | ||
2254 | 2254 | next(); |
2255 | 2255 | } else if (t == EOF ) { |
2256 | 2256 | error("Unexpected EOF."); |
2257 | - } else if (t < TOK_UNDEFINED_SYMBOL) { | |
2258 | - error("Unexpected symbol or keyword"); | |
2257 | + } else if (!checkSymbol(t, &tString)) { | |
2258 | + // Don't have to do anything special here, the error | |
2259 | + // message was printed by checkSymbol() above. | |
2259 | 2260 | } else { |
2260 | 2261 | if (t == TOK_UNDEFINED_SYMBOL) { |
2261 | 2262 | t = (intptr_t) mSymbolTable.addGlobal( |
@@ -2370,7 +2371,10 @@ class Compiler : public ErrorSink { | ||
2370 | 2371 | void block(intptr_t l, bool outermostFunctionBlock) { |
2371 | 2372 | intptr_t a, n, t; |
2372 | 2373 | |
2373 | - if (tok == TOK_IF) { | |
2374 | + if (tok == TOK_INT || tok == TOK_CHAR) { | |
2375 | + /* declarations */ | |
2376 | + localDeclarations(); | |
2377 | + } else if (tok == TOK_IF) { | |
2374 | 2378 | next(); |
2375 | 2379 | skip('('); |
2376 | 2380 | a = test_expr(); |
@@ -2418,8 +2422,6 @@ class Compiler : public ErrorSink { | ||
2418 | 2422 | mSymbolTable.pushLevel(); |
2419 | 2423 | } |
2420 | 2424 | next(); |
2421 | - /* declarations */ | |
2422 | - localDeclarations(); | |
2423 | 2425 | while (tok != '}' && tok != EOF) |
2424 | 2426 | block(l, false); |
2425 | 2427 | skip('}'); |
@@ -2548,31 +2550,30 @@ class Compiler : public ErrorSink { | ||
2548 | 2550 | } |
2549 | 2551 | |
2550 | 2552 | bool checkSymbol() { |
2551 | - bool result = isSymbol(); | |
2553 | + return checkSymbol(tok, &mTokenString); | |
2554 | + } | |
2555 | + | |
2556 | + bool checkSymbol(int token, String* pText) { | |
2557 | + bool result = token < EOF || token >= TOK_UNDEFINED_SYMBOL; | |
2552 | 2558 | if (!result) { |
2553 | 2559 | String temp; |
2554 | - if (tok == EOF ) { | |
2560 | + if (token == EOF ) { | |
2555 | 2561 | temp.printf("EOF"); |
2556 | - } else if (tok == TOK_NUM) { | |
2562 | + } else if (token == TOK_NUM) { | |
2557 | 2563 | temp.printf("numeric constant"); |
2558 | - } else if (tok >= 0 && tok < 256) { | |
2559 | - temp.printf("char \'%c\'", tok); | |
2560 | - } else if (tok >= TOK_KEYWORD && tok < TOK_UNSUPPORTED_KEYWORD) { | |
2561 | - temp.printf("keyword \"%s\"", mTokenString.getUnwrapped()); | |
2564 | + } else if (token >= 0 && token < 256) { | |
2565 | + temp.printf("char \'%c\'", token); | |
2566 | + } else if (token >= TOK_KEYWORD && token < TOK_UNSUPPORTED_KEYWORD) { | |
2567 | + temp.printf("keyword \"%s\"", pText->getUnwrapped()); | |
2562 | 2568 | } else { |
2563 | 2569 | temp.printf("reserved keyword \"%s\"", |
2564 | - mTokenString.getUnwrapped()); | |
2570 | + pText->getUnwrapped()); | |
2565 | 2571 | } |
2566 | 2572 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
2567 | 2573 | } |
2568 | 2574 | return result; |
2569 | 2575 | } |
2570 | 2576 | |
2571 | - /* Is a possibly undefined symbol */ | |
2572 | - bool isSymbol() { | |
2573 | - return tok < EOF || tok >= TOK_UNDEFINED_SYMBOL; | |
2574 | - } | |
2575 | - | |
2576 | 2577 | void globalDeclarations() { |
2577 | 2578 | while (tok != EOF) { |
2578 | 2579 | Type base; |