Implemented seq module.
h3rald h3rald@h3rald.com
Fri, 14 Apr 2017 18:48:51 +0200
7 files changed,
186 insertions(+),
15 deletions(-)
M
lib/min_seq.nim
→
lib/min_seq.nim
@@ -46,13 +46,53 @@ i.reqQuotation q
let v = i.pop i.push newVal(v & q.qVal, i.scope) - .symbol("at") do (i: In): + .symbol("get") do (i: In): var index, q: MinValue i.reqIntAndQuotation index, q - if q.qVal.len-1 < index.intVal: - raiseOutOfBounds("Insufficient items in quotation") - i.push q.qVal[index.intVal.int] + let ix = index.intVal + if q.qVal.len < ix or ix < 0: + raiseOutOfBounds("Index out of bounds") + i.push q.qVal[ix.int] + + .symbol("set") do (i: In): + var val, index, q: MinValue + i.reqInt index + val = i.pop + i.reqQuotation q + let ix = index.intVal + if q.qVal.len < ix or ix < 0: + raiseOutOfBounds("Index out of bounds") + q.qVal[ix.int] = val + i.push q + .symbol("remove") do (i: In): + var index, q: MinValue + i.reqIntAndQuotation index, q + let ix = index.intVal + if q.qVal.len < ix or ix < 0: + raiseOutOfBounds("Index out of bounds") + var res = newSeq[MinValue](0) + for x in 0..q.qVal.len-1: + if x == ix: + continue + res.add q.qVal[x] + i.push res.newVal(i.scope) + + .symbol("insert") do (i: In): + var val, index, q: MinValue + i.reqInt index + val = i.pop + i.reqQuotation q + let ix = index.intVal + if q.qVal.len < ix or ix < 0: + raiseOutOfBounds("Index out of bounds") + var res = newSeq[MinValue](0) + for x in 0..q.qVal.len-1: + if x == ix: + res.add val + res.add q.qVal[x] + i.push res.newVal(i.scope) + .symbol("size") do (i: In): var q: MinValue i.reqQuotation q@@ -97,6 +137,18 @@ i.push e
i.unquote(filter) var check = i.pop if check.isBool and check.boolVal == true: + res.add e + i.push res.newVal(i.scope) + + .symbol("reject") do (i: In): + var filter, list: MinValue + i.reqTwoQuotations filter, list + var res = newSeq[MinValue](0) + for e in list.qVal: + i.push e + i.unquote(filter) + var check = i.pop + if check.isBool and check.boolVal == false: res.add e i.push res.newVal(i.scope)@@ -134,7 +186,7 @@ i2.push b
i2.unquote(cmp) let r = i2.pop if r.isBool: - if r.boolVal == true: + if r.isBool and r.boolVal == true: return 1 else: return -1@@ -150,6 +202,101 @@ i.reqIntAndQuotation n, q
if n.intVal > q.qVal.len: raiseInvalid("Quotation is too short") i.push q.qVal[0..n.intVal.int-1].newVal(i.scope) + + .symbol("find") do (i: In): + var s, test, result: MinValue + i.reqTwoQuotations test, s + var res = -1 + var c = 0 + for el in s.qVal: + i.push el + i.unquote test + result = i.pop + if result.isBool and result.boolVal == true: + res = c + break + c.inc + i.push res.newVal + + .symbol("reduce") do (i: In): + var s, q, acc: MinValue + i.reqQuotation q + acc = i.pop + i.reqQuotation s + for el in s.qVal: + i.push acc + i.push el + i.unquote q + acc = i.pop + i.push acc + + .symbol("map-reduce") do (i: In): + var s, map, red, acc: MinValue + i.reqThreeQuotations red, map, s + if s.qVal.len == 0: + raiseInvalid("Quotation must have at least one element") + i.push s.qVal[0] + i.unquote map + acc = i.pop + for ix in 1..s.qVal.len-1: + i.push s.qVal[ix] + i.unquote map + i.push acc + i.unquote red + acc = i.pop + i.push acc + + .symbol("partition") do (i: In): + var s, test: MinValue + i.reqTwoQuotations test, s + var tseq = newSeq[MinValue](0) + var fseq = newSeq[MinValue](0) + for el in s.qVal: + i.push el + i.unquote test + let res = i.pop + if res.isBool and res.boolVal == true: + tseq.add el + else: + fseq.add el + i.push tseq.newVal(i.scope) + i.push fseq.newVal(i.scope) + + .symbol("slice") do (i: In): + var start, finish, q: MinValue + i.reqInt finish + i.reqInt start + i.reqQuotation q + let st = start.intVal + let fn = finish.intVal + if st < 0 or fn > q.qVal.len-1: + raiseOutOfBounds("Index out of bounds") + elif fn < st: + raiseInvalid("End index must be greater than start index") + let rng = q.qVal[st.int..fn.int] + i.push rng.newVal(i.scope) + + .symbol("harvest") do (i: In): + var q: MinValue + i.reqQuotation q + var res = newSeq[MinValue](0) + for el in q.qVal: + if el.isQuotation and el.qVal.len == 0: + continue + res.add el + i.push res.newVal(i.scope) + + .symbol("flatten") do (i: In): + var q: MinValue + i.reqQuotation q + var res = newSeq[MinValue](0) + for el in q.qVal: + if el.isQuotation: + for el2 in el.qVal: + res.add el2 + else: + res.add el + i.push res.newVal(i.scope) # Operations on dictionaries
M
lib/min_sys.nim
→
lib/min_sys.nim
@@ -58,12 +58,12 @@ i.reqStringLike cmd
let res = execCmdEx(cmd.getString) i.push @[@["output".newSym, res.output.newVal].newVal(i.scope), @["code".newSym, res.exitCode.newVal].newVal(i.scope)].newVal(i.scope) - .symbol("getenv") do (i: In): + .symbol("get-env") do (i: In): var a: MinValue i.reqStringLike a i.push a.getString.getEnv.newVal - .symbol("putenv") do (i: In): + .symbol("put-env") do (i: In): var key, value: MinValue i.reqTwoStringLike key, value key.getString.putEnv value.getString@@ -180,7 +180,7 @@ i.reqStringLike f
i.push f.getString.parentDir.unix.newVal .symbol("$") do (i: In): - i.push("getenv".newSym) + i.push("get-env".newSym) .symbol("!") do (i: In): i.push("system".newSym)@@ -189,7 +189,7 @@ .symbol("&") do (i: In):
i.push("run".newSym) .sigil("$") do (i: In): - i.push("getenv".newSym) + i.push("get-env".newSym) .sigil("!") do (i: In): i.push("system".newSym)
M
min.vim
→
min.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 at atime b bind bool bool? bury1 bury2 bury3 c 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? dig1 dig2 dig3 dip dir? dirname div dprint dprint! dset dup dupd encode env? error eval even? exists? exit fappend fatal file? filename filter first float float? foreach fperms fread from-json format-error fs fsize fstats ftype fwrite gets getenv hardlink hidden? id if import in? indent info int int? interpolate interval io join k keep keys length linrec load load-symbol logic loglevel loglevel? lowercase ls ls-r map match md5 mkdir mod module module-symbols module-sigils mtime mv newline newstack nip not notice now num number? odd? os over password pick pop popd pred prepend print print! prompt publish puts puts! putenv q quotation? quote quote-bind quote-define random raise regex remove-symbol repeat replace rest reverse rm rmdir run save-symbol scope scope? seal search sha1 sha224 sha256 sha384 sha512 shorten sigils sip size sleep 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 b bind bool bool? bury1 bury2 bury3 c 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? dig1 dig2 dig3 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 k 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 match minDefaultSigil ;\<[:@'~!$%&$=<>#^*#+/]; contained
M
tests/lang.min
→
tests/lang.min
@@ -113,7 +113,7 @@ (("test" (1 2) :)) try stack ("test") ==) assert
( ( - (() 1 at) + (() 1 get) (1) ) try 1 ==) assert
M
tests/seq.min
→
tests/seq.min
@@ -13,10 +13,18 @@ (4 (1 2 3) append (1 2 3 4) ==) assert
(0 (1 2 3) prepend (0 1 2 3) ==) assert - ((1 2 3 4) 2 at 3 ==) assert + ((1 2 3 4) 2 get 3 ==) assert + + ((1 2 3 4) 222 2 set (1 2 222 4) ==) assert + + ((1 2 3 4) 2 remove (1 2 4) ==) assert + + ((1 2 3 4) 333 2 insert (1 2 333 3 4) ==) assert + ((1 2 3) size 3 ==) assert ((1 2 3 4) 5 in? false ==) assert + ((1 2 3 4) 2 in?) assert ((1 2 3 4) (2 +) map (3 4 5 6) ==) assert@@ -40,6 +48,22 @@
((3 4 7 2 4 6 5 6) '< sort (7 6 6 5 4 4 3 2) ==) assert ((1 2 3 4 5) 3 shorten (1 2 3) ==) assert + + ((1 2 3 4 5) (2 >) find 2 ==) assert + + ((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?) reject (2 4 6) ==) assert + + ((1 2 3 4 5 6) 2 4 slice (3 4 5) ==) assert + + ((2 3 () 4 (3 4) () () "test") harvest (2 3 4 (3 4) "test") ==) assert + + ((1 2 3 (4 5 6) 7 (8 9)) flatten (1 2 3 4 5 6 7 8 9) ==) assert report newstack
M
tests/stack.min
→
tests/stack.min
@@ -33,7 +33,7 @@ (1 2 3 pick stack (1 2 3 1) ==) assert
((1 2 3) ('sum 'size) => cleave / 2 ==) assert - ((1 2) (3 4) ((0 at) (1 at)) spread stack (1 4) ==) assert + ((1 2) (3 4) ((0 get) (1 get)) spread stack (1 4) ==) assert report newstack
M
tests/sys.min
→
tests/sys.min
@@ -36,7 +36,7 @@ ("PATH" env?) assert
($PATH length 0 >) assert - ("TEST" "AAA" putenv $AAA "TEST" ==) assert + ("TEST" "AAA" put-env $AAA "TEST" ==) assert (os length 0 >) assert@@ -73,4 +73,4 @@
report newstack - "systest" rmdir + "systest" rmdir