Merge branch 'master' of github.com:h3rald/min
h3rald h3rald@h3rald.com
Thu, 03 Dec 2020 06:02:49 +0000
6 files changed,
73 insertions(+),
7 deletions(-)
M
lib/min_lang.nim
→
lib/min_lang.nim
@@ -73,10 +73,40 @@
def.symbol("lite?") do (i: In): i.push defined(lite).newVal + def.symbol("from-yaml") do (i: In): + let vals = i.expect("string") + let s = vals[0] + try: + var dict = newDict(i.scope) + let lines = s.strVal.split("\n") + for line in lines: + let pair = line.split(":") + i.dset(dict, pair[0].strip, pair[1].strip.newVal) + i.push(dict) + except: + raiseInvalid("Invalid/unsupported YAML object (only dictionaries with string values are supported)") + def.symbol("from-json") do (i: In): let vals = i.expect("string") let s = vals[0] i.push i.fromJson(s.getString.parseJson) + + def.symbol("to-yaml") do (i: In): + let vals = i.expect "a" + let a = vals[0] + let err = "YAML conversion is only supported from dictionaries with string values" + if a.kind != minDictionary: + raiseInvalid(err) + var yaml = "" + try: + for key in i.keys(a).qVal: + let value = i.dget(a, key) + if value.kind != minString: + raiseInvalid(err) + yaml &= "$1: $2\n" % [key.strVal, value.strVal] + i.push(yaml.strip.newVal) + except: + raiseInvalid(err) def.symbol("to-json") do (i: In): let vals = i.expect "a"@@ -681,7 +711,7 @@ else:
raiseInvalid("Cannot convert a quotation to float.") def.symbol("prompt") do (i: In): - i.eval(""""[$1]$$ " (.) => %""") + i.eval(""""[$1]\n$$ " (.) => %""") # Sigils
M
lib/min_str.nim
→
lib/min_str.nim
@@ -22,6 +22,10 @@ strings.add $$el
let res = s.strVal % strings i.push res.newVal + def.symbol("apply-interpolate") do (i: In): + i.push "apply".newSym + i.push "interpolate".newSym + def.symbol("strip") do (i: In): let vals = i.expect("'sym") let s = vals[0]@@ -152,5 +156,8 @@ i.push("regex".newSym)
def.symbol("%") do (i: In): i.push("interpolate".newSym) + + def.symbol("=%") do (i: In): + i.push("apply-interpolate".newSym) def.finalize("str")
M
site/contents/reference-lang.md
→
site/contents/reference-lang.md
@@ -149,8 +149,15 @@ > > `{"MyError" :error "This is a test error" :message} 'error set-type format-error`
> > > > produces: `"This is a test error"`#} -{#op||from-json||{{s}}||{{a0p}}|| +{#op||from-json||{{s}}||{{any}}|| Converts a JSON string into {{m}} data.#} + +{#op||from-yaml||{{s}}||{{any}}|| +> Converts a YAML string into {{m}} data. +> > %note% +> > Note +> > +> > At present, only YAML objects containing string values are supported.#} {#op||if||{{q1}} {{q2}} {{q3}}||{{a0p}}|| If {{q1}} evaluates to {{t}} then evaluates {{q2}}, otherwise evaluates {{q3}}.#}@@ -380,8 +387,15 @@
{#op||times||{{q}} {{i}}||{{a0p}}|| Applies the quotation {{q}} {{i}} times.#} -{#op||to-json||{{q}}||{{s}}|| -Converts {{q}} into a JSON string {{s}}.#} +{#op||to-json||{{any}}||{{s}}|| +Converts {{any}} into a JSON string.#} + +{#op||to-yaml||{{any}}||{{s}}|| +> Converts {{any}} into a YAML string. +> > %note% +> > Note +> > +> > At present, only {{m}} dictionaries containing string values are supported.#} {#op||try||({{q1}} {{q}}{{2}}{{01}} {{q}}{{3}}{{01}})||{{a0p}}|| > Evaluates a quotation as a try/catch/finally block.
M
site/contents/reference-str.md
→
site/contents/reference-str.md
@@ -6,7 +6,12 @@ {@ _defs_.md || 0 @}
{#alias||%||interpolate#} +{#alias||=%||apply-interpolate#} + {#alias||=~||regex#} + +{#op||apply-interpolate||{{s}} {{q}}||{{s}}|| +The same as pushing `apply` and then `interpolate` on the stack.#} {#op||capitalize||{{sl}}||{{s}}|| Returns a copy of {{sl}} with the first character capitalized.#}@@ -23,9 +28,11 @@
{#op||interpolate||{{s}} {{q}}||{{s}}|| > Substitutes the placeholders included in {{s}} with the values in {{q}}. > > %note% -> > Note +> > Notes > > -> > If {{q}} contains symbols or quotations, they are not interpreted. To do so, call `apply` before interpolating. +> > * If {{q}} contains symbols or quotations, they are not interpreted. To do so, call `apply` before interpolating or use `apply-interpolate` instead. +> > * You can use the `$#` placeholder to indicate the next placeholder that has not been already referenced in the string. +> > * You can use named placeholders like `$pwd`, but in this case {{q}} must contain a quotation containing both the placeholder names (odd items) and the values (even items). > > > %sidebar% > > Example@@ -74,7 +81,9 @@ > > The following:
> > > > `"This is a stupid test. Is it really a stupid test?" " s[a-z]+" " simple" replace` > > -> > produces: `"This is a simple test. Is it really a simple test?"`#} +> > produces: +> > +> > `"This is a simple test. Is it really a simple test?"`#} {#op||regex||{{s1}} {{s2}}||{{q}}|| > Performs a search and/or a search-and-replace operation using pattern {{s2}}.
M
tests/lang.min
→
tests/lang.min
@@ -220,6 +220,10 @@ ({3 :x 5 :y} "point" set-type 'point type?) assert
({} :myscope (2 :two 3 :three scope @myscope) -> myscope scope-symbols ("three" "two") ==) assert + ((2 < 3 and (4 > 2)) >< true) assert + + ((float 3) >> 3.0 ==) assert + report ; Tidy up clear-stack
M
tests/str.min
→
tests/str.min
@@ -53,5 +53,7 @@ ("test" 4 indent " test" ==) assert
((1 3 "test") ", " join "1, 3, test" ==) assert + ("PWD: $pwd" ("pwd" .) =% ("PWD: " .) => "" join ==) assert + report clear-stack