all repos — min @ a5bd677dfba859aa0eac0e3ddac3a6751d977e66

A small but practical concatenative programming language.

Enhanced invoke operator to work on nested dictionaries.
h3rald h3rald@h3rald.com
Thu, 24 Dec 2020 20:32:48 +0100
commit

a5bd677dfba859aa0eac0e3ddac3a6751d977e66

parent

682c0d05f1ebfbb832ffe7dc7fe4cbd0f65c1e4d

4 files changed, 27 insertions(+), 13 deletions(-)

jump to
M lib/min_lang.nimlib/min_lang.nim

@@ -454,18 +454,20 @@ let s = vals[0].getString

let parts = s.split("/") if parts.len < 2: raiseInvalid("Dictionary identifier not specified") - let mdlId = parts[0] - let symId = parts[1] + var mdlId = parts[0] i.apply i.scope.getSymbol(mdlId) - let mdl = i.pop - if mdl.kind != minDictionary: - raiseInvalid("'$#' is not a dictionary" % mdlId) - var sym: MinValue - try: - sym = i.dget(mdl, symId) - except: - raiseInvalid("Symbol '$#' not found in dictionary '$#'" % [symId, mdlId]) - i.dequote sym + for p in 0..parts.len-2: + let mdl = i.pop + if not mdl.isDictionary: + raiseInvalid("'$#' is not a dictionary" % mdlId) + let symId = parts[p+1] + var sym: MinValue + try: + sym = i.dget(mdl, symId) + except: + raiseInvalid("Symbol '$#' not found in dictionary '$#'" % [symId, mdlId]) + mdlId = symId + i.dequote sym def.symbol("set-type") do (i: In): let vals = i.expect("'sym", "dict")
M next-release.mdnext-release.md

@@ -1,7 +1,7 @@

* Added **encode-url**, **decode-url**, **parse-url** symbols. * Added **dstore** module providing support for simple, persistent, in-memory JSON stores. * Added **require** symbol to read a min file and automatically create a module containing all symbols defined in the file. -* Added **invoke** symbol (and **\*** sigil) to easily call a symbol defined in a module or dictionary, e.g. `*mymodule/mymethod`. +* Added **invoke** symbol (and **\*** sigil) to easily call a symbol defined in a module or dictionary, e.g. `*mymodule/mymethod`, or `*dict1/a/b`. * Fixed library installation via nimble * Fixed error handling and stack trace for **start-server** symbol. * Added the possibility to bundle assets in a compiled min program by specifying tbe **-a** (or **--asset-path**) option.
M site/contents/reference-lang.mdsite/contents/reference-lang.md

@@ -215,7 +215,15 @@ > * 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||invoke||{{sl}}||{{a0p}}|| -Assming that {{sl}} is a formatted like *dictionary*/*symbol*, calls *symbol* defined in *dictionary*. #} +> Assming that {{sl}} is a formatted like *dictionary*/*symbol*, calls *symbol* defined in *dictionary* (note that this also works for nested dictionaries. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `100` on the stack: +> > +> > {{100 :b} :a} :test *test/a/b + #} {#op||linrec||{{q1}} {{q2}} {{q3}} {{q4}}||{{a0p}}|| > Implements linear recursions as follows:
M tests/lang.mintests/lang.min

@@ -229,6 +229,10 @@ ("x" prefix) 'x define-sigil

'x unseal-sigil 'x delete-sigil 'x defined-sigil? false == ) assert + + ( + {{100 :b} :a} :test *test/a/b 100 == + ) assert report ; Tidy up