HPInc / HP-Digital-Microfluidics

HP Digital Microfluidics Software Platform and Libraries
MIT License
2 stars 0 forks source link

Add `with` statements/expressions to macro language #234

Open EvanKirshenbaum opened 5 months ago

EvanKirshenbaum commented 5 months ago

It would be useful for macro writers to have an equivalent of LET (actually, probably LET*) expressions. Something like

   with x = ...,
           y = ...
   {
      // use `x` and `y`
   }

with x and y local to the block. I think that the least astonishing semantics is to have the variable declared sequentially (so that each can see the earlier value), allowing you to say things like

   with p = // big long expression
           r = p's row
   { ... }

when you're really only interested in the last one.

Also, as with current declarations, the initial value should be in the environment without the declaration, allowing you to do dynamic binding by saying

   with x = x + 1 { ...}

I think that, as with macros (https://github.com/HPInc/HP-Digital-Microfluidics/issues/160) [comment by @EvanKirshenbaum on Jan 20, 2023 at 3:19 PM PST], I probably want to allow both a statement form (taking a block) and an expression form, separating the header from the body by a colon.

Migrated from internal repository. Originally created by @EvanKirshenbaum on Feb 04, 2023 at 3:26 PM PST.
EvanKirshenbaum commented 5 months ago

It dawned on me last night that in order to really do dynamic binding, you need to have a form that doesn't introduce a new binding, but rather performs an assignment and then undoes the assignment on exit. I'd propose adding a keyword such as temporary or dynamic or existing, to allow you to say

with existing x = x + 1 { ... }

This sets the visible x, which may be global or otherwise visible to dynamic calls.

Thinking about it, my initial choice of temporary is probably bad, because people will assume they can say temp, which will be read as temperature, violating the PLA. dynamic is too esoteric, so I'd go with existing.

An alternative would be to say that if you provide a type, it's a new declaration, and if you don't, it's rebinding (and then restoring) the original. That may be more consistent with the rules for declarations and assignments. As with declarations, you can add local if necessary to say "infer the type, but create a new variable".

Migrated from internal repository. Originally created by @EvanKirshenbaum on Feb 11, 2023 at 11:37 AM PST.