From d0d462623debac7203aa963f4a2363e1617f3334 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Wed, 8 Jan 2025 12:39:11 -0600 Subject: [PATCH] major parser fix, reorg kl source files fixed an unfortunate off-by-one error, and reorganised how source files are stored (especially for the web interface), as work towards some sort of standard library develops --- klsrc/combinators.kl | 29 +++++++++++++++++++++++++++++ klsrc/stdlib.kl | 30 ++++++++++++++++++++++++++++++ klsrc/util.kl | 6 ++++++ src/Makefile | 4 +++- src/emscr.c | 2 +- src/parser.c | 2 +- 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 klsrc/combinators.kl create mode 100644 klsrc/stdlib.kl create mode 100644 klsrc/util.kl diff --git a/klsrc/combinators.kl b/klsrc/combinators.kl new file mode 100644 index 0000000..431ef57 --- /dev/null +++ b/klsrc/combinators.kl @@ -0,0 +1,29 @@ +(def D (B B)) +(def T (C I)) +(def V (B C T)) +(def O (S I)) +(def M (S I I)) +(def Q (C B)) +(def L (C B M)) +(def G (B B C)) +(def E (B (B B B))) +(def R (B B T)) +(def H (B W (B C))) +(def U (L O)) +(def F (E T T E T)) + +(def B1 (B B B)) +(def B2 (B B1 B)) +(def B3 (B (B B) B)) + +(def C* (B C)) +(def C** (B C*)) +(def C*** (B C**)) + +(def C*n (Z (B (S (C (eq 0) C)) (B D (C B (C - 1)))))) + +(def C1 (C* (B C))) +(def C2 (C* (B C1))) +(def C3 (C* (B C2))) + +(def Cn (Z (B (S (C (eq 0) C)) (B (B (B C)) (B D (C B (C - 1))))))) diff --git a/klsrc/stdlib.kl b/klsrc/stdlib.kl new file mode 100644 index 0000000..e986dce --- /dev/null +++ b/klsrc/stdlib.kl @@ -0,0 +1,30 @@ +(def comment (rest (K nil))) + +(def map (B (B reverse) (B (B (Z (B (S (S (B (eq nil) (2 cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (Phi cons (S car (B (B car cdr) cdr)) (B car cdr)) (3 cdr))))))) (C (B B cons) (cons nil))))) + +(def list (rest (map unquote))) + +(def abstract-rec (B (B (B (C B cons))) (B (B (B B)) (B (B (B Z)) (C (B B (B B (B B (B S (C (C Phi cdr) car))))) (B (B (C B)) (B (C B (C B cdr)) (B (Phi cons) (C (C Phi car) cdr))))))))) + +(def abstract-generator (Z (B (B (B (B (C (C C nil))))) (B (Phi B (B S (B cons))) (B (B W) (B (B B))))))) + +(def reverse (abstract-rec not (C (B cons car)) cdr nil)) + +(def len (abstract-rec not (B K (+ 1)) cdr 0)) + +(def fold (B (S (C (C (eq nil) nil) (Z (B (S (S (B (eq nil) (B cdr cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (S (S car (B car cdr)) (B car (2 cdr))) (3 cdr)))))))) cons)) + +(def compose-all (rest (B (fold (B (C B unquote) B)) (cons I)))) + +(def append (B (C (abstract-rec not (B (C B car) (C cons)) cdr)) reverse)) + +(def filter (C (C (B (abstract-rec not) (B W (B (B C) (C (B C (B (B B) (C (B B S) (C cons)))) car)))) cdr) nil)) + +(def qsort ((B Z (B (B (S (W atom))) (Phi (Phi (Phi append)) (B (C B) (C (B (Phi filter) (B (B (B not)) (C B car))) cdr)) (B (B (Phi cons car)) (B (C B) (C (B (Phi filter) (C B car)) cdr)))))))) + + +(def import (B (B B) B (map unquote) parse readfile)) + + +(import "klsrc/combinators.kl") +(import "klsrc/util.kl") \ No newline at end of file diff --git a/klsrc/util.kl b/klsrc/util.kl new file mode 100644 index 0000000..8e8a40c --- /dev/null +++ b/klsrc/util.kl @@ -0,0 +1,6 @@ + +(def range (abstract-rec (eq 0) (C cons) (C - 1) nil)) + +(def x-times-n (C (C (D abstract-rec (eq 0) (D B K cons)) (C - 1)) nil)) + +(def m-rand-n (B (C (C (D abstract-rec (eq 0) (D B K cons)) (C - 1)) nil) rand)) \ No newline at end of file diff --git a/src/Makefile b/src/Makefile index bb8ed45..abab88e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -44,7 +44,9 @@ $(WEB_OBJS): $(WEB_BUILD)/%.o:%.c Makefile .PHONY: web web: $(WEB_OBJS) $(WEB_BUILD)/../sw.js $(WEB_BUILD)/../setup.js $(WEB_BUILD)/../shell.html $(WEB_BUILD)/../manifest.json - emcc $(WEB_OBJS) --shell-file $(WEB_SHELL) -o $(WEB_BUILD)/klrepl.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" --preload-file ../demos.kl@demos.kl + mkdir -p $(WEB_BUILD)/../klsources/klsrc/ + cp -r ../klsrc/*.kl $(WEB_BUILD)/../klsources/klsrc + emcc $(WEB_OBJS) --shell-file $(WEB_SHELL) -o $(WEB_BUILD)/klrepl.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" --preload-file $(WEB_BUILD)/../klsources@/ mkdir -p $(WEB_BUILD)/../static/repl cp $(WEB_BUILD)/../kl.png $(WEB_BUILD)/../static/repl cp $(WEB_BUILD)/../manifest.json $(WEB_BUILD)/../static/repl diff --git a/src/emscr.c b/src/emscr.c index f312ade..b7e9119 100644 --- a/src/emscr.c +++ b/src/emscr.c @@ -15,7 +15,7 @@ Sexpr* my_env = NULL; int main() { my_env = init_dict(); load_env(my_env); - load_file(my_env, "/demos.kl"); + load_file(my_env, "klsrc/stdlib.kl"); printf(" ready\n"); return 0; } diff --git a/src/parser.c b/src/parser.c index 7559581..0269ec2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -23,7 +23,7 @@ char* escapify(char* s) { // converts strings that are indeed supposed to be 'strings' // into properly escaped versions of themselves // i.e. the two-character "\n" gets converted to the single-character newline - char* out = malloc(sizeof(char)*strlen(s)); + char* out = malloc(sizeof(char)*(strlen(s)+1)); char* outidx = out; char* sidx = s; // index on s char tmp[3] = {0, 0, 0}; -- 2.39.2