Add SectorLambda

This commit is contained in:
Justine Tunney 2022-03-17 14:12:41 -07:00
parent 741c836e9d
commit f5831a62fa
21 changed files with 3275 additions and 0 deletions

65
tool/lambda/lib/blc.h Normal file
View file

@ -0,0 +1,65 @@
#ifndef COSMOPOLITAN_TOOL_LAMBDA_LIB_BLC_H_
#define COSMOPOLITAN_TOOL_LAMBDA_LIB_BLC_H_
#include "libc/stdio/stdio.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define BUILTINS 4
#define LOC 12
#define TERMS 5000000
#define IOP 0 // code for gro, wr0, wr1, put
#define VAR 1 // code for variable lookup
#define APP 2 // code for applications
#define ABS 3 // code for abstractions
struct Parse {
int n;
int i;
};
struct Closure {
struct Closure *next;
struct Closure *envp;
int refs;
int term;
};
extern char vlog;
extern char slog;
extern char alog;
extern char rlog;
extern char safer;
extern char style;
extern char binary;
extern char noname;
extern char asciiname;
extern int ci;
extern int co;
extern long ip;
extern long end;
extern int heap;
extern FILE *logh;
extern int mem[TERMS];
extern struct Closure root;
extern struct Closure *envp;
extern struct Closure *frep;
extern struct Closure *contp;
char GetBit(FILE *);
char NeedBit(FILE *);
struct Parse Parse(int, FILE *);
void Dump(int, int, FILE *);
void Error(int, const char *, ...);
void PrintLambda(int, int, int, int, FILE *);
void PrintBinary(int, int, int, FILE *);
void PrintDebruijn(int, int, int, FILE *);
void PrintMachineState(FILE *);
void PrintExpressions(FILE *, char, char);
void Print(int, int, int, FILE *);
void PrintVar(int, FILE *);
void *Calloc(size_t, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TOOL_LAMBDA_LIB_BLC_H_ */

43
tool/lambda/lib/calloc.c Normal file
View file

@ -0,0 +1,43 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
void *Calloc(size_t a, size_t b) {
char *r;
size_t z;
static char *p;
static size_t i;
static size_t n;
z = a * b;
if (!p) {
n = FRAMESIZE;
p = mmap((void *)0x300000000000, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
}
if (i + z > n) {
mmap(p + i, FRAMESIZE, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
n += FRAMESIZE;
}
r = p + i;
i += z;
return r;
}

154
tool/lambda/lib/debug.c Normal file
View file

@ -0,0 +1,154 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "tool/lambda/lib/blc.h"
const char *GetOpName(int x) {
switch (x) {
case VAR:
return "var";
case APP:
return "app";
case ABS:
return "abs";
case IOP:
return "iop";
default:
return "wut";
}
}
int GetDepth(struct Closure *env) {
int i;
for (i = 0; env && env != &root; ++i) {
env = env->next;
}
return i;
}
void PrintClosure(struct Closure *c, const char *name, int indent, FILE *f) {
int i, j;
char ibuf[21];
while (c && c != &root) {
for (j = 0; j < indent; ++j) {
if (j) {
fputs("", f);
} else {
fputs(" ", f);
}
}
fputs(name, f);
fputs(": ", f);
Print(c->term, 0, GetDepth(c->envp), f);
fputs(" +", f);
int64toarray_radix10(c->refs, ibuf);
fputs(ibuf, f);
fputc('\n', f);
PrintClosure(c->envp, "envp", indent + 1, f);
c = c->next;
}
}
void PrintMachineState(FILE *f) {
int i;
char buf[256];
static int op;
struct Closure *c;
fputc('\n', f);
for (i = 0; i < 80; ++i) fputwc(L'', f);
ksnprintf(buf, sizeof(buf),
"%d\n ip %ld | op %d %s | arg %d | end %ld\n", op++, ip,
mem[ip], GetOpName(mem[ip]), mem[ip + 1], end);
fputs(buf, f);
fputs(" term ", f);
Print(ip, 0, GetDepth(envp), f);
fputc('\n', f);
fputc('\n', f);
PrintClosure(contp, "contp", 1, f);
fputc('\n', f);
PrintClosure(envp, "envp", 1, f);
fputc('\n', f);
PrintClosure(frep, "frep", 1, f);
}
void PrintExpressions(FILE *f, char alog, char vlog) {
int i, d;
char buf[48];
struct Closure *p, ps;
ps.term = ip;
ps.next = contp;
ps.envp = envp;
for (p = &ps; p; p = p->next) {
Print(p->term, 1, GetDepth(p->envp), f);
if (p->next) fputc(' ', f);
}
if (alog) {
fputs("", f);
switch (mem[ip]) {
case VAR:
ksnprintf(buf, sizeof(buf), "var[%d]", mem[ip + 1]);
fputs(buf, f);
break;
case APP:
fputs("app[", f);
Print(ip + 2 + mem[ip + 1], 1, GetDepth(envp), f);
fputc(']', f);
break;
case ABS:
if (contp) {
fputs("abs[", f);
Print(ip + 1, 1, GetDepth(envp), f);
fputc(']', f);
} else {
ksnprintf(buf, sizeof(buf), "bye[%d]", mem[ip + 2]);
fputs(buf, f);
}
break;
case IOP:
if (ip < 22) {
if (!binary) {
ksnprintf(buf, sizeof(buf), "put[%c]", '0' + (int)(ip & 1));
} else if (mem[ip + 1] & 1) {
ksnprintf(buf, sizeof(buf), "put[0%hho '%c']", co,
isprint(co) ? co : '.');
} else {
ksnprintf(buf, sizeof(buf), "wr%d[0%hho]", (int)(ip & 1), co);
}
fputs(buf, f);
} else {
fputs("gro", f);
}
break;
default:
break;
}
}
if (vlog) {
d = GetDepth(envp);
for (i = 0, p = envp; p->term != -1; ++i, p = p->next) {
fputc('\n', f);
fputc('\t', f);
PrintVar(style != 1 ? i : d - 1 - i, f);
fputc('=', f);
Print(p->term, 0, GetDepth(p), f);
}
}
fputc('\n', f);
}

63
tool/lambda/lib/dump.c Normal file
View file

@ -0,0 +1,63 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/kprintf.h"
#include "tool/lambda/lib/blc.h"
void Dumper(int i, int j, FILE *f) {
char buf[64];
if (i) fputc('\n', f);
for (; i < j; ++i) {
switch (mem[i]) {
case VAR:
ksnprintf(buf, sizeof(buf), " %s,%d,\t// %2d: ", "VAR", mem[i + 1],
i);
fputs(buf, f);
Print(i, 1, 0, f);
fputc('\n', f);
++i;
break;
case APP:
ksnprintf(buf, sizeof(buf), " %s,%d,\t// %2d: ", "APP", mem[i + 1],
i);
fputs(buf, f);
Print(i, 1, 0, f);
fputc('\n', f);
++i;
break;
case ABS:
ksnprintf(buf, sizeof(buf), " %s,\t// %2d: ", "ABS", i);
fputs(buf, f);
Print(i, 1, 0, f);
fputc('\n', f);
break;
default:
ksnprintf(buf, sizeof(buf), " %d,\t// %2d: ", mem[i], i);
fputs(buf, f);
Print(i, 1, 0, f);
fputc('\n', f);
break;
}
}
}
void Dump(int i, int j, FILE *f) {
fputs("\nstatic int kTerm[] = {\n", f);
Dumper(i, j, f);
fputs("};\n", f);
}

37
tool/lambda/lib/error.c Normal file
View file

@ -0,0 +1,37 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/kprintf.h"
#include "tool/lambda/lib/blc.h"
void Error(int rc, const char* s, ...) {
va_list va;
fflush(stdout);
fputs("\n\33[1;31mERROR\33[37m:\t", stderr);
fflush(stderr);
va_start(va, s);
kvprintf(s, va);
va_end(va);
fputs("\33[0m\n", stderr);
kprintf(" ip:\t%ld\n", ip);
kprintf(" end:\t%ld\n", end);
kprintf(" term:\t");
PrintExpressions(stderr, 0, 1);
/* Dump(0, end, stderr); */
exit(rc);
}

54
tool/lambda/lib/getbit.c Normal file
View file

@ -0,0 +1,54 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/lambda/lib/blc.h"
char GetBit(FILE* f) {
wint_t c;
char comment;
static wint_t buf, mask;
if (!binary) {
for (comment = 0;;) {
c = fgetwc(f);
if (c == -1) break;
if (!comment) {
fflush(stdout);
if (c == ';') {
comment = 1;
} else if (!iswspace(c) && c != '(' && c != ')' && c != '[' &&
c != ']') {
if (c != -1) c &= 1;
break;
}
} else if (c == '\n') {
comment = 0;
}
}
} else if (mask) {
c = !!(buf & mask);
mask >>= 1;
} else {
c = fgetc(f);
if (c != -1) {
buf = c;
c = (c >> 7) & 1;
mask = 64;
}
}
return c;
}

65
tool/lambda/lib/lib.mk Normal file
View file

@ -0,0 +1,65 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += TOOL_LAMBDA_LIB
TOOL_LAMBDA_LIB_ARTIFACTS += TOOL_LAMBDA_LIB_A
TOOL_LAMBDA_LIB = $(TOOL_LAMBDA_LIB_A_DEPS) $(TOOL_LAMBDA_LIB_A)
TOOL_LAMBDA_LIB_A = o/$(MODE)/tool/lambda/lib/lambdalib.a
TOOL_LAMBDA_LIB_A_FILES := $(filter-out %/.%,$(wildcard tool/lambda/lib/*))
TOOL_LAMBDA_LIB_A_HDRS = $(filter %.h,$(TOOL_LAMBDA_LIB_A_FILES))
TOOL_LAMBDA_LIB_A_SRCS_S = $(filter %.S,$(TOOL_LAMBDA_LIB_A_FILES))
TOOL_LAMBDA_LIB_A_SRCS_C = $(filter %.c,$(TOOL_LAMBDA_LIB_A_FILES))
TOOL_LAMBDA_LIB_A_CHECKS = \
$(TOOL_LAMBDA_LIB_A_HDRS:%=o/$(MODE)/%.ok) \
$(TOOL_LAMBDA_LIB_A).pkg
TOOL_LAMBDA_LIB_A_SRCS = \
$(TOOL_LAMBDA_LIB_A_SRCS_S) \
$(TOOL_LAMBDA_LIB_A_SRCS_C)
TOOL_LAMBDA_LIB_A_OBJS = \
$(TOOL_LAMBDA_LIB_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(TOOL_LAMBDA_LIB_A_SRCS_C:%.c=o/$(MODE)/%.o)
TOOL_LAMBDA_LIB_A_DIRECTDEPS = \
LIBC_BITS \
LIBC_CALLS \
LIBC_INTRIN \
LIBC_LOG \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_UNICODE \
LIBC_MEM \
LIBC_FMT \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GETOPT
TOOL_LAMBDA_LIB_A_DEPS := \
$(call uniq,$(foreach x,$(TOOL_LAMBDA_LIB_A_DIRECTDEPS),$($(x))))
$(TOOL_LAMBDA_LIB_A): \
$(TOOL_LAMBDA_LIB_A).pkg \
$(TOOL_LAMBDA_LIB_A_OBJS)
$(TOOL_LAMBDA_LIB_A).pkg: \
$(TOOL_LAMBDA_LIB_A_OBJS) \
$(foreach x,$(TOOL_LAMBDA_LIB_A_DIRECTDEPS),$($(x)_A).pkg)
TOOL_LAMBDA_LIB_LIBS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)))
TOOL_LAMBDA_LIB_SRCS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_SRCS))
TOOL_LAMBDA_LIB_HDRS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_HDRS))
TOOL_LAMBDA_LIB_BINS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_BINS))
TOOL_LAMBDA_LIB_CHECKS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_CHECKS))
TOOL_LAMBDA_LIB_OBJS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_OBJS))
TOOL_LAMBDA_LIB_TESTS = $(foreach x,$(TOOL_LAMBDA_LIB_ARTIFACTS),$($(x)_TESTS))
.PHONY: o/$(MODE)/tool/lambda/lib
o/$(MODE)/tool/lambda/lib: $(TOOL_LAMBDA_LIB_CHECKS)

25
tool/lambda/lib/needbit.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/lambda/lib/blc.h"
char NeedBit(FILE* f) {
char b = GetBit(f);
if (b == -1) Error(9, "UNEXPECTED EOF");
return b;
}

55
tool/lambda/lib/parse.c Normal file
View file

@ -0,0 +1,55 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/lambda/lib/blc.h"
/**
* Parses binary lambda calculus closed expression from stream.
*/
struct Parse Parse(int ignored, FILE* f) {
int t, start;
char bit, need;
struct Parse p;
for (need = 0, start = end;;) {
if (end + 2 > TERMS) Error(5, "OUT OF TERMS");
if ((bit = GetBit(f)) == -1) {
if (!need) break;
fflush(stdout);
fputs("---\n", stderr);
Print(start, 0, 0, stderr);
Error(9, "UNFINISHED EXPRESSION");
} else if (bit) {
for (t = 0; NeedBit(f);) ++t;
mem[end++] = VAR;
mem[end++] = t;
break;
} else if (NeedBit(f)) {
t = end;
end += 2;
mem[t] = APP;
p = Parse(0, f);
mem[t + 1] = p.n;
need = 1;
} else {
mem[end++] = ABS;
}
}
p.i = start;
p.n = end - start;
return p;
}

View file

@ -0,0 +1,72 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/lambda/lib/blc.h"
static struct Parse ParseImpl(int tail, int need, FILE *f) {
struct Parse p, q;
int b, i, j, t, start;
for (start = end;;) {
if (end + 2 > TERMS) Error(5, "OUT OF TERMS");
if ((b = GetBit(f)) == -1) {
if (need) Error(9, "UNFINISHED EXPRESSION");
break;
} else if (b) {
for (t = 0; NeedBit(f);) ++t;
mem[end++] = VAR;
mem[end++] = t;
break;
} else if (NeedBit(f)) {
t = end;
end += 2;
p = ParseImpl(0, 1, f);
q = ParseImpl(t + 2, 1, f);
mem[t + 0] = APP;
mem[t + 1] = q.i - (t + 2);
break;
} else {
mem[end++] = ABS;
}
}
p.i = start;
p.n = end - start;
if (p.n && tail) {
/* find backwards overlaps within 8-bit displacement */
i = tail - 32768;
j = start - p.n;
for (i = i < 0 ? 0 : i; i <= j; ++i) {
if (!memcmp(mem + i, mem + p.i, p.n * sizeof(*mem))) {
memset(mem + start, -1, p.n * sizeof(*mem));
end = start;
p.i = i;
break;
}
}
}
return p;
}
/**
* Parses binary lambda calculus closed expression from stream.
*
* If `tail` is non-zero then this subroutine will perform expensive
* deduplication so that optimal ROMs may be computed ahead of time.
*/
struct Parse Parse(int tail, FILE *f) {
return ParseImpl(tail, 0, f);
}

1289
tool/lambda/lib/print.c Normal file

File diff suppressed because it is too large Load diff

40
tool/lambda/lib/vars.c Normal file
View file

@ -0,0 +1,40 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/lambda/lib/blc.h"
char binary; // 8-bit
char safer; // safer
char style; // notation
char asciiname; // <3 ascii
char noname; // rewriting
char rlog; // redex log
char slog; // state log
char alog; // action log
char vlog; // variable log
int co; // output character
int heap; // heap usage counter
long ip; // instruction pointer
long end; // end of code pointer
FILE *logh; // log file stdio stream
struct Closure *frep; // freed closures list
struct Closure *contp; // continuations stack
int mem[TERMS]; // bss memory for terms
struct Closure root = {.refs = 100000, .term = -1, .next = 0};
struct Closure *envp = &root;