From 05f3cde3e7924c9ffcc1937661b3cc290d89c11a Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sat, 7 Sep 2024 17:53:27 +0200 Subject: Initial bootstrap --- src/parse.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/parse.c (limited to 'src/parse.c') diff --git a/src/parse.c b/src/parse.c new file mode 100644 index 0000000..f2f89cd --- /dev/null +++ b/src/parse.c @@ -0,0 +1,66 @@ +// Copyright (c) 2024, Marvin Borner +// SPDX-License-Identifier: MIT + +#include +#include +#include + +Term *parse_blc_fp(FILE *fp) +{ + Term *res = 0; + + char a = getc(fp); + + if (a == '0') { + char b = getc(fp); + if (b == '0') { + res = term_new(ABS); + res->u.abs.body = parse_blc_fp(fp); + } + if (b == '1') { + res = term_new(APP); + res->u.app.lhs = parse_blc_fp(fp); + res->u.app.rhs = parse_blc_fp(fp); + } + } + + if (a == '1') { + res = term_new(IDX); + res->u.index = 0; + while (getc(fp) == '1') + res->u.index++; + } + + if (!res) + fatal("invalid parsing state!\n"); + + return res; +} + +Term *parse_blc(const char **term) +{ + Term *res = 0; + if (!**term) { + fatal("invalid parsing state!\n"); + } else if (**term == '0' && *(*term + 1) == '0') { + (*term) += 2; + res = term_new(ABS); + res->u.abs.body = parse_blc(term); + } else if (**term == '0' && *(*term + 1) == '1') { + (*term) += 2; + res = term_new(APP); + res->u.app.lhs = parse_blc(term); + res->u.app.rhs = parse_blc(term); + } else if (**term == '1') { + const char *cur = *term; + while (**term == '1') + (*term)++; + res = term_new(IDX); + res->u.index = *term - cur - 1; + (*term)++; + } else { + (*term)++; + res = parse_blc(term); + } + return res; +} -- cgit v1.2.3