all repos — min @ 2183c39775bf4f363fb35285f2b430bbb551cff7

A small but practical concatenative programming language.

Renamed trig to math; added tests and additional symbols.
h3rald h3rald@h3rald.com
Sat, 28 Oct 2017 11:21:41 +0200
commit

2183c39775bf4f363fb35285f2b430bbb551cff7

parent

ec9d3eace454c53ab31d57edc9958cc37098129d

8 files changed, 156 insertions(+), 109 deletions(-)

jump to
M core/value.nimcore/value.nim

@@ -80,6 +80,14 @@ return MinValue(kind: minSymbol, symVal: s)

# Get string value from string or quoted symbol +proc getFloat*(v: MinValue): float = + if v.isInt: + return v.intVal.float + elif v.isFloat: + return v.floatVal + else: + raiseInvalid("Value is not a number") + proc getString*(v: MinValue): string = if v.isSymbol: return v.symVal
A lib/min_math.nim

@@ -0,0 +1,109 @@

+import + tables, + math +import + ../core/parser, + ../core/value, + ../core/interpreter, + ../core/utils + + # Math + +proc math_module*(i: In)= + + let def = i.define() + + def.symbol("floor") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.floor.newVal + + def.symbol("ceil") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.ceil.newVal + + def.symbol("trunc") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.trunc.newVal + + def.symbol("round") do (i: In): + let vals = i.expect("int", "num") + let places = vals[0].intVal.int + let n = vals[1].getFloat + i.push n.round(places).newVal + + def.symbol("e") do (i: In): + i.push E.newVal + + def.symbol("pi") do (i: In): + i.push PI.newVal + + def.symbol("tau") do (i: In): + i.push TAU.newVal + + def.symbol("ln") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.ln.newVal + + def.symbol("log2") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.log2.newVal + + def.symbol("log10") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.log10.newVal + + def.symbol("pow") do (i: In): + let vals = i.expect("num", "num") + let y = vals[0].getFloat + let x = vals[1].getFloat + i.push x.pow(y).newVal + + def.symbol("sqrt") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.sqrt.newVal + + def.symbol("sin") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.sin.newVal + + def.symbol("cos") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.cos.newVal + + def.symbol("tan") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.tan.newVal + + def.symbol("sinh") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.sinh.newVal + + def.symbol("cosh") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.cosh.newVal + + def.symbol("tanh") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.tanh.newVal + + def.symbol("asin") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.arcsin.newVal + + def.symbol("acos") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.arccos.newVal + + def.symbol("atan") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.arctan.newVal + + def.symbol("d2r") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.degToRad.newVal + + def.symbol("r2g") do (i: In): + let vals = i.expect("num") + i.push vals[0].getFloat.radToDeg.newVal + + def.finalize("math")
D lib/min_trig.nim

@@ -1,104 +0,0 @@

-import - tables, - math -import - ../core/parser, - ../core/value, - ../core/interpreter, - ../core/utils - - # Trigonometry - -proc trig_module*(i: In)= - - let def = i.define() - - def.symbol("sin") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(sin(a.intVal.float)) - else: - i.push newVal(sin(a.floatVal)) - - def.symbol("cos") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(cos(a.intVal.float)) - else: - i.push newVal(cos(a.floatVal)) - - def.symbol("tan") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(tan(a.intVal.float)) - else: - i.push newVal(tan(a.floatVal)) - - def.symbol("sinh") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(sinh(a.intVal.float)) - else: - i.push newVal(sinh(a.floatVal)) - - def.symbol("cosh") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(cosh(a.intVal.float)) - else: - i.push newVal(cosh(a.floatVal)) - - def.symbol("tanh") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(tanh(a.intVal.float)) - else: - i.push newVal(tanh(a.floatVal)) - - def.symbol("asin") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(arcsin(a.intVal.float)) - else: - i.push newVal(arcsin(a.floatVal)) - - def.symbol("acos") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(arccos(a.intVal.float)) - else: - i.push newVal(arccos(a.floatVal)) - - def.symbol("atan") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(arctan(a.intVal.float)) - else: - i.push newVal(arctan(a.floatVal)) - - def.symbol("dtr") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(degToRad(a.intVal.float)) - else: - i.push newVal(degToRad(a.floatVal)) - - def.symbol("rtg") do (i: In): - let vals = i.expect("num") - let a = vals[0] - if a.isInt: - i.push newVal(radToDeg(a.intVal.float)) - else: - i.push newVal(radToDeg(a.floatVal)) - - def.finalize("trig")
M min.nimmin.nim

@@ -27,11 +27,11 @@ lib/min_logic,

lib/min_time, lib/min_io, lib/min_sys, - lib/min_fs, - lib/min_trig + lib/min_fs when not defined(lite): import lib/min_crypto + import lib/min_math export parser,

@@ -125,9 +125,9 @@ i.str_module

i.sys_module i.time_module i.fs_module - i.trig_module when not defined(lite): i.crypto_module + i.math_module i.eval PRELUDE, "<prelude>" i.eval MINRC.readFile() i.eval "\"prompt\" unseal"
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 ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ -inf ROOT aes all? and any? append apply args ask atime bind bool boolean? call call! capitalize case cd chmod choose clear-stack cleave column-print concat confirm cons cp cpu crypto ctime datetime ddel debug decode define defined? delete dget dhas? dkeys dictionary? dip dir? dirname div dpick dprint dprint! dset dsort dup dvalues encode env? error eval even? exists? exit expect 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 indexof inf info insert int integer? interpolate interval io join keep last 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 nan newline nip not notice now num number? odd? opts 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 tap tap! tformat time timeinfo times timestamp titleize to-json to-timestamp try dequote uppercase unzip version warn when which while with xor zip +syntax keyword minDefaultSymbol ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ -inf ROOT acos aes all? and any? append apply args asin ask atan atime bind bool boolean? call call! capitalize case cd ceil chmod choose clear-stack cleave column-print concat confirm cons cos cosh cp cpu crypto ctime d2r datetime ddel debug decode define defined? delete dget dhas? dkeys dictionary? dip dir? dirname div dpick dprint dprint! dset dsort dup dvalues e encode env? error eval even? exists? exit expect fappend fatal find file? filename filter first flatten float float? floor 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 indexof inf info insert int integer? interpolate interval io join keep last length linrec ln load load-symbol log2 log10 logic loglevel loglevel? lowercase ls ls-r map map-reduce match math md5 mkdir mod module module-symbols module-sigils mtime mv nan newline nip not notice now num number? odd? opts os over partition password pi pick pop popd pow pred prepend print print! prompt publish puts puts! put-env q quotation? quote quote-bind quote-define r2d random raise reduce regex reject remove remove-symbol repeat replace rest reverse rm rmdir round run save-symbol scope scope? seal search seq set set-stack sha1 sha224 sha256 sha384 sha512 shorten sigils sin sinh sip size sleep slice sort source split spread sqrt stack startup stored-symbols str string string? strip succ sum swap swons symbols symlink symlink? sys system take tan tanh tap tap! tau tformat time timeinfo times timestamp titleize to-json to-timestamp try trunc dequote uppercase unzip version warn when which while with xor zip syntax match minDefaultSigil ;\<[:@'~!?$%&$=<>#^*#+/]; contained syntax match minSpecialSymbols ;[:@'~!?$%&$=<>#^*#+/]; contained
M prelude.minprelude.min

@@ -9,4 +9,4 @@ 'seq import

'time import 'fs import 'crypto import -'trig import +'math import
M tests/all.mintests/all.min

@@ -19,6 +19,7 @@ 'sys load

'time load 'fs load 'crypto load +'math load "=" 70 repeat puts! "Total Failures: $1" (failures) => % puts! failures exit
A tests/math.min

@@ -0,0 +1,33 @@

+'test load +'test import + +"math" describe + + (0 d2r sin 0 ==) assert + + (30 d2r sin 0.5 ==) assert + + (45 d2r sin 2 sqrt 2 / ==) assert + + (60 d2r sin 3 sqrt 2 / ==) assert + + (90 d2r sin 1 ==) assert + + (15 d2r sin 75 d2r cos ==) assert + + (15 cos 2 pow 15 sin 2 pow + 1 ==) assert + + (pi 4 / tan 1 ==) assert + + (e 3 pow ln 3 ==) assert + + (pi floor 3 ==) assert + + (pi 2 round 3.14 ==) assert + + (pi ceil 4 ==) assert + + (pi trunc 3 ==) assert + + report + clear-stack