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
|
CC = tcc
|
||||||
CFLAGS = -Wall -Wunsupported -Wwrite-strings -b -g
|
CFLAGS = -Wall -Wunsupported -Wwrite-strings -b -g
|
||||||
OBJ = main.o token.o
|
OBJ = main.o token.o parse.o
|
||||||
|
|
||||||
all: woody
|
all: woody
|
||||||
|
|
||||||
@ -11,6 +11,7 @@ woody: $(OBJ)
|
|||||||
|
|
||||||
main.o: main.c token.o
|
main.o: main.c token.o
|
||||||
token.o: token.c token.h
|
token.o: token.c token.h
|
||||||
|
parse.o: parse.c parse.h token.o
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f woody $(OBJ)
|
rm -f woody $(OBJ)
|
||||||
|
3
main.c
3
main.c
@ -52,6 +52,9 @@ test(void)
|
|||||||
case OP:
|
case OP:
|
||||||
printf("val: %c\n", token->val.op);
|
printf("val: %c\n", token->val.op);
|
||||||
break;
|
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
|
static inline void
|
||||||
skip_whitespace(struct tokenstate *ts)
|
skip_whitespace(struct tokenstate *ts)
|
||||||
{
|
{
|
||||||
while (isspace(ts->look))
|
while (isspace(ts->look) && ts->look != '\n')
|
||||||
getch(ts);
|
getch(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,6 +268,10 @@ tokenize(struct tokenstate *ts)
|
|||||||
tokenizevarkw(token, ts);
|
tokenizevarkw(token, ts);
|
||||||
else if (ts->look == '"')
|
else if (ts->look == '"')
|
||||||
tokenizestring(token, ts);
|
tokenizestring(token, ts);
|
||||||
|
else if (ts->look == '\n') {
|
||||||
|
token->type = NEWLINE;
|
||||||
|
getch(ts);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
/* something else, probably an operator */
|
/* something else, probably an operator */
|
||||||
/* don't bother to check here, it's not worth it */
|
/* don't bother to check here, it's not worth it */
|
||||||
|
2
token.h
2
token.h
@ -1,5 +1,5 @@
|
|||||||
/* please also see token.c */
|
/* 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 };
|
enum kwtype { IF, THEN, ELSIF, ELSE, WHILE, ENDKWTYPE };
|
||||||
|
|
||||||
struct token {
|
struct token {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user