all repos — min @ c47b116b60f04c05cc1499c31a440988e0eb3f7e

A small but practical concatenative programming language.

Implemented very basic from-yaml and to-yaml operators.
h3rald h3rald@h3rald.com
Wed, 02 Dec 2020 20:20:52 +0100
commit

c47b116b60f04c05cc1499c31a440988e0eb3f7e

parent

37be4c8071e4c7a5dde4c3902299053abb432342

2 files changed, 48 insertions(+), 4 deletions(-)

jump to
M lib/min_lang.nimlib/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 site/contents/reference-lang.mdsite/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}}.#}

@@ -379,8 +386,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.