From a9d6c221d7686e3055593359b02125a3f20ab3e5 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Sat, 20 Jan 2024 02:38:26 -0800 Subject: add map, filter might work? --- README.md | 2 +- examples/aoc1.stem | 9 -------- examples/fib.stem | 20 ------------------ examples/math.stem | 2 -- examples/repl.stem | 6 ------ examples/simple.stem | 1 - examples/stdlib.stem | 29 ------------------------- src/main.c | 4 ++-- stemlib/aoc1.stem | 9 ++++++++ stemlib/fib.stem | 20 ++++++++++++++++++ stemlib/math.stem | 2 ++ stemlib/repl.stem | 6 ++++++ stemlib/simple.stem | 1 + stemlib/stdlib.stem | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 101 insertions(+), 70 deletions(-) delete mode 100644 examples/aoc1.stem delete mode 100644 examples/fib.stem delete mode 100644 examples/math.stem delete mode 100644 examples/repl.stem delete mode 100644 examples/simple.stem delete mode 100644 examples/stdlib.stem create mode 100644 stemlib/aoc1.stem create mode 100644 stemlib/fib.stem create mode 100644 stemlib/math.stem create mode 100644 stemlib/repl.stem create mode 100644 stemlib/simple.stem create mode 100644 stemlib/stdlib.stem diff --git a/README.md b/README.md index efea70a..cd1c919 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Introduction {#mainpage} +# Introduction Stem aims to be a minimal interpreted stack based programming language that allows for metaprogramming and a foreign language interface. It features a C API that is elegant and simple. Additionally, garbage collection is diff --git a/examples/aoc1.stem b/examples/aoc1.stem deleted file mode 100644 index 16f739d..0000000 --- a/examples/aoc1.stem +++ /dev/null @@ -1,9 +0,0 @@ - -dsc -swap -isnum -char -0 -str - -read_str [ ] diff --git a/examples/fib.stem b/examples/fib.stem deleted file mode 100644 index de02b5c..0000000 --- a/examples/fib.stem +++ /dev/null @@ -1,20 +0,0 @@ -# Author: Andrei Sova 2023 -fib [ - dup 1 <= [ ] [ - dup 1 - fib - swap - dup 2 - fib - swap - dsc - + - ] if -] func - -main [ - dup 10 <= [ - dup fib . - 1 + main - ] [ exit ] if -] func - -0 main diff --git a/examples/math.stem b/examples/math.stem deleted file mode 100644 index 1a6f931..0000000 --- a/examples/math.stem +++ /dev/null @@ -1,2 +0,0 @@ -factorial [ dup 0 <= [ 1 + ] [ dup 1 - factorial * ] if ] func -PI 3.1415926 func diff --git a/examples/repl.stem b/examples/repl.stem deleted file mode 100644 index 6ed6795..0000000 --- a/examples/repl.stem +++ /dev/null @@ -1,6 +0,0 @@ -"./stdlib.stem" fread strquote eval -"./math.stem" include - -"You can make your own REPL by changing the code!" dsc - -repl [ "> " . read strquote eval repl ] func repl diff --git a/examples/simple.stem b/examples/simple.stem deleted file mode 100644 index 5853c3f..0000000 --- a/examples/simple.stem +++ /dev/null @@ -1 +0,0 @@ -3 4 + . diff --git a/examples/stdlib.stem b/examples/stdlib.stem deleted file mode 100644 index df81cd8..0000000 --- a/examples/stdlib.stem +++ /dev/null @@ -1,29 +0,0 @@ -evalstr [ strquote eval ] func -include [ fread evalstr ] func - -# Author: Preston Pan -neg [ 0 swap - ] func - -# Author of loop function: Andrei S -loop [ - swap dup 0 > [ - swap - dup eval - swap 1 - swap loop - ] [ dsc dsc ] if -] func - -# Author: Matthew H -dupd [ [ dup ] keep ] func -over [ dupd swap ] func -dup2 [ over over ] func -dip2 [ swap [ dip ] dip ] func -while [ dup2 [ [ ] if ] dip2 over [ while ] [ dsc dsc ] if ] func - -loop-times [ dup2 [ swap [ ] if ] dip2 -dup [ 1 - loop-times ] [ dsc dsc ] if ] func - -d>base [ [ pow * "" swap ] keep2 - -[ [ over ] [ [ dup2 / floor * swap over - ] keep [ [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "↊" "↋" ] vat swap + ] with dip2 ] while dsc2 dup len(str-len) ] dip -- dup2 tail [ head "." ] dip + + ] func diff --git a/src/main.c b/src/main.c index cbf7f34..9d2d381 100644 --- a/src/main.c +++ b/src/main.c @@ -48,8 +48,8 @@ void sigint_handler(int signum) { int main(int argc, char **argv) { value_t *v; - size_t len; - char *buf; + size_t len = 0; + char *buf = ""; /* Parsing arguments */ if (argc < 2) { diff --git a/stemlib/aoc1.stem b/stemlib/aoc1.stem new file mode 100644 index 0000000..16f739d --- /dev/null +++ b/stemlib/aoc1.stem @@ -0,0 +1,9 @@ + +dsc +swap +isnum +char +0 +str + +read_str [ ] diff --git a/stemlib/fib.stem b/stemlib/fib.stem new file mode 100644 index 0000000..de02b5c --- /dev/null +++ b/stemlib/fib.stem @@ -0,0 +1,20 @@ +# Author: Andrei Sova 2023 +fib [ + dup 1 <= [ ] [ + dup 1 - fib + swap + dup 2 - fib + swap + dsc + + + ] if +] func + +main [ + dup 10 <= [ + dup fib . + 1 + main + ] [ exit ] if +] func + +0 main diff --git a/stemlib/math.stem b/stemlib/math.stem new file mode 100644 index 0000000..1a6f931 --- /dev/null +++ b/stemlib/math.stem @@ -0,0 +1,2 @@ +factorial [ dup 0 <= [ 1 + ] [ dup 1 - factorial * ] if ] func +PI 3.1415926 func diff --git a/stemlib/repl.stem b/stemlib/repl.stem new file mode 100644 index 0000000..0131c18 --- /dev/null +++ b/stemlib/repl.stem @@ -0,0 +1,6 @@ +"./stdlib.stem" fread strquote eval +"./math.stem" include + +# You can make your own REPL by changing the code! + +repl [ "> " . read strquote eval repl ] func repl diff --git a/stemlib/simple.stem b/stemlib/simple.stem new file mode 100644 index 0000000..5853c3f --- /dev/null +++ b/stemlib/simple.stem @@ -0,0 +1 @@ +3 4 + . diff --git a/stemlib/stdlib.stem b/stemlib/stdlib.stem new file mode 100644 index 0000000..6b2edac --- /dev/null +++ b/stemlib/stdlib.stem @@ -0,0 +1,60 @@ +#!/usr/local/bin/stem +# Author: Preston Pan +evalstr [ strquote eval ] func +include [ fread evalstr ] func + +neg [ 0 swap - ] func + +# Author of loop function: Andrei S +loop [ + swap dup 0 > [ + swap + dup eval + swap 1 - swap loop + ] [ dsc dsc ] if +] func + + +swapd [ [ swap ] keep ] func +swapt [ [ [ swap ] keep ] keep ] func + +dscd [ swap dsc ] func +dsct [ swapd swap dsc ] func + +dsc3 [ dsc dsc dsc ] func + +# Author: Matthew H +dupd [ [ dup ] keep ] func +dupt [ [ [ dup ] keep ] keep ] func +dupq [ [ [ [ dup ] keep ] keep ] keep ] func + +over [ dupd swap ] func +over2 [ dupt swapd swap ] func +over3 [ dupq swapt swapd swap ] func + +dup2 [ over over ] func +dup3 [ over2 over2 over2 ] func +dup4 [ over3 over3 over3 over3 ] func + +dip2 [ swap [ dip ] dip ] func +dip3 [ swap [ dip ] dip ] func +while [ dup2 [ [ ] if ] dip2 over [ while ] [ dsc dsc ] if ] func +when [ [ ] if ] func + +loop-times [ dup2 [ swap [ ] if ] dip2 +dup [ 1 - loop-times ] [ dsc dsc ] if ] func + + +# d>base [ [ pow * "" swap ] keep2 + +# [ [ over ] [ [ dup2 / floor * swap over - ] keep [ [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "↊" "↋" ] vat swap + ] with dip2 ] while dsc2 dup len(str-len) ] dip +# - dup2 tail [ head "." ] dip + + ] func + +# Author: Preston Pan +map [ [ ] over2 over2 len 0 swap [ dup4 swap vat over2 eval dscd dscd quote compose swap 1 + dsct dsct dsct over3 swap over3 swap ] swap loop-times dsc3 dscd dscd ] func +filter [ [ ] over2 over2 len 0 swap [ dup4 swap vat over2 eval dscd dscd [ quote compose ] when swap 1 + dsct dsct dsct over3 swap over3 swap ] swap loop-times dsc3 dscd dscd ] func + +# [map][quote][valnew][1] + +# [map][quote][val][map][quote][0] +# [val][map][quote][map][1] -- cgit