Read-Back Extension
Jul. 28th, 2014 04:35 pmDecoding extension for Implementation of Closed Reduction using Interaction Nets Compiler:
${ #include <stdio.h> #include <stdlib.h> #include <string.h> char *var(int fresh); char *append(char *format, char *buf, char *str); #define ABST(BUF, STR) append("%s%s: ", (BUF), (STR)) #define APPL(BUF, STR) append("%s%s ", (BUF), (STR)) #define ATOM(BUF, STR) append("%s(%s)", (BUF), (STR)) }$ \print { /* Output results of read-back. */ puts(RVAL); exit(EXIT_SUCCESS); } \atom; \read[a] { /* Read back abstraction. */ } \lambda[\atom_{var(1)}, \read_{ABST(LVAL, var(0))}(a)]; \lambda[\read_{APPL(strdup(""), RVAL)}(a), a] { /* Read back application. */ } \atom; \read[\atom_{ATOM(LVAL, RVAL)}] { /* Read back an atom. */ } \atom; \bind[a, \atom_{RVAL}, a] { /* Bind variable to an atom. */ } \atom; \copy[\atom_{RVAL}, \atom_{strdup(RVAL)}] { /* Copy an atom. */ } \atom; \dup[\atom_{RVAL}, \atom_{strdup(RVAL)}] { /* Duplicate an atom. */ } \atom; \erase { /* Erase an atom. */ free(RVAL); } \atom; $$ term = \read_{strdup("")}(\print); term = {"Encoding"}; $$ char *var(int fresh) { static int id; char buf[BUFSIZ]; if (fresh) ++id; sprintf(buf, "v%d", id); return strdup(buf); } char *append(char *format, char *buf, char *str) { size_t size = strlen(format) + strlen(str); char *result = malloc(strlen(buf) + size); sprintf(result, format, buf, str); free(buf); free(str); return result; }