scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit d3960dec0c54014b6a7072a7646d35ee7cb86533
parent 6981fdf248be1f4a15a9b8fd0296111fba9ae2dd
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 17 Jul 2014 22:42:37 +0200

Parse function headers in cc2

This commits only parses the name of the function and ignores
all the remaining characters.

Diffstat:
Mcc2/parser.c | 51++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 46 insertions(+), 5 deletions(-)

diff --git a/cc2/parser.c b/cc2/parser.c @@ -5,6 +5,9 @@ #include <cc.h> #include <sizes.h> +#define STR(x) XSTR(x) +#define XSTR(x) #x + #define NR_STACKSIZ 32 #define NR_NODEPOOL 128 #define NR_EXPRESSIONS 64 @@ -196,20 +199,33 @@ declaration(char sclass) } static void +chop(void) +{ + int c; + + while ((c = getchar()) != EOF && c != '\n') + /* nothing */; +} + +static void deflabel(void) { Symbol *sym = &symtbl[getid()]; sym->u.l.addr = listp - listexp; + chop(); } -int -parse(void) +static void +function(void) { int c; + char name[IDENTSIZ + 1]; - while ((c = getchar()) != EOF) { - switch (c) { + scanf("%" STR(IDENTSIZ) "s", name); + chop(); + for (;;) { + switch (c = getchar()) { case '\t': expression(); break; @@ -219,9 +235,12 @@ parse(void) case 'S': /* struct */ break; - case 'T': case 'A': case 'G': case 'R': + case 'T': case 'A': case 'R': declaration(c); break; + case '}': + chop(); + return; default: esyntax(); break; @@ -229,3 +248,25 @@ parse(void) } } +int +parse(void) +{ + int c; + + for (;;) { + switch (c = getchar()) { + case 'G': + declaration(c); + break; + case 'X': + function(); + break; + case EOF: + return; + break; + default: + esyntax(); + } + } +} +