Implemented more combinators & math operators.
h3rald h3rald@h3rald.com
Sun, 30 Nov 2014 16:52:45 +0100
2 files changed,
84 insertions(+),
7 deletions(-)
M
prelude.min
→
prelude.min
@@ -15,9 +15,27 @@ [$HOME] [$HOMEPATH] :
[$USER] [$USERNAME] : -// Mathematical +// Mathematical Operators +[1 +] [succ] : +[1 -] [pred] : +[2 mod 0 ==] [even?] : +[even? not] [odd?] : -[1 +] [succ] : -[1 -] [pred] : +[[ dup 0 == ] [ 1 + ] [ dup 1 - ] [ * ] linrec] [factorial] : -[[ dup 0 == ] [ 1 + ] [ dup 1 - ] [ * ] linrec] [factorial] : +// Stack Operators +[swap cons] [swons] : +[[pop] dip] [popd] : +[[dup] dip] [dupd] : +[[swap] dip] [swapd] : +[[dup] dip i] [q] : +[[zap] dip i] [k] : +[[cons] dip i] [b] : +[[swap] dip i] [c] : +[[dip] cons cons] [take] : +[[] cons dip] [dig1] : +[[] cons cons dip] [dig2] : +[[] cons cons cons dip] [dig3] : +[[[] cons] dip swap i] [bury1] : +[[[] cons cons] dip swap i] [bury2] : +[[[] cons cons cons] dip swap i] [bury3] :
M
primitives.nim
→
primitives.nim
@@ -16,13 +16,16 @@ for s in ALIASES:
q.add s.newVal i.push q.newVal +minsym "debug?": + i.push i.debugging.newVal + minsym "debug": i.debugging = not i.debugging echo "Debugging: $1" % [$i.debugging] # Common stack operations -minsym "i": +minsym "id": discard minsym "pop":@@ -45,6 +48,16 @@ let a = i.pop
let b = i.pop i.push a i.push b + +minsym "sip": + let a = i.pop + let b = i.pop + if a.isQuotation and b.isQuotation: + i.push b + i.push a.qVal + i.push b + else: + i.error(errIncorrect, "Two quotations are required on the stack") # Operations on quotations@@ -105,6 +118,21 @@ while check.isBool and check.boolVal == true:
i.push d.qVal i.push b.qVal check = i.pop + else: + i.error(errIncorrect, "Two quotations are required on the stack") + +minsym "filter": + let filter = i.pop + let list = i.pop + var res = newSeq[TMinValue](0) + if filter.isQuotation and list.isQuotation: + for e in list.qVal: + i.push e + i.push filter.qVal + var check = i.pop + if check.isBool and check.boolVal == true: + res.add e + i.push res.newVal else: i.error(errIncorrect, "Two quotations are required on the stack")@@ -248,6 +276,22 @@ i.push newVal(b.intVal.float / a.floatVal)
else: i.error(errTwoNumbersRequired) +minsym "div": + let b = i.pop + let a = i.pop + if a.isInt and b.isInt: + i.push(newVal(a.intVal div b.intVal)) + else: + i.error errIncorrect, "Two integers are required on the stack" + +minsym "mod": + let b = i.pop + let a = i.pop + if a.isInt and b.isInt: + i.push(newVal(a.intVal mod b.intVal)) + else: + i.error errIncorrect, "Two integers are required on the stack" + # Language constructs minsym "def":@@ -482,12 +526,20 @@ i.push execProcess(cmd, args, nil, {poUsePath}).newVal
else: i.error(errIncorrect, "A string is required on the stack") -minsym "env": +minsym "getenv": let a = i.pop if a.isString: i.push a.strVal.getEnv.newVal else: i.error(errIncorrect, "A string is required on the stack") + +minsym "putenv": + let value = i.pop + let key = i.pop + if value.isString and key.isString: + key.strVal.putEnv value.strVal + else: + i.error(errIncorrect, "Two strings are required on the stack") minsym "os": i.push hostOS.newVal@@ -538,10 +590,12 @@ except:
warn getCurrentExceptionMsg() else: i.error errIncorrect, "A string is required on the stack" + # Aliases minalias "quit", "exit" minalias "&", "concat" +minalias "cat", "concat" minalias "%", "print" minalias ":", "def" minalias "eq", "=="@@ -555,4 +609,9 @@ minalias "shell", "system"
minalias "sh", "system" minalias "!", "system" minalias "!&", "run" -minalias "$", "env" +minalias "$", "getenv" +minalias "zap", "pop" +minalias "unit", "quote" +minalias "i", "unquote" +minalias "i", "apply" +minalias "select", "filter"