all repos — min @ 8c3abc2e6fe3de6a588bf5d11c8eaa01560a657b

A small but practical concatenative programming language.

Finlized stack module; documented seq module.
h3rald h3rald@h3rald.com
Sat, 15 Apr 2017 10:43:45 +0200
commit

8c3abc2e6fe3de6a588bf5d11c8eaa01560a657b

parent

75915243a6b1a7a28cde00ffaa859909b9f2dece

M lib/min_stack.nimlib/min_stack.nim

@@ -12,14 +12,14 @@ proc stack_module*(i: In)=

i.define() - .symbol("newstack") do (i: In): + .symbol("clear-stack") do (i: In): while i.stack.len > 0: discard i.pop - .symbol("stack") do (i: In): + .symbol("get-stack") do (i: In): i.push i.stack.newVal(i.scope) - .symbol("unstack") do (i: In): + .symbol("set-stack") do (i: In): var q: MinValue i.reqQuotation q i.stack = q.qVal

@@ -32,37 +32,21 @@ if i.stack.len < 1:

raiseEmptyStack() discard i.pop - .symbol("popop") do (i: In): - if i.stack.len < 2: - raiseEmptyStack() - discard i.pop - discard i.pop - - # (pop) dip - .symbol("popd") do (i: In): - i.push newVal(@["pop".newSym], i.scope) - i.push "dip".newSym - - # ((pop) dip unquote) - .symbol("k") do (i: In): - i.push newVal(@["pop".newSym], i.scope) - i.push "dip".newSym - i.push "unquote".newSym - .symbol("dup") do (i: In): i.push i.peek - # (dup) dip - .symbol("dupd") do (i: In): - i.push newVal(@["dup".newSym], i.scope) - i.push "dip".newSym - .symbol("dip") do (i: In): var q: MinValue i.reqQuotation q let v = i.pop i.unquote(q) i.push v + + .symbol("nip") do (i: In): + var a, b: MinValue + a = i.pop + b = i.pop + i.push a .symbol("cleave") do (i: In): var q: MinValue

@@ -137,11 +121,6 @@ i.push second

i.push first i.push third - # (swap) dip - .symbol("swapd") do (i: In): - i.push newVal(@["swap".newSym], i.scope) - i.push "dip".newSym - .symbol("cons") do (i: In): var q: MinValue i.reqQuotation q
M min.vimmin.vim

@@ -11,7 +11,7 @@

setl iskeyword=@,36-39,+,-,/,*,.,:,~,!,48-57,60-65,94-95,192-255 setl iskeyword+=^ -syntax keyword minDefaultSymbol ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ ROOT aes all? and any? append ask atime bind bool bool? call call! capitalize case cd chmod choose cleave column-print concat confirm cons cp cpu crypto ctime datetime ddel debug decode define delete dget dictionary? dip dir? dirname div dprint dprint! dset dup dupd encode env? error eval even? exists? exit fappend fatal find file? filename filter first flatten float float? foreach fperms fread from-json format-error fs fsize fstats ftype fwrite get gets get-env hardlink harvest hidden? id if import in? indent info insert int int? interpolate interval io join keep keys length linrec load load-symbol logic loglevel loglevel? lowercase ls ls-r map map-reduce match md5 mkdir mod module module-symbols module-sigils mtime mv newline newstack nip not notice now num number? odd? os over partition password pick pop popd pred prepend print print! prompt publish puts puts! put-env q quotation? quote quote-bind quote-define random raise reduce regex reject remove remove-symbol repeat replace rest reverse rm rmdir run save-symbol scope scope? seal search set sha1 sha224 sha256 sha384 sha512 shorten sigils sip size sleep slice sort source split spread stack startup stored-symbols str string string? strip succ sum swap swapd swons symbols symlink symlink? sys system take tformat time timeinfo times timestamp titleize to-json try unquote unstack uppercase unzip values version warn when which while with xor zip +syntax keyword minDefaultSymbol ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ ROOT aes all? and any? append ask atime bind bool bool? call call! capitalize case cd chmod choose clear-stack cleave column-print concat confirm cons cp cpu crypto ctime datetime ddel debug decode define delete dget dictionary? dip dir? dirname div dprint dprint! dset dup encode env? error eval even? exists? exit fappend fatal find file? filename filter first flatten float float? foreach fperms fread from-json format-error fs fsize fstats ftype fwrite get gets get-env get-stack hardlink harvest hidden? id if import in? indent info insert int int? interpolate interval io join keep keys length linrec load load-symbol logic loglevel loglevel? lowercase ls ls-r map map-reduce match md5 mkdir mod module module-symbols module-sigils mtime mv newline nip not notice now num number? odd? os over partition password pick pop popd pred prepend print print! prompt publish puts puts! put-env q quotation? quote quote-bind quote-define random raise reduce regex reject remove remove-symbol repeat replace rest reverse rm rmdir run save-symbol scope scope? seal search seq set set-stack sha1 sha224 sha256 sha384 sha512 shorten sigils sip size sleep slice sort source split spread stack startup stored-symbols str string string? strip succ sum swap swons symbols symlink symlink? sys system take tformat time timeinfo times timestamp titleize to-json try unquote uppercase unzip values version warn when which while with xor zip syntax match minDefaultSigil ;\<[:@'~!$%&$=<>#^*#+/]; contained
M site/contents/_includes/_defs_.mdsite/contents/_includes/_defs_.md

@@ -12,6 +12,8 @@ {{d => [dict](class:kwd)}}

{{d1 => [dict<sub>1</sub>](class:kwd)}} {{d2 => [dict<sub>2</sub>](class:kwd)}} {{i => [int](class:kwd)}} +{{i1 => [a<sub>1</sub>](class:kwd)}} +{{i2 => [a<sub>2</sub>](class:kwd)}} {{n => [num](class:kwd)}} {{any => [a](class:kwd)}} {{a1 => [a<sub>1</sub>](class:kwd)}}
M site/contents/_includes/_reference-seq.mdsite/contents/_includes/_reference-seq.md

@@ -3,7 +3,7 @@

{#op||all?||{{q1}} {{q2}}||{{b}}|| Applies predicate {{q2}} to each element of {{q1}} and returns {{t}} if all elements of {{q1}} satisfy predicate {{q2}}, {{f}} otherwise. #} -{#op||any?||{{q1}} {{q2}}||B|| +{#op||any?||{{q1}} {{q2}}||{{b}}|| Applies predicate {{q2}} to each element of {{q1}} and returns {{t}} if at least one element of {{q1}} satisfies predicate {{q2}}, {{f}} otherwise. #} {#op||append||{{any}} {{q}}||({{a0p}} {{any}})||

@@ -20,17 +20,6 @@ Concatenates {{q1}} with {{q2}}. #}

{#op||ddel||{{d1}} {{sl}}||{{d2}}|| Returns a copy of {{d1}} without the element with key {{sl}}. #} - -{#op||filter||{{q1}} {{q2}}||{{q3}}|| -> Returns a new quotation {{q3}} containing all elements of {{q1}} that satisfy predicate {{q2}}. -> -> > %sidebar% -> > Example -> > -> > The following program leaves `(2 6 8 12)` on the stack: -> > -> > (1 37 34 2 6 8 12 21) -> > (dup 20 < swap even? and) filter #} {#op||dget||{{d}} {{sl}}||{{any}}|| Returns the value of key {{sl}} from dictionary {{d}}. #}

@@ -49,9 +38,53 @@

{#op||dset||{{d1}} {{any}} {{sl}}||{{d2}}|| Sets the value of the {{sl}} of {{d1}} to {{any}}, and returns the modified copy of the dictionary {{d2}}. #} +{#op||filter||{{q1}} {{q2}}||{{q3}}|| +> Returns a new quotation {{q3}} containing all elements of {{q1}} that satisfy predicate {{q2}}. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(2 6 8 12)` on the stack: +> > +> > (1 37 34 2 6 8 12 21) +> > (dup 20 < swap even? and) filter #} + +{#op||find||{{q1}} {{q2}}||{{i}}|| +> Returns the index of the first element within {{q1}} that satisfies predicate {{q2}}. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `3` on the stack: +> > +> > (1 2 4 8 16) +> > (5 >) find #} + {#op||first||{{q}}||{{any}}|| Returns the first element of {{q}}. #} +{#op||flatten||{{q1}}||{{q2}}|| +> Flattens all quotations within {{q1}} and returns the resulting sequence {{q2}}. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(1 2 3 4 5 6 7 8)` on the stack: +> > +> > (1 (2 3 4) 5 (6 7) 8) +> > flatten #} + +{#op||harvest||{{q1}}||{{q2}}|| +> Creates a new quotation {{q2}} containing all elements of {{q1}} except for empty quotations. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(1 2 3)` on the stack: +> > +> > (1 () () () 2 () 3) +> > harvest #} + {#op||in?||{{q}} {{any}}||{{b}}|| Returns {{t}} if {{any}} is contained in {{q}}, {{f}} otherwise.#}

@@ -64,9 +97,42 @@

{#op||map||{{q1}} {{q2}}||{{q3}}|| Returns a new quotation {{q3}} obtained by applying {{q2}} to each element of {{q1}}.#} +{#op||map-reduce||{{q1}} {{q2}} {{q3}}||{{i}}|| +> Applies {{q2}} (map) to each element of {{q1}} and then applies {{q3}} (reduce) to each successive element of {{q1}}. {{q1}} must have at least one element. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `35` on the stack: +> > +> > (1 3 5) +> > (dup *) (+) map-reduce #} + +{#op||partition||{{q1}} {{q2}}||{{q3}} {{q4}}|| +> Partitions {{q1}} into two quotations: {{q3}} contains all elements of {{q1}} that satisfy predicate {{q2}}, {{q4}} all the others. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(1 2 3) (2 4 6)` on the stack: +> > +> > (1 2 3 4 5 6) +> > (odd?) partition #} + {#op||prepend||{{any}} {{q}}||({{any}} {{a0p}})|| Returns a new quotation containing the contents of {{q}} with {{any}} prepended. #} +{#op||reduce||{{q1}} {{any}} {{q2}}||{{i}}|| +> Combines each successive element of {{q1}} using {{q2}}. On the first iteration, the first two inputs processed by {{q2}} are {{any}} and the first element of {{q1}}. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `120` on the stack: +> > +> > (1 2 3 4 5) +> > 1 (*) reduce #} + {#op||reject||{{q1}} {{q2}}||{{q3}}|| Returns a new quotatios {{q3}} including all elements of {{q1}} that do not satisfy predicate {{q2}} (i.e. the opposite of `filter`)#}

@@ -87,6 +153,17 @@ Returns a quotation {{q2}} containing the first _n_ values of the input quotation {{q1}}. #}

{#op||size||{{q}}||{{i}}|| Returns the length of {{q}}.#} + +{#op||slice||{{q1}} {{i1}} {{i2}}||{{q2}}|| +> Creates a new quotation {{q2}} obtaining by selecting all elements of {{q1}} between indexes {{i1}} and {{i2}}. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(3 4 5)` on the stack: +> > +> > (1 2 3 4 5 6) +> > 2 4 slice #} {#op||sort||{{q1}} {{q2}}||{{q3}}|| > Sorts all elements of {{q1}} according to predicate {{q2}}.
M site/contents/_includes/_reference-stack.mdsite/contents/_includes/_reference-stack.md

@@ -1,5 +1,8 @@

{@ _defs_.md || 0 @} +{#op||clear-stack||{{null}}||{{null}}|| +Empties the stack.#} + {#op||cleave||{{a1}} ({{q}}{{0p}})||{{a0p}}|| > Applies each quotation contained in the first element to the second element {{a1}}. > > %sidebar%

@@ -18,15 +21,12 @@

{#op||dup||{{a1}}||{{a1}} {{a1}}|| Duplicates the first element on the stack.#} -{#op||dupd||{{a1}} {{a2}}||{{a1}} {{a1}} {{a2}}|| -Duplicates the second element on the stack.#} +{#op||get-stack||{{null}}||({{a0p}})|| +Returns a quotation containing the contents of the stack.#} {#op||id||{{null}}||{{null}}|| Does nothing.#} -{#op||k||{{a1}} ({{a2}})||{{a0p}}|| -K combinator; removes the second element from the stack and unquotes the first element on the stack.#} - {#op||keep||{{a1}} {{q}}||{{a0p}} {{a1}}|| > Applies each quotation contained in the first element to each subsequent corresponding element. > > %sidebar%

@@ -36,8 +36,8 @@ > > The following program leaves `5 3` on the stack:

> > > > `2 3 '+ keep` #} -{#op||newstack||{{null}}||{{null}}|| -Empties the stack.#} +{#op||nip||{{a1}} {{a2}}||{{a2}}|| +Removes the second element from the stack.#} {#op||over||{{a1}} {{a2}}||{{a1}} {{a2}} {{a1}}|| Pushes a copy of the second element on top of the stack.#}

@@ -48,17 +48,14 @@

{#op||pop||{{any}}||{{null}}|| Removes the first element from the stack.#} -{#op||popd||{{a1}} {{a2}}||{{a2}}|| -Removes the second element from the stack.#} - -{#op||popop||{{a1}} {{a2}}||{{null}}|| -Removes the first two elements from the stack.#} - {#op||rolldown||{{a1}} {{a2}} {{a3}}||{{a2}} {{a3}} {{a1}}|| Moves the third element in first position, the second in third position and the the first in second position.#} {#op||rollup||{{a1}} {{a2}} {{a3}}||{{a3}} {{a2}} {{a1}}|| Moves the third and second element into second and third position and moves the first element into third position.#} + +{#op||set-stack||{{q}}||{{a0p}}|| +Substitute the existing stack with the contents of {{q}}.#} {#op||sip||{{a1}} ({{a2}})||{{a0p}} {{a1}}|| Saves the {{a1}}, unquotes {{a2}}, and restores {{a1}}.#}

@@ -71,20 +68,9 @@ > >

> > The following program leaves `(1 4)` on the stack: > > > > `(1 2) (3 4) ((0 get) (1 get)) spread` #} - -{#op||stack||{{null}}||({{a0p}})|| -Returns a quotation containing the contents of the stack.#} {#op||swap||{{a1}} {{a2}}||{{a2}} {{a1}}|| Swaps the first two elements on the stack. #} -{#op||swapd||{{a1}} {{a2}} {{a3}}||{{a2}} {{a1}} {{a3}}|| -Swaps the second and third elements on the stack. #} - {#op||swons||({{a0p}}) {{a1}}||({{a1}} {{a0p}})|| Prepends {{a1}} to the quotation that follows it.#} - -{#op||unstack||{{q}}||{{a0p}}|| -Substitute the existing stack with the contents of {{q}}.#} - -
M site/contents/_includes/_reference.mdsite/contents/_includes/_reference.md

@@ -54,10 +54,10 @@ : An error dictionary:

<pre><code>( (error "MyError") (message "An error occurred") - (symbol "symbol1") - (filename "dir1/file1.min") - (line 3) - (column 13) + (symbol "symbol1") + (filename "dir1/file1.min") + (line 3) + (column 13) ) </code></pre> {{t}}

@@ -80,6 +80,6 @@ : The fourth value of the specified type.

{{01}} : Zero or one. {{0p}} -: Zero more. +: Zero or more. {{1p}} : One or more
M tests/crypto.mintests/crypto.min

@@ -20,4 +20,4 @@

("test" "test" aes "test" aes strip "test" ==) assert report - newstack + clear-stack
M tests/fs.mintests/fs.min

@@ -16,5 +16,5 @@

("test.txt" fstats 'type dget "file" ==) assert report - newstack - "test.txt" rm + clear-stack + "test.txt" rm
M tests/io.mintests/io.min

@@ -11,4 +11,4 @@ "test.txt" fread "TEST - TEST" ==) assert

"test.txt" rm report - newstack + clear-stack
M tests/lang.mintests/lang.min

@@ -68,7 +68,7 @@

(2 quote (2) ==) assert - ((2 3) unquote stack (2 3) ==) assert + ((2 3) unquote get-stack (2 3) ==) assert (3 (succ) 3 times 6 ==) assert

@@ -89,7 +89,7 @@ (

(pop) ('error dget) ("finally") - ) try stack ("MinEmptyStackError" "finally") ==) assert + ) try get-stack ("MinEmptyStackError" "finally") ==) assert ("aaaa" :cd cd "aaaa" ==) assert ;It is possible to shadow sealed symbols in child scopes

@@ -109,7 +109,7 @@ ('error dget)

) try "TestError" ==) assert ( - (("test" (1 2) :)) try stack ("test") ==) assert + (("test" (1 2) :)) try get-stack ("test") ==) assert ( (

@@ -149,4 +149,4 @@ (sys module-sigils ("!" "$" "&") ==) assert

report ; Tidy up - newstack + clear-stack
M tests/logic.mintests/logic.min

@@ -133,4 +133,4 @@ (false quotation? false ==) assert

(("a" 2 c) quotation?) assert report - newstack + clear-stack
M tests/num.mintests/num.min

@@ -43,4 +43,4 @@

((2 4 6 8) (even?) all?) assert report - newstack + clear-stack
M tests/seq.mintests/seq.min

@@ -55,7 +55,7 @@ ((1 2 3 4 5) 1 (*) reduce 120 ==) assert

((1 3 5) (dup *) (+) map-reduce 35 ==) assert - ((1 2 3 4 5 6) (odd?) partition stack ((1 3 5) (2 4 6)) ==) assert + ((1 2 3 4 5 6) (odd?) partition get-stack ((1 3 5) (2 4 6)) ==) assert ((1 2 3 4 5 6) (odd?) reject (2 4 6) ==) assert

@@ -66,4 +66,4 @@

((1 2 3 (4 5 6) 7 (8 9)) flatten (1 2 3 4 5 6 7 8 9) ==) assert report - newstack + clear-stack
M tests/stack.mintests/stack.min

@@ -3,35 +3,35 @@ 'test import

"stack" describe - (1 2 3 4 stack (1 2 3 4) ==) assert + (1 2 3 4 get-stack (1 2 3 4) ==) assert - ((1 2 3) unstack stack (1 2 3) ==) assert + ((1 2 3) set-stack get-stack (1 2 3) ==) assert (1 id 1 ==) assert - (2 id 2 ==) assert - - (2 pop stack () ==) assert + (2 pop get-stack () ==) assert - (1 dup stack (1 1) ==) assert + (1 dup get-stack (1 1) ==) assert (3 2 (1 +) dip + 6 ==) assert - (1 2 4 '+ dip stack (3 4) ==) assert + (1 2 nip get-stack (2) ==) assert - (1 2 4 '+ keep stack (1 6 4) ==) assert + (1 2 4 '+ dip get-stack (3 4) ==) assert + + (1 2 4 '+ keep get-stack (1 6 4) ==) assert ((1) (2 swap append) sip concat (1 2 1) ==) assert (1 (2 3) cons (1 2 3) ==) assert - (1 2 over stack (1 2 1) ==) assert + (1 2 over get-stack (1 2 1) ==) assert - (1 2 3 pick stack (1 2 3 1) ==) assert + (1 2 3 pick get-stack (1 2 3 1) ==) assert ((1 2 3) ('sum 'size) => cleave / 2 ==) assert - ((1 2) (3 4) ((0 get) (1 get)) spread stack (1 4) ==) assert + ((1 2) (3 4) ((0 get) (1 get)) spread get-stack (1 4) ==) assert report - newstack + clear-stack
M tests/str.mintests/str.min

@@ -68,4 +68,4 @@

("3.678" float 3.678 ==) assert report - newstack + clear-stack
M tests/sys.mintests/sys.min

@@ -72,5 +72,5 @@

report - newstack + clear-stack "systest" rmdir
M tests/test.mintests/test.min

@@ -54,7 +54,7 @@ ()

if ) map - pop ;Remove results accomulator from stack + pop ;Remove results accomulator from get-stack ) :report ) +test
M tests/time.mintests/time.min

@@ -14,4 +14,4 @@

(1464951736 timeinfo 'second dget 16 ==) assert report - newstack + clear-stack