// Copyright (c) 2024, Marvin Borner // SPDX-License-Identifier: MIT #include #include static void encode(Term *term, FILE *fp) { switch (term->type) { case ABS: fprintf(fp, "01"); encode(term->u.abs.body, fp); break; case APP: fprintf(fp, "0"); Term *stack[128] = { 0 }; // TODO: HEAP! int stacked = 0; while (1) { fprintf(fp, "0"); stack[stacked++] = term->u.app.lhs; term = term->u.app.rhs; if (term->type != APP) break; } fprintf(fp, "1"); for (int i = 0; i < stacked; i++) encode(stack[i], fp); encode(term, fp); break; case IDX: for (size_t i = 0; i <= term->u.index; i++) fprintf(fp, "1"); fprintf(fp, "0"); break; default: fatal("invalid type %d\n", term->type); } } static Term *decode(FILE *fp) { Term *res = 0; char a = getc(fp); if (a == '0') { size_t count = 0; while (getc(fp) == '0') count++; if (count == 0) { res = term_new(ABS); res->u.abs.body = decode(fp); } else { Term *head = term_new(APP); res = head; for (size_t i = 0; i < count - 1; i++) { res->u.app.lhs = decode(fp); res->u.app.rhs = term_new(APP); res = res->u.app.rhs; } res->u.app.lhs = decode(fp); res->u.app.rhs = decode(fp); res = head; } } else 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; } Impl impl_app_right(void) { return (Impl){ .name = "app_right", .encode = encode, .decode = decode, }; }