From 9fc685a2237b789800b770f3bc1eecb8e1bfdf7b Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 19 Aug 2021 07:52:26 +0000 Subject: c4: added switch statement --- miniany/cc.c | 143 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 84 insertions(+), 59 deletions(-) (limited to 'miniany/cc.c') diff --git a/miniany/cc.c b/miniany/cc.c index e5b30c7..ba7ead2 100644 --- a/miniany/cc.c +++ b/miniany/cc.c @@ -114,15 +114,17 @@ void scanIdent( int c ) int keyword( char *ident ) { - if( *ident == 'i' ) { - if( strcmp( ident, "int" ) == 0 ) { - return S_INT; - } else { - return 0; - } + switch( ident[0] ) { + case 'i': + if( strcmp( ident, "int" ) == 0 ) { + return S_INT; + } else { + return 0; + } + break; + default: + return 0; } - - return 0; } int getToken( ) @@ -132,55 +134,76 @@ int getToken( ) c = skipWhite( ); - if( c == EOF ) { - t = S_EOI; - } else if( c == '+' ) { - t = S_PLUS; - } else if( c == '-' ) { - t = S_MINUS; - } else if( c == '*' ) { - t = S_STAR; - } else if ( c == '/' ) { - c = getChar( ); - if( c == '/' ) { - while( c != '\n' ) { - c = getChar( ); - } - t = getToken( ); - } else if( c == '*' ) { - do { - while( c != '*' ) { + switch( c ) { + case EOF: + t = S_EOI; + break; + case '+': + t = S_PLUS; + break; + case '-': + t = S_MINUS; + break; + case '*': + t = S_STAR; + break; + case '/': + c = getChar( ); + if( c == '/' ) { + while( c != '\n' ) { c = getChar( ); } + t = getToken( ); + } else if( c == '*' ) { + do { + while( c != '*' ) { + c = getChar( ); + } + c = getChar( ); + } while( c != '/' ); c = getChar( ); - } while( c != '/' ); - c = getChar( ); - t = getToken( ); - } else { - pushBack( c ); - t = S_SLASH; - } - } else if( c == ';' ) { - t = S_SEMICOLON; - } else if( c == '=' ) { - t = S_EQUALS; - } else if( isdigit( c ) ) { - scanNumber( c ); - t = S_NUM; - } else if( c >= 'a' && c <= 'z' ) { - scanIdent( c ); - if( ( t = keyword( ident ) ) ) { - } else { - t = S_IDENT; - } - } else { - t = S_ERR; - printErrorHeader( ); - putstring( "unknown token '" ); - putchar( c ); - putstring( "'" ); - putnl( ); - exit( EXIT_FAILURE ); + t = getToken( ); + } else { + pushBack( c ); + t = S_SLASH; + } + break; + case ';': + t = S_SEMICOLON; + break; + case '=': + t = S_EQUALS; + break; + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + scanNumber( c ); + t = S_NUM; + break; + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'y': case 'z': + case 'A': case 'B': case 'C': case 'D': case 'E': + case 'F': case 'G': case 'H': case 'I': case 'J': + case 'K': case 'L': case 'M': case 'N': case 'O': + case 'P': case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'Y': case 'Z': + case '_': + scanIdent( c ); + if( ( t = keyword( ident ) ) ) { + } else { + t = S_IDENT; + } + break; + default: + t = S_ERR; + printErrorHeader( ); + putstring( "unknown token '" ); + putchar( c ); + putstring( "'" ); + putnl( ); + exit( EXIT_FAILURE ); } if( DEBUG_SCANNER ) { @@ -253,11 +276,13 @@ void parseExpression( ) } } -//~ struct Symbol { - //~ char *name; -//~ }; +/* +struct Symbol { + char *name; +}; -//~ struct Symbol *symbol; +struct Symbol *symbol; +*/ void newSymbol( char *s ) { @@ -298,7 +323,7 @@ int main( int argc, char **argv ) row = 1; pushback = 0; DEBUG_SCANNER = 1; - //~ symbol = NULL; + /* symbol = NULL; */ ident = "12345678901234567890"; token = getToken( ); -- cgit v1.2.3-54-g00ecf