scc

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

commit 57380d876cf8ee79ba67e06dca722cc54189ac27
parent b6b38357b4e631ba81d71e372dadc5e9511f4f78
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 19 Sep 2014 07:48:02 +0200

Add apply() function

apply() applies a function to a list of nodes (usually each node is
the root of a tree). This function is going to be called in almost
of the passes.

Diffstat:
cc2/cc2.h | 3++-
cc2/cgen.c | 22+++++-----------------
cc2/parser.c | 11++++++++++-
3 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -95,6 +95,7 @@ enum nerrors { #define ADDABLE 10 extern void error(unsigned nerror, ...); -extern void genaddable(Node *list[]); +extern void genaddable(Node *np); extern void generate(Symbol *sym, Node *list[]); extern void genstack(Symbol *fun); +extern void apply(Node *list[], void (*fun)(Node *)); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -240,7 +240,6 @@ void generate(Symbol *sym, Node *list[]) { extern char odebug; - Node *np; char frame = sym->u.f.locals != 0 || odebug; emit(ADDR, sym->name); @@ -252,8 +251,7 @@ generate(Symbol *sym, Node *list[]) emit(LD, SP, HL); } - while (np = *list++) - cgen(np); + apply(list, cgen); if (frame) { emit(LD, SP, IX); @@ -269,8 +267,8 @@ generate(Symbol *sym, Node *list[]) * STATIC => 12 (value) * CONST => 20 $value */ -static void -xaddable(Node *np) +void +genaddable(Node *np) { Node *lp, *rp; @@ -296,9 +294,9 @@ xaddable(Node *np) break; default: if (lp) - xaddable(lp); + genaddable(lp); if (rp) - xaddable(rp); + genaddable(rp); break; } @@ -318,13 +316,3 @@ xaddable(Node *np) ++np->complex; return; } - -void -genaddable(Node *list[]) -{ - Node *np; - - while (np = *list++) - xaddable(np); -} - diff --git a/cc2/parser.c b/cc2/parser.c @@ -77,6 +77,15 @@ Type l_uint64 = { .c_int = 1, }; +void +apply(Node *list[], void (*fun)(Node *)) +{ + Node *np; + + while (np = *list++) + (*fun)(np); +} + static Symbol * parameter(char *num) { @@ -396,7 +405,7 @@ endfunction(char *token) if (!curfun) error(ESYNTAX); listp = NULL; - genaddable(listexp); + apply(listexp, genaddable); generate(curfun, listexp); curfun = NULL; }