Add newline as token type and add skeleton for parser

This commit is contained in:
matt 2022-04-02 18:10:01 +08:00
parent 29166c8bff
commit 21444c2654
6 changed files with 54 additions and 3 deletions

View File

@ -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)

3
main.c
View File

@ -52,6 +52,9 @@ test(void)
case OP:
printf("val: %c\n", token->val.op);
break;
case NEWLINE:
printf("val: <newline>\n");
break;
}
}
}

18
parse.c Normal file
View File

@ -0,0 +1,18 @@
#include <inttypes.h>
#include <stdio.h>
#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;
}

25
parse.h Normal file
View File

@ -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);

View File

@ -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 */

View File

@ -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 {