scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit eab03ebe04b15733d202211eee7b995f8dd6b39c
parent b670dbdea0cbe8f952c8b9aa134fd8ac6f34e271
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 16 Aug 2012 19:27:35 +0200

Added suppor for printing tree in compound nodes

This patch enables printing nodes of compound type. This state is temporal
because almost node types return NULL, which can not be used.

Diffstat:
flow.c | 68+++++++++++++++++++++++++++++++++-----------------------------------
syntax.h | 2+-
2 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/flow.c b/flow.c @@ -5,16 +5,17 @@ #include "tokens.h" #include "syntax.h" -void stmt(void); +static struct node *stmt(void); -static void +static struct node * do_goto(void) { expect(GOTO); expect(IDEN); + return NULL; } -static void +static struct node * do_while(void) { expect(WHILE); @@ -22,9 +23,10 @@ do_while(void) expr(); expect(')'); stmt(); + return NULL; } -static void +static struct node * do_do(void) { expect(DO); @@ -33,9 +35,10 @@ do_do(void) expect('('); expr(); expect(')'); + return NULL; } -static void +static struct node * do_for(void) { expect(FOR); @@ -50,9 +53,10 @@ do_for(void) expr(); expect(')'); stmt(); + return NULL; } -static void +static struct node * do_if(void) { expect(IF); @@ -62,10 +66,11 @@ do_if(void) stmt(); if (accept(ELSE)) stmt(); + return NULL; } -static void +static struct node * do_switch(void) { expect(SWITCH); @@ -73,37 +78,25 @@ do_switch(void) expr(); expect(')'); stmt(); + return NULL; } -void +static struct node * stmt(void) { + register struct node *np; switch (yytoken) { - case '{': - compound(); - break; - case SWITCH: - do_switch(); - break; - case IF: - do_if(); - break; - case FOR: - do_for(); - break; - case DO: - do_do(); - break; - case WHILE: - do_while(); - break; + case '{': np = compound(); break; + case SWITCH: np = do_switch(); break; + case IF: np = do_if(); break; + case FOR: np = do_for(); break; + case DO: np = do_do(); break; + case WHILE: np = do_while(); break; case CONTINUE: case BREAK: case RETURN: - case GOTO: - do_goto(); - break; + case GOTO: np = do_goto(); break; case CASE: /* TODO */ case DEFAULT: @@ -111,22 +104,27 @@ stmt(void) break; case IDEN: /* TODO: check if it can be a label */ - default: - prtree(expr()); - putchar('\n'); - break; + default: np = expr(); break; + } expect(';'); + return np; } -void +struct node * compound(void) { + register struct node *np = nodecomp(); + expect('{'); new_ctx(); while (decl()) /* nothing */; while (!accept('}')) - stmt(); + addstmt(np, stmt()); del_ctx(); + + prtree(np); + putchar('\n'); + return np; } diff --git a/syntax.h b/syntax.h @@ -16,7 +16,7 @@ enum { struct node; struct symbol; -extern void compound(void); +extern struct node *compound(void); extern struct node *expr(void); extern unsigned char decl(void); extern void type_name(void);