ursalang / ursa

A friendly, stable general-purpose programming language
https://ursalang.github.io
3 stars 1 forks source link

Alistair's v0.3 feature requests #12

Open apt1002 opened 9 months ago

apt1002 commented 9 months ago

A short list of requests for Ursa v0.3

rrthomas commented 7 months ago

Async is done; generators turn out to be hard. JavaScript already provides async, and I've already made ArkExp.eval async throughout, so adding async to Ursa was just a matter of selectively engaging or avoiding serialization of execution at certain points.

Meanwhile, JavaScript provides generators too, but I can't see how to use them without changing the structure of the Ark interpreter considerably. Currently, Ark uses exceptions to implement return, as each Ursa function call is represented by a stack of JavaScript function calls, because .eval() recurses into the Ark tree. There is no way in JavaScript to resume execution after an exception.

As far as I can tell, the only way to resume execution of a function in JavaScript is to use its generators. To use them for Ursa generators, I would either have to flatten the implementation of .eval() so that it does not make JavaScript function calls except to implement Ursa function calls; or, I would have to manually implement stack unwinding for return, continue and break, and use generators everywhere in the implementation of eval().

There are two options I can think of to implement Ursa generators without JavaScript generators:

  1. Have a way to represent the state of an Ursa computation, including its stack and "program counter". The stack frame is already represented in an ArkState, and could be saved in the yield exception. But the "program counter" is currently encoded in the JavaScript stack, and encoding it explicitly would require Ark to be changed to make it possible to record the state of a partially constructed ArkVal, and the execution location within an ArkExp. This is tricky.
  2. Replace the Ark interpreter with a JavaScript back-end. I was planning to rewrite the current prototype JavaScript backend anyway to translate Ark rather than Ursa directly (so that in future the Ark code can be transformed, as in a grown-up compiler, before target code is output), so this seems the sensible way to go. (This will still necessitate the workaround mentioned in #15 to implement general continuations.)
rrthomas commented 7 months ago

Retargeting algebraic types to version 0.4. See #29.