all repos — min @ 8fdfdf818d7cd7a3999bfa63c2e0888f951b1e75

A small but practical concatenative programming language.

Moved float, int, string and bool to lang module.
h3rald h3rald@h3rald.com
Sun, 04 Jun 2017 16:55:35 +0200
commit

8fdfdf818d7cd7a3999bfa63c2e0888f951b1e75

parent

eb3a75b75b51f523090113c9b3a2a8ba17a5c0a9

M lib/min_lang.nimlib/min_lang.nim

@@ -520,6 +520,56 @@ var q: MinValue

i.reqQuotationOfSymbols q i.push(i.expect(q.qVal.mapIt(it.getString())).newVal(i.scope)) + # Converters + + def.symbol("string") do (i: In): + let s = i.pop + i.push(($$s).newVal) + + def.symbol("bool") do (i: In): + let v = i.pop + let strcheck = (v.isString and (v.getString == "false" or v.getString == "")) + let intcheck = v.isInt and v.intVal == 0 + let floatcheck = v.isFloat and v.floatVal == 0 + let boolcheck = v.isBool and v.boolVal == false + let quotcheck = v.isQuotation and v.qVal.len == 0 + if strcheck or intcheck or floatcheck or boolcheck or quotcheck: + i.push false.newVal + else: + i.push true.newVal + + def.symbol("int") do (i: In): + let s = i.pop + if s.isString: + i.push s.getString.parseInt.newVal + elif s.isFloat: + i.push s.floatVal.int.newVal + elif s.isInt: + i.push s + elif s.isBool: + if s.boolVal == true: + i.push 1.int.newVal + else: + i.push 0.int.newVal + else: + raiseInvalid("Cannot convert a quotation to an integer.") + + def.symbol("float") do (i: In): + let s = i.pop + if s.isString: + i.push s.getString.parseFloat.newVal + elif s.isInt: + i.push s.intVal.float.newVal + elif s.isFloat: + i.push s + elif s.isBool: + if s.boolVal == true: + i.push 1.float.newVal + else: + i.push 0.float.newVal + else: + raiseInvalid("Cannot convert a quotation to float.") + # Sigils def.sigil("'") do (i: In):
M lib/min_str.nimlib/min_str.nim

@@ -80,54 +80,6 @@ let n = vals[0]

let s = vals[1] i.push s.getString.indent(n.intVal).newVal - def.symbol("string") do (i: In): - let s = i.pop - i.push(($$s).newVal) - - def.symbol("bool") do (i: In): - let v = i.pop - let strcheck = (v.isString and (v.getString == "false" or v.getString == "")) - let intcheck = v.isInt and v.intVal == 0 - let floatcheck = v.isFloat and v.floatVal == 0 - let boolcheck = v.isBool and v.boolVal == false - let quotcheck = v.isQuotation and v.qVal.len == 0 - if strcheck or intcheck or floatcheck or boolcheck or quotcheck: - i.push false.newVal - else: - i.push true.newVal - - def.symbol("int") do (i: In): - let s = i.pop - if s.isString: - i.push s.getString.parseInt.newVal - elif s.isFloat: - i.push s.floatVal.int.newVal - elif s.isInt: - i.push s - elif s.isBool: - if s.boolVal == true: - i.push 1.int.newVal - else: - i.push 0.int.newVal - else: - raiseInvalid("Cannot convert a quotation to an integer.") - - def.symbol("float") do (i: In): - let s = i.pop - if s.isString: - i.push s.getString.parseFloat.newVal - elif s.isInt: - i.push s.intVal.float.newVal - elif s.isFloat: - i.push s - elif s.isBool: - if s.boolVal == true: - i.push 1.float.newVal - else: - i.push 0.float.newVal - else: - raiseInvalid("Cannot convert a quotation to float.") - def.symbol("search") do (i: In): let vals = i.expect("string", "string") let reg = vals[0]
M site/contents/_includes/_reference-lang.mdsite/contents/_includes/_reference-lang.md

@@ -42,6 +42,14 @@

{#op||bind||{{any}} {{sl}}||{{null}}|| Binds the specified value (auto-quoted) to an existing symbol {{sl}}.#} +{#op||bool||{{any}}||{{b}}|| +> Converts {{any}} to a boolean value based on the following rules: +> +> * If {{any}} is a boolean value, no conversion is performed. +> * If {{any}} is a non-zero numeric value, it is converted to {{t}}, otherwise it is converted to {{f}}. +> * If {{any}} is a non-empty quotation, it is converted to {{t}}, otherwise it is converted to {{f}}. +> * If {{any}} is a non-empty string or not `"false"`, it is converted to {{t}}, otherwise it is converted to {{f}}.#} + {#op||call||{{q}} {{sl}}||{{a0p}}|| Calls operator {{sl}} defined in scope {{q}}. #}

@@ -95,6 +103,15 @@ > > `(int string num) expect`

> > > > produces: `(1 "test" 3.4)`#} +{#op||float||{{any}}||{{flt}}|| +> Converts {{any}} to an integer value based on the following rules: +> +> * If {{any}} is {{t}}, it is converted to `1.0`. +> * If {{any}} is {{f}}, it is converted to `0.0`. +> * If {{any}} is a integer, it is converted to float value. +> * If {{any}} is a float, no conversion is performed. +> * If {{any}} is a string, it is parsed as a float value.#} + {#op||foreach||{{q1}} {{q2}}||{{a0p}}|| Applies the quotation {{q2}} to each element of {{q1}}.#}

@@ -117,6 +134,15 @@ If {{q1}} evaluates to {{t}} then evaluates {{q2}}, otherwise evaluates {{q3}}.#}

{#op||import||{{sl}}||{{null}}|| Imports the a previously-loaded module {{sl}}, defining all its symbols in the current scope. #} + +{#op||int||{{any}}||{{i}}|| +> Converts {{any}} to an integer value based on the following rules: +> +> * If {{any}} is {{t}}, it is converted to `1`. +> * If {{any}} is {{f}}, it is converted to `0`. +> * If {{any}} is an integer, no conversion is performed. +> * If {{any}} is a float, it is converted to an integer value by truncating its decimal part. +> * If {{any}} is a string, it is parsed as an integer value.#} {#op||linrec||{{q1}} {{q2}} {{q3}} {{q4}}||{{a0p}}|| > Implements linear recursions as follows:

@@ -205,6 +231,9 @@ Display the source code of symbol {{sl}} (if it has been implemented a {{m}} quotation). #}

{#op||stored-symbols||{{null}}||({{s0p}})|| Returns a quotation containing all symbols stored in the [.min\_symbols](class:file) file. #} + +{#op||string||{{any}}||{{s}}|| +Converts {{any}} to its string representation.#} {#op||symbols||{{null}}||({{s0p}})|| Returns a list of all symbols defined in the [ROOT](class:kwd) scope.#}
M tests/lang.mintests/lang.min

@@ -151,6 +151,34 @@ (opts () ==) assert

(args ("tests/all.min") ==) assert + (3 string "3" ==) assert + + ("false" bool false ==) assert + + ("" bool false ==) assert + + (0 bool false ==) assert + + (false bool false ==) assert + + (0.0 bool false ==) assert + + ("something" bool true ==) assert + + ("345" int 345 ==) assert + + (true int 1 ==) assert + + (3.5 int 3 ==) assert + + (3.5 float 3.5 ==) assert + + (3 float 3.0 ==) assert + + (false float 0.0 ==) assert + + ("3.678" float 3.678 ==) assert + report ; Tidy up clear-stack
M tests/str.mintests/str.min

@@ -39,33 +39,5 @@ ("test" 4 indent " test" ==) assert

((1 3 "test") ", " join "1, 3, test" ==) assert - (3 string "3" ==) assert - - ("false" bool false ==) assert - - ("" bool false ==) assert - - (0 bool false ==) assert - - (false bool false ==) assert - - (0.0 bool false ==) assert - - ("something" bool true ==) assert - - ("345" int 345 ==) assert - - (true int 1 ==) assert - - (3.5 int 3 ==) assert - - (3.5 float 3.5 ==) assert - - (3 float 3.0 ==) assert - - (false float 0.0 ==) assert - - ("3.678" float 3.678 ==) assert - report clear-stack