From 21444c265493484b5ee9867298dd5d50d8f587be Mon Sep 17 00:00:00 2001 From: matt Date: Sat, 2 Apr 2022 18:10:01 +0800 Subject: [PATCH] Add newline as token type and add skeleton for parser --- Makefile | 3 ++- main.c | 3 +++ parse.c | 18 ++++++++++++++++++ parse.h | 25 +++++++++++++++++++++++++ token.c | 6 +++++- token.h | 2 +- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 parse.c create mode 100644 parse.h diff --git a/Makefile b/Makefile index 0c0365c..22e6b30 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = tcc CFLAGS = -Wall -Wunsupported -Wwrite-strings -b -g -OBJ = main.o token.o +OBJ = main.o token.o parse.o all: woody @@ -11,6 +11,7 @@ woody: $(OBJ) main.o: main.c token.o token.o: token.c token.h +parse.o: parse.c parse.h token.o clean: rm -f woody $(OBJ) diff --git a/main.c b/main.c index dd05c11..b5bf752 100644 --- a/main.c +++ b/main.c @@ -52,6 +52,9 @@ test(void) case OP: printf("val: %c\n", token->val.op); break; + case NEWLINE: + printf("val: \n"); + break; } } } diff --git a/parse.c b/parse.c new file mode 100644 index 0000000..4e5462d --- /dev/null +++ b/parse.c @@ -0,0 +1,18 @@ +#include +#include +#include "token.h" +#include "parse.h" + +/** + * Takes the tokenized program and parses them into an array of trees. + */ +int +parseall(struct parsestate *ps) +{ + struct parsenode *this; + + while (1) { + } + + return 0; +} diff --git a/parse.h b/parse.h new file mode 100644 index 0000000..33b4128 --- /dev/null +++ b/parse.h @@ -0,0 +1,25 @@ +enum nodetype { + NODEIF, NODEELSE, NODEELSIF, NODEWHILE, NODEOP, NODENUM, + NODEVAR, +}; + +struct parsenode { + union { + const char *var; + char op; + } val; + const char *filename; + struct parsenode *left, *right; + int col, line, pos; + enum nodetype type; +}; + +struct parsestate { + struct tokenstate ts; + struct parsenode *program; + /* progsz is the total size of program */ + /* progind is the index (where to put the next tree) */ + size_t progsz, progind; +}; + +extern int parseall(struct parsestate *ps); diff --git a/token.c b/token.c index 1f51f3f..860e7c6 100644 --- a/token.c +++ b/token.c @@ -41,7 +41,7 @@ getch(struct tokenstate *ts) static inline void skip_whitespace(struct tokenstate *ts) { - while (isspace(ts->look)) + while (isspace(ts->look) && ts->look != '\n') getch(ts); } @@ -268,6 +268,10 @@ tokenize(struct tokenstate *ts) tokenizevarkw(token, ts); else if (ts->look == '"') tokenizestring(token, ts); + else if (ts->look == '\n') { + token->type = NEWLINE; + getch(ts); + } else { /* something else, probably an operator */ /* don't bother to check here, it's not worth it */ diff --git a/token.h b/token.h index 1ebe3d7..937b88f 100644 --- a/token.h +++ b/token.h @@ -1,5 +1,5 @@ /* please also see token.c */ -enum tokentype { KW, STRING, VAR, NUM, OP }; +enum tokentype { KW, STRING, VAR, NUM, OP, NEWLINE }; enum kwtype { IF, THEN, ELSIF, ELSE, WHILE, ENDKWTYPE }; struct token {