Picolab / pico-engine

An implementation of the pico-engine hosted on node.js
http://picolabs.io/
MIT License
43 stars 8 forks source link

Language change to allow bindings interleaved with actions with `every` compound action #528

Open b1conrad opened 3 years ago

b1conrad commented 3 years ago

Actions and defactions can return a result, which can be bound to a name with a setting clause. Often the result isn't directly useful to subsequent actions in the sequence (in an every compound action).

For one example, the ctx:newChannel built-in action returns the entire channel map

  ctx:newChannel(tags,eventPolicy,queryPolicy) setting(channel)

but what we'll need in subsequent actions will most likely be just the channel ID (aka ECI)

  ctx:delChannel(channel{"id"})

so it would be useful notation to be able to place bindings between actions, like so:

every {
  ctx:newChannel(tags,eventPolicy,queryPolicy) setting(channel)
  eci = channel{"id"}
  ... // including uses of eci
   ctx:delChannel(eci)
}

This makes coding clearer and eliminates repetitions of the sub-expression because it can be bound to a name.

b1conrad commented 3 years ago

Here's a real example, with names rid, eci, and res bound to an expression based on a returned result. Rather than having to repeat the expression every time it is needed, I can just use the name.

The left hand side shows a function and a defaction without this feature. The RHS shows the simpler code that I would like to be able to write in KRL.

Screen Shot 2021-01-09 at 9 38 00 AM

Orange highlighting shows the three times rid is used, yellow for eci used twice. Purple shows how res is used once to make the code clearer by calling out an expression that would otherwise have to be embedded.

b1conrad commented 3 years ago

Matthew says, "#528 seems like a good idea. It'll require changes to the krl-parser and the krl-compiler the runtime probably won't need any changes."