Add newline as token type and add skeleton for parser
This commit is contained in:
parent
29166c8bff
commit
21444c2654
3
Makefile
3
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)
|
||||
|
3
main.c
3
main.c
@ -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
18
parse.c
Normal 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
25
parse.h
Normal 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);
|
6
token.c
6
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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user