all repos — min @ 5588d7e892dfa37b430ad117f27903aed0c32ae7

A small but practical concatenative programming language.

Merge branch 'PMunch-fpimprovements'
h3rald h3rald@h3rald.com
Fri, 27 Oct 2017 20:20:03 +0200
commit

5588d7e892dfa37b430ad117f27903aed0c32ae7

parent

306bee26b25e9598e90cb34a32dc2b5a3318e63d

M core/utils.nimcore/utils.nim

@@ -254,5 +254,5 @@

proc reqTwoSimilarTypesNonSymbol*(i: var MinInterpreter, a, b: var MinValue) = a = i.pop b = i.pop - if not ((a.kind == a.kind or (a.isNumber and a.isNumber)) and not a.isSymbol): + if not ((a.kind == b.kind or (a.isNumber and b.isNumber)) and not a.isSymbol): raiseInvalid("Two non-symbol values of similar type are required on the stack")
M lib/min_logic.nimlib/min_logic.nim

@@ -1,5 +1,6 @@

import - tables + tables, + math import ../core/parser, ../core/value,

@@ -8,6 +9,28 @@ ../core/utils

# Comparison operators +proc floatCompare(n1, n2: MinValue): bool = + let + a:float = if n1.kind != minFloat: n1.intVal.float else: n1.floatVal + b:float = if n2.kind != minFloat: n2.intVal.float else: n2.floatVal + if a.classify == fcNan and b.classify == fcNan: + return true + else: + const + FLOAT_MIN_NORMAL = 2e-1022 + FLOAT_MAX_VALUE = (2-2e-52)*2e1023 + epsilon = 0.00001 + let + absA = abs(a) + absB = abs(b) + diff = abs(a - b) + + if a == b: + return true + elif a == 0 or b == 0 or diff < FLOAT_MIN_NORMAL: + return diff < (epsilon * FLOAT_MIN_NORMAL) + else: + return diff / min((absA + absB), FLOAT_MAX_VALUE) < epsilon proc logic_module*(i: In)= let def = i.define()

@@ -34,13 +57,13 @@ if n1.isNumber and n2.isNumber:

if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: - i.push newVal(n1.intVal.float >= n2.floatVal) + i.push newVal(n1.intVal.float > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isFloat: - i.push newVal(n1.floatVal >= n2.floatVal) + i.push newVal(n1.floatVal > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isInt: - i.push newVal(n1.floatVal >= n2.intVal.float) + i.push newVal(n1.floatVal > n2.intVal.float or floatCompare(n1, n2)) else: - i.push newVal(n1.strVal >= n2.strVal) + i.push newVal(n1.strVal >= n2.strVal) def.symbol("<") do (i: In): var n1, n2: MinValue

@@ -64,22 +87,27 @@ if n1.isNumber and n2.isNumber:

if n1.isInt and n2.isInt: i.push newVal(n1.intVal >= n2.intVal) elif n1.isInt and n2.isFloat: - i.push newVal(n1.intVal.float >= n2.floatVal) + i.push newVal(n1.intVal.float > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isFloat: - i.push newVal(n1.floatVal >= n2.floatVal) + i.push newVal(n1.floatVal > n2.floatVal or floatCompare(n1, n2)) elif n1.isFloat and n2.isInt: - i.push newVal(n1.floatVal >= n2.intVal.float) + i.push newVal(n1.floatVal > n2.intVal.float or floatCompare(n1, n2)) else: i.push newVal(n1.strVal >= n2.strVal) def.symbol("==") do (i: In): var n1, n2: MinValue i.reqTwoSimilarTypesNonSymbol n2, n1 - i.push newVal(n1 == n2) + if n1.kind == minFloat or n2.kind == minFloat: + i.push newVal(floatCompare(n1, n2)) + else: + i.push newVal(n1 == n2) def.symbol("!=") do (i: In): var n1, n2: MinValue i.reqTwoSimilarTypesNonSymbol n2, n1 + if n1.kind == minFloat or n2.kind == minFloat: + i.push newVal(not floatCompare(n1, n2)) i.push newVal(not (n1 == n2)) # Boolean Logic
M lib/min_num.nimlib/min_num.nim

@@ -12,6 +12,15 @@

proc num_module*(i: In)= let def = i.define() + + def.symbol("nan") do (i: In): + i.push newVal(NaN) + + def.symbol("inf") do (i: In): + i.push newVal(Inf) + + def.symbol("-inf") do (i: In): + i.push newVal(NegInf) def.symbol("+") do (i: In): let vals = i.expect("num", "num")
M min.vimmin.vim

@@ -11,8 +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 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 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 newline nip not notice now num number? odd? opts or 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 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 match minDefaultSigil ;\<[:@'~!?$%&$=<>#^*#+/]; contained syntax match minSpecialSymbols ;[:@'~!?$%&$=<>#^*#+/]; contained
M site/contents/_includes/_reference-num.mdsite/contents/_includes/_reference-num.md

@@ -7,6 +7,9 @@

{#op||-||{{n1}} {{n2}}||{{n3}}|| Subtracts {{n2}} from {{n1}}. #} +{#op||-inf||{{null}}||{{n}}|| +Returns negative infinity. #} + {#op||\*||{{n1}} {{n2}}||{{n3}}|| Multiplies {{n1}} by {{n2}}. #}

@@ -19,8 +22,14 @@

{#op||div||{{i1}} {{i2}}||{{i3}}|| Divides {{i1}} by {{i2}} (integer division). #} +{#op||inf||{{null}}||{{n}}|| +Returns infinity. #} + {#op||mod||{{i1}} {{i2}}||{{i3}}|| Returns the integer module of {{i1}} divided by {{i2}}. #} + +{#op||nan||{{null}}||nan|| +Returns **NaN** (not a number). #} {#op||odd?||{{i}}||{{b}}|| Returns {{t}} if {{i}} is odd, {{f}} otherwise. #}
M tests/logic.mintests/logic.min

@@ -132,5 +132,10 @@ (true quotation? false ==) assert

(false quotation? false ==) assert (("a" 2 c) quotation?) assert + (7 0 / inf ==) assert + (-7 0 / -inf ==) assert + (0 0 / nan ==) assert + (10 3 / 3.33333 ==) assert + report clear-stack