aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
diff options
context:
space:
mode:
authorMarvin Borner2023-02-20 20:32:45 +0100
committerMarvin Borner2023-02-20 20:32:45 +0100
commitd0b4cf4497d9dfdb0a5f5f973af25008f8b5c65f (patch)
treeba8ef2c7a8229cdf7fccde7d5b03fdac7f5d4b3b /src/parse.c
parent9906ba3585ce74c10d2fe9d6a991d6ea0ab99114 (diff)
Added BLC parsing/printing
Diffstat (limited to 'src/parse.c')
-rw-r--r--src/parse.c52
1 files changed, 43 insertions, 9 deletions
diff --git a/src/parse.c b/src/parse.c
index 33ca556..0b9fd82 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -1,13 +1,11 @@
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
-// Just for testing purposes
-// -> parses custom bruijn syntax
#include <stdio.h>
#include <parse.h>
#include <term.h>
-static struct term *rec(const char **term)
+static struct term *rec_bruijn(const char **term)
{
struct term *res = 0;
if (!**term) {
@@ -15,12 +13,12 @@ static struct term *rec(const char **term)
} else if (**term == '[') {
(*term)++;
res = new_term(ABS);
- res->u.abs.term = rec(term);
+ res->u.abs.term = rec_bruijn(term);
} else if (**term == '(') {
(*term)++;
res = new_term(APP);
- res->u.app.lhs = rec(term);
- res->u.app.rhs = rec(term);
+ res->u.app.lhs = rec_bruijn(term);
+ res->u.app.rhs = rec_bruijn(term);
} else if (**term >= '0' && **term <= '9') {
res = new_term(VAR);
res->u.var.name = **term - '0';
@@ -28,14 +26,50 @@ static struct term *rec(const char **term)
(*term)++;
} else {
(*term)++;
- res = rec(term); // this is quite tolerant..
+ res = rec_bruijn(term); // this is quite tolerant..
}
return res;
}
-struct term *parse(const char *term)
+static struct term *rec_blc(const char **term)
{
- struct term *parsed = rec(&term);
+ struct term *res = 0;
+ if (!**term) {
+ fprintf(stderr, "invalid parsing state!\n");
+ } else if (**term == '0' && *(*term + 1) == '0') {
+ (*term) += 2;
+ res = new_term(ABS);
+ res->u.abs.term = rec_blc(term);
+ } else if (**term == '0' && *(*term + 1) == '1') {
+ (*term) += 2;
+ res = new_term(APP);
+ res->u.app.lhs = rec_blc(term);
+ res->u.app.rhs = rec_blc(term);
+ } else if (**term == '1') {
+ const char *cur = *term;
+ while (**term == '1')
+ (*term)++;
+ res = new_term(VAR);
+ res->u.var.name = *term - cur - 1;
+ res->u.var.type = BRUIJN_INDEX;
+ (*term)++;
+ } else {
+ (*term)++;
+ res = rec_blc(term); // this is quite tolerant..
+ }
+ return res;
+}
+
+struct term *parse_bruijn(const char *term)
+{
+ struct term *parsed = rec_bruijn(&term);
+ to_barendregt(parsed);
+ return parsed;
+}
+
+struct term *parse_blc(const char *term)
+{
+ struct term *parsed = rec_blc(&term);
to_barendregt(parsed);
return parsed;
}