moor icon indicating copy to clipboard operation
moor copied to clipboard

Language enhancement: Introduce lexical scopes to the MOO language:

Open github-actions[bot] opened this issue 1 year ago • 2 comments

add a 'with' keyword to the language which introduces a new scope, similar to ML's "let":

with x = 1 in

...

endlet

Multiple variables can be introduced at once:

with x = 1, y = 2 in ...

Variables not declared with 'with' are verb-scoped as they are now

'with' variables that shadow already-known verb-scoped variables override the verb-scope

Add LetBegin and LetEnd opcodes to the language.

Make the environment have a width, and expand and contract as scopes are entered and exited.

Likewise, Names in Program should be scope delimited somehow

/ The values of the variables currently in scope, by their offset.

https://github.com/rdaum/moor/blob/9922628e2a0cc8e4b73952a3a0a4dd185aab1daf/crates/kernel/src/vm/activation.rs#L71


    pub(crate) program: Program,
    /// The program counter.
    pub(crate) pc: usize,
    // TODO: Language enhancement: Introduce lexical scopes to the MOO language:
    //      add a 'with' keyword to the language which introduces a new scope, similar to ML's "let":
    //              with x = 1 in
    //                     ...
    //              endlet
    //      Multiple variables can be introduced at once:
    //              with x = 1, y = 2 in ...
    //      Variables not declared with 'with' are verb-scoped as they are now
    //      'with' variables that shadow already-known verb-scoped variables override the verb-scope
    //      Add LetBegin and LetEnd opcodes to the language.
    //      Make the environment have a width, and expand and contract as scopes are entered and exited.
    //      Likewise, Names in Program should be scope delimited somehow
    /// The values of the variables currently in scope, by their offset.
    pub(crate) environment: BitArray<Var, 256, Bitset16<16>>,
    /// The value stack.

github-actions[bot] avatar Feb 11 '24 15:02 github-actions[bot]

Actual syntax is more conservative.

begin
  let a = 5;
  const b = 3;
  global c = 2;
  implicit_global = 1;
end

rdaum avatar Aug 12 '24 21:08 rdaum

Remaining work here is:

  • ~~add let as an option for scatter assignment operations so that scatter-assigns are declared lexically scoped~~
  • lexically scoped iteration variables for for; something like for let x in ...
  • a CLI option to disable implicit global variables entirely (would break back-compat with LambdaMOO cores entirely)

rdaum avatar Aug 14 '24 01:08 rdaum