commit 59d88e7aa377b1cb4d333d7e13217e511978709c
parent e0b2ed14ceb074d192ef69ef8a8b3104d424f0c1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 2 Jun 2012 08:36:49 +0200
Added compound statements
This implies add all loops, selections and jumps.
Diffstat:
M | Makefile | | | 2 | +- |
M | decl.c | | | 42 | ++++++++++++++---------------------------- |
A | flow.c | | | 144 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | main.c | | | 19 | +++++++++++++++++++ |
A | syntax.h | | | 8 | ++++++++ |
5 files changed, 186 insertions(+), 29 deletions(-)
diff --git 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
diff --git a/decl.c b/decl.c
@@ -1,20 +1,14 @@
-#include <alloca.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
#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 @@ static void dirdcl(void)
declarator();
expect(')');
} else if (accept(IDENTIFIER)) {
- strcpy(symname, yytext);
- symhash = yyhash;
+ ;
} else {
error("expected '(' or identifier before of '%s'", yytext);
}
@@ -222,41 +215,34 @@ static void declarator(void)
pushtype(PTR);
}
- printf("leaving dcl %c\n", yytoken);
+ puts("leaving dcl");
return;
duplicated:
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");
-}
-
-void stmt(void)
-{
- for (;;) {
- decl();
- expect(';');
- }
+ expect(';'); /* TODO: initialisation */
+ 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");
}
diff --git a/flow.c b/flow.c
@@ -0,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");
+}
diff --git a/main.c b/main.c
@@ -0,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;
+}
diff --git a/syntax.h b/syntax.h
@@ -0,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