all repos — min @ 350b3490dcbcfd1e9a739830391392dca72c26cf

A small but practical concatenative programming language.

Clarified usage of lambda capturing in output values
h3rald h3rald@h3rald.com
Tue, 02 Feb 2021 03:58:34 +0000
commit

350b3490dcbcfd1e9a739830391392dca72c26cf

parent

89db4c3f5d51f3abb9a89e3583dac416a1d18646

3 files changed, 61 insertions(+), 5 deletions(-)

jump to
M minpkg/lib/min_lang.nimminpkg/lib/min_lang.nim

@@ -198,6 +198,7 @@ var inExpects= newSeq[string](0)

var inVars = newSeq[string](0) var outExpects= newSeq[string](0) var outVars = newSeq[string](0) + var rawOutVars = newSeq[string](0) var generics: CritBitTree[string] var origGenerics: CritBitTree[string] var o = false

@@ -239,11 +240,16 @@ raiseInvalid("typeclasses can only have one input value")

inExpects.add v else: if v[0] != ':' and v[0] != '^': - raiseInvalid("No mapping symbol specified in signature at position $#" % $(c+1)) + raiseInvalid("No capturing symbol specified in signature at position $#" % $(c+1)) else: if o: + if v[0] == '^' and outExpects[outExpects.len-1] != "quot": + raiseInvalid("Only quotations can be captured to a lambda, found $# instead at position $#" % [outExpects[outExpects.len-1], $(c+1)]) + rawOutVars.add v outVars.add v[1..v.len-1] else: + if v[0] == '^': + raiseInvalid("A lambda capturing symbol was specified in signature at position $#. Lambda capturing symbols are only allowed for output values" % $(c+1)) inVars.add v[1..v.len-1] c.inc() if not o:

@@ -286,9 +292,9 @@ except MinReturnException:

discard # Validate output for k in 0..outVars.len-1: - #i.pushSym outVars[k] - #let x = i.peek - let x = i.scope.symbols[outVars[k]].val + var x = i.scope.symbols[outVars[k]].val + if rawOutVars[k][0] == ':': + x = x.qVal[0] if t == "constructor": x.objType = n let o = outExpects[k]

@@ -302,7 +308,6 @@ break

else: r = i.validate(x, o, generics) if not r: - discard i.pop var tp = o if generics.hasKey(o): tp = generics[o]
M next-release.mdnext-release.md

@@ -1,2 +1,29 @@

+ +### Fixes + * Added `===` at the end of integrated help descriptions (#127). + +### Mew additions + * New symbol: [parent-scope](https://min-lang.org/reference-lang/#op-parent-scope) (#117). + +### Notable changes + +#### Lambda capturing in operator output values + +You can now specify a lambda to be captured to an output value, like this: + + ( + symbol square + (==> quot ^o) + ( + (dup *) ~o + ) + ) :: + +Essentially, this allows you to push a lambda on the stack from an operator. + +Note that: +* Lambdas must be captured using the `^` sigil in signatures and bound using `lambda-bind` in the operator body. +* Lambdas cannot be captured in input values (they have already been pushed on the stack). +* Requiring a lambda as an output value effectively bypasses stack pollution checks. While this can be useful at times, use with caution!
M site/contents/learn-operators.mdsite/contents/learn-operators.md

@@ -48,6 +48,11 @@ * have no built-in validation of input and output values.

* do not support the `return` symbol to immediately end their execution. * have no built-in stack pollution checks. +> %tip +> Tip +> +> You can use {#link-operator||lang||lambda-bind#} to re-set a previously set lambda. + ## Sigils Besides symbols, you can also define sigils. min provides a set of predefined _sigils_ as abbreviations for for commonly-used symbols.

@@ -172,6 +177,25 @@ > %tip%

> Tip > > `typeclass:`-prefixed symbols are just like ordinary shmbols: they are lexically scoped, they can be sealed, unsealed and deleted. + +#### Capturing lambdas + +You can also specify a lambda to be captured to an output value, like this: + + ( + symbol square + (==> quot ^o) + ( + (dup *) ~o + ) + ) :: + +Essentially, this allows you to push a lambda on the stack from an operator. + +Note that: +* Lambdas must be captured using the `^` sigil in signatures and bound using {#link-operator||lang||lambda-bind#} in the operator body. +* Lambdas cannot be captured in input values (they have already been pushed on the stack). +* Requiring a lambda as an output value effectively bypasses stack pollution checks. While this can be useful at times, use with caution! ### Generics