ref: 59d88e7aa377b1cb4d333d7e13217e511978709c
parent: e0b2ed14ceb074d192ef69ef8a8b3104d424f0c1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Jun 2 04:36:49 EDT 2012
Added compound statements This implies add all loops, selections and jumps.
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
-OBJS = types.o decl.o lex.o error.o symbol.o
+OBJS = types.o decl.o lex.o error.o symbol.o flow.o main.o
LIBS =
all: kcc
--- a/decl.c
+++ b/decl.c
@@ -1,4 +1,3 @@
-#include <alloca.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
@@ -5,16 +4,11 @@
#include "cc.h"
#include "tokens.h"
-#include "symbol.h"
#include "types.h"
char parser_out_home;
-static unsigned char symhash;
-static char symname[TOKSIZ_MAX + 1];
-
-
#include <stdio.h> /* TODO: remove this */
static void declarator(void);
@@ -27,8 +21,7 @@
declarator();
expect(')'); } else if (accept(IDENTIFIER)) {- strcpy(symname, yytext);
- symhash = yyhash;
+ ;
} else { error("expected '(' or identifier before of '%s'", yytext);}
@@ -222,7 +215,7 @@
pushtype(PTR);
}
- printf("leaving dcl %c\n", yytoken);+ puts("leaving dcl");return;
duplicated:
@@ -229,34 +222,27 @@
error("duplicated '%s'", yytext);}
-void decl(void)
+char decl(void)
{struct type *t, *spec;
- spec = specifier();
+ puts("decl");+ if (!(spec = specifier()))
+ return 0;
do {declarator();
t = decl_type(spec);
} while (accept(','));- puts("leaving declaration");-}
+ expect(';'); /* TODO: initialisation */-void stmt(void)
-{- for (;;) {- decl();
- expect(';');- }
-
+ puts("leaving decl");+ return 1;
}
-int main(int argc, char *argv[])
+char decl_list(void)
{- init_lex();
-
- open_file(NULL);
- next();
- stmt();
-
- return 0;
+ puts("decl_list");+ while (decl())
+ /* nothing */;
+ puts("leaving decl_list");}
--- /dev/null
+++ b/flow.c
@@ -1,0 +1,144 @@
+
+#include <stddef.h>
+
+#include "tokens.h"
+#include "syntax.h"
+
+
+void stmt(void);
+
+void expr(void)
+{+ puts("expr");+ puts("leaving expr");+}
+
+static void do_goto(void)
+{+ puts("void do_goto");+ expect(GOTO);
+ expect(IDENTIFIER);
+ puts("leaving void do_goto");+}
+
+static void do_while(void)
+{+ puts("void do_while");+ expect(WHILE);
+ expect('(');+ expr();
+ expect(')');+ stmt();
+ puts("leaving void do_while");+}
+
+static void do_do(void)
+{+ puts("void do_do");+ expect(DO);
+ stmt();
+ expect(WHILE);
+ expect('(');+ expr();
+ expect(')');+ puts("leaving void do_do");+}
+
+static void do_for(void)
+{+ puts("void do_for");+ expect(FOR);
+ expect('(');+ if (yytoken != ';')
+ expr();
+ expect(';');+ if (yytoken != ';')
+ expr();
+ expect(';');+ if (yytoken != ')')
+ expr();
+ expect(')');+ stmt();
+ puts("leaving void do_for");+}
+
+static void do_if(void)
+{+ puts("void do_if");+ expect(IF);
+ expect('(');+ expr();
+ expect(')');+ stmt();
+ if (accept(ELSE))
+ stmt();
+
+ puts("leaving void do_if");+}
+
+static void do_switch(void)
+{+ puts("do_switch");+ expect(SWITCH);
+ expect('(');+ expr();
+ expect(')');+ stmt();
+ puts("leaving do_switch");+}
+
+void stmt(void)
+{+ puts("stmt");+ unsigned char tok;
+
+ 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 CONTINUE:
+ case BREAK:
+ case RETURN:
+ case GOTO:
+ do_goto();
+ break;
+ case CASE:
+ /* TODO */
+ case DEFAULT:
+ /* TODO */
+ break;
+ case IDENTIFIER:
+ /* TODO: check if it can be a label */
+ default:
+ expr();
+ expect(';');+ break;
+ }
+ puts("leaving stmt");+}
+
+void compound(void)
+{+ puts("compound");+ if (accept('{')) {+ decl_list();
+ while (!accept('}'))+ stmt();
+ }
+ puts("leaving compound");+}
--- /dev/null
+++ b/main.c
@@ -1,0 +1,19 @@
+
+#include <stddef.h>
+
+extern void open_file(const char *file);
+extern void init_lex();
+extern void next();
+extern void stmt();
+
+
+
+int main(int argc, char *argv[])
+{+ init_lex();
+ open_file(NULL);
+ next();
+ compound();
+
+ return 0;
+}
--- /dev/null
+++ b/syntax.h
@@ -1,0 +1,8 @@
+#ifndef SYNTAX_H
+#define SYNTAX_H
+
+extern char decl(void);
+extern void compound(void);
+extern void expr(void);
+extern char decl_list(void);
+#endif
--
⑨