diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2018-06-27 06:31:39 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2018-06-27 06:31:39 +0200 |
commit | 9cf182c016074f46f01f88de92af2a7c66e0f4c0 (patch) | |
tree | 0b8d25f18e2a9efa1e971835db48b56ac5d8126c /minic | |
parent | 3f838a994053966cdcbb1e2a32ad00801156d343 (diff) | |
download | compilertests-9cf182c016074f46f01f88de92af2a7c66e0f4c0.tar.gz compilertests-9cf182c016074f46f01f88de92af2a7c66e0f4c0.tar.bz2 |
scanning identifiers and LPAREN, some rearrangements around who reads chars when
Diffstat (limited to 'minic')
-rw-r--r-- | minic/parse.c | 18 | ||||
-rw-r--r-- | minic/scan.c | 30 | ||||
-rw-r--r-- | minic/scan.h | 3 |
3 files changed, 31 insertions, 20 deletions
diff --git a/minic/parse.c b/minic/parse.c index 8de56a6..d1464cd 100644 --- a/minic/parse.c +++ b/minic/parse.c @@ -1,6 +1,7 @@ #include "parse.h" #include "io.h" +#include "string.h" void parser_init( Parser *p, Scanner *s ) { @@ -20,9 +21,13 @@ void parser_debug( Parser *p, int enable ) } } -static void print_symbol( scanner_Symbol s ) +static void print_symbol( scanner_Symbol sym ) { - switch( s.sym ) { + char s[MAX_TMP_LEN]; + + s[0] = '\0'; + + switch( sym.sym ) { case S_eof: print( "PARSER(EOF)" ); break; @@ -33,8 +38,13 @@ static void print_symbol( scanner_Symbol s ) print( "PARSER(DIV)" ); break; case S_IDENT: - // TODO: print identifier - print( "PARSER(IDENT)" ); + strcat( s, "PARSER(IDENT," ); + strcat( s, sym.data.s ); + strcat_c( s, ')' ); + print( s ); + break; + case S_LPARENT: + print( "PARSER(LPARENT)" ); break; default: print( "PARSER(<unknown symbol>)" ); diff --git a/minic/scan.c b/minic/scan.c index 6e325bc..33803a5 100644 --- a/minic/scan.c +++ b/minic/scan.c @@ -65,12 +65,10 @@ static void skip_char( Scanner *s, int seek ) static void skip_whitespace( Scanner *s ) { for( ; ; get_char( s ) ) { - if( s->peek == ' ' || s->peek == '\t' ) { + if( s->peek == ' ' || s->peek == '\t' || s->peek == '\n' ) { continue; - } else if( s->peek == '\n' ) { - break; } else if( s->peek == '\0' ) { - break; + return; } else { break; } @@ -126,20 +124,16 @@ static scanner_Symbol parse_ident( Scanner *s ) { scanner_Symbol sym; int len = 0; - char tok[MAX_IDENT_LEN+1]; + + sym.sym = S_IDENT; + sym.data.s[0] = '\0'; - while( len < MAX_IDENT_LEN && isalnum( s->peek ) ) { - strcat_c( tok, s->peek ); + while( len < MAX_IDENT_LEN && ( isalnum( s->peek ) || s->peek == '_' ) ) { + strcat_c( sym.data.s, s->peek ); len++; get_char( s ); } - /* TODO: we can add the token into the identifier table ourselves or - * pass it to the parser and let the parser do it? - */ - - sym.sym = S_IDENT; - return sym; } @@ -149,7 +143,6 @@ scanner_Symbol scanner_scan( Scanner *s ) int c; for( ;; ) { - get_char( s ); skip_whitespace( s ); switch( s->peek ) { case '\0': @@ -171,6 +164,7 @@ scanner_Symbol scanner_scan( Scanner *s ) } continue; } else { + get_char( s ); sym.sym = S_DIV; return sym; } @@ -181,8 +175,9 @@ scanner_Symbol scanner_scan( Scanner *s ) case 'n': switch( peek_char( s, 2 ) ) { case 't': - // todo: consume two chars skip_char( s, 2 ); + get_char( s ); + get_char( s ); sym.sym = S_INT; return sym; } @@ -194,6 +189,11 @@ scanner_Symbol scanner_scan( Scanner *s ) case 'm': case 'a': case 'n': sym = parse_ident( s ); return sym; + + case '(': + get_char( s ); + sym.sym = S_LPARENT; + return sym; default: unexpected_char( s, s->peek ); diff --git a/minic/scan.h b/minic/scan.h index be24e08..1a7cc79 100644 --- a/minic/scan.h +++ b/minic/scan.h @@ -7,7 +7,8 @@ typedef enum scanner_Sym { S_eof, S_DIV, S_INT, - S_IDENT + S_IDENT, + S_LPARENT } scanner_Sym; typedef struct scanner_Symbol { |