diff options
author | Marvin Borner | 2022-12-13 15:43:39 +0100 |
---|---|---|
committer | Marvin Borner | 2022-12-13 15:44:59 +0100 |
commit | 2eecfca85b2b703318e57fcce33c864757b79c00 (patch) | |
tree | 5386231745ddb9b5d24281a889ef28dfa84f0b29 /src/parse.c |
Basic parsing
Diffstat (limited to 'src/parse.c')
-rw-r--r-- | src/parse.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..00fd498 --- /dev/null +++ b/src/parse.c @@ -0,0 +1,36 @@ +// Just for debugging purposes +// -> parses custom bruijn syntax + +#include <parse.h> +#include <term.h> +#include <stdio.h> + +static struct term *rec(const char **term) +{ + struct term *res = 0; + if (!**term) { + fprintf(stderr, "invalid parsing state!\n"); + } else if (**term == '[') { + (*term)++; + res = new_term(ABS); + res->u.abs.term = rec(term); + } else if (**term == '(') { + (*term)++; + res = new_term(APP); + res->u.app.lhs = rec(term); + res->u.app.rhs = rec(term); + } else if (**term >= '0' && **term <= '9') { + res = new_term(VAR); + res->u.var = **term - '0'; + (*term)++; + } else { + (*term)++; + res = rec(term); // this is quite tolerant.. + } + return res; +} + +struct term *parse(const char *term) +{ + return rec(&term); +} |