aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c1
-rw-r--r--src/parse.c36
-rw-r--r--src/reducer.c6
-rw-r--r--src/term.c4
4 files changed, 43 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 024cc7a..7ff90d2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,6 +7,7 @@ int main(void)
{
struct term *term = parse("([[((0 1) [(1 0)])]] [0])");
print_term(term);
+ printf("\n");
free_term(term);
return 0;
}
diff --git a/src/parse.c b/src/parse.c
index 495f55f..306bf4f 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -6,6 +6,12 @@
#include <parse.h>
#include <term.h>
+static int name_generator(void)
+{
+ static int current = 0x424242; // TODO: idk?
+ return current++;
+}
+
static struct term *rec(const char **term)
{
struct term *res = 0;
@@ -22,7 +28,8 @@ static struct term *rec(const char **term)
res->u.app.rhs = rec(term);
} else if (**term >= '0' && **term <= '9') {
res = new_term(VAR);
- res->u.var = **term - '0';
+ res->u.var.name = **term - '0';
+ res->u.var.type = BRUIJN_INDEX;
(*term)++;
} else {
(*term)++;
@@ -31,7 +38,32 @@ static struct term *rec(const char **term)
return res;
}
+// TODO: WARNING: This might not be 100% correct! Verify!
+static void to_barendregt(struct term *term, int level, int replacement)
+{
+ switch (term->type) {
+ case ABS:
+ term->u.abs.name = name_generator();
+ to_barendregt(term->u.abs.term, level + 1, term->u.abs.name);
+ break;
+ case APP:
+ to_barendregt(term->u.app.lhs, level, replacement);
+ to_barendregt(term->u.app.rhs, level, replacement);
+ break;
+ case VAR:
+ if (term->u.var.type == BRUIJN_INDEX) {
+ term->u.var.name = replacement - term->u.var.name;
+ term->u.var.type = BARENDREGT_VARIABLE;
+ }
+ break;
+ default:
+ fprintf(stderr, "Invalid type %d\n", term->type);
+ }
+}
+
struct term *parse(const char *term)
{
- return rec(&term);
+ struct term *parsed = rec(&term);
+ to_barendregt(parsed, -1, -1);
+ return parsed;
}
diff --git a/src/reducer.c b/src/reducer.c
new file mode 100644
index 0000000..e74946b
--- /dev/null
+++ b/src/reducer.c
@@ -0,0 +1,6 @@
+#include <reducer.h>
+
+struct term *reduce(struct term *term)
+{
+ return term;
+}
diff --git a/src/term.c b/src/term.c
index 746c6f7..d13ec9f 100644
--- a/src/term.c
+++ b/src/term.c
@@ -38,7 +38,7 @@ void print_term(struct term *term)
{
switch (term->type) {
case ABS:
- printf("[");
+ printf("[{%d} ", term->u.abs.name);
print_term(term->u.abs.term);
printf("]");
break;
@@ -50,7 +50,7 @@ void print_term(struct term *term)
printf(")");
break;
case VAR:
- printf("%d", term->u.var);
+ printf("%d", term->u.var.name);
break;
default:
fprintf(stderr, "Invalid type %d\n", term->type);