Skip to content

Scoped local variables in SCM scripts RFC #284

Open

Description

@MiranDMC

Please introduce scope block concept for named local variables.

Idea is to add new keyword scope which would be used in pair with end to create blocks (same as for example const).
Block like that would result in reset of used local variable counter value used to allocating named local variable indexes.
Scope should also make previously allocated local variable names not accessible.
It should not be possible to nest scope blocks. This will create two possible states: script global scope, or local scope.

int a // 0@
int b = 4 // 1@
int c = // 2@

scope
    int d // now 0@ instead of 3@
    int e = b // error, b is unknown identifier
    int a = 1 // 1@ - allowed 'a' is new identifier
end

Scope block should not affect global $ variables.
Scope block should not affect local @ variables with indexes <0 and >31 (future virtual variables: #270)

This feature would enable us to use named local variables within cleo_call [0AB1] functions, which actually implements concept of current scope in term of storing-restoring all local variables along with scope traversing via call-return opcodes.

scope
    float a, b, c // input arguments
    :FUNC
        float d
        d = a
        d += b
    cleo_return 1 d
end

If implemented this way it wil even work correctly with nested cleo calls.

This feature will be also reused in future functions feature #263, as function keyword will inherit (and extend?) behaviour of scope.

Activity

  1. changed the title [-]Scoped local variables[/-] [+]Scoped local variables RFC[/+] on Nov 2, 2023
  2. x87 commented on Nov 2, 2023

    @x87
    Collaborator
  3. MiranDMC commented on Nov 16, 2023

    @MiranDMC
    Author

    Scope block will be also useful for enclosing code of missions and script sub-threads (create script from label), as these two spawn their own set of local variables.

  4. changed the title [-]Scoped local variables RFC[/-] [+]Scoped local variables in SCM scripts RFC[/+] on Jan 12, 2024
  5. x87 commented on Feb 7, 2024

    @x87
    Collaborator

    4.0.0-beta.5 allows declaration of custom variables in functions in main.scm

  6. x87 commented on Apr 21, 2024

    @x87
    Collaborator

    4.0.0-beta.8 allows declaration of custom variables in missions and external scripts.

    the only remaining place where custom variables are not available now is a non-function code in MAIN section of main.scm

  7. x87 commented on Dec 8, 2024

    @x87
    Collaborator

    Custom variables in SCM missions should be allocated starting from 34@. This is how it's done for .CM (CLEO Missions)

  8. x87 commented on Mar 16, 2025

    @x87
    Collaborator

    Custom variables in SCM missions should be allocated starting from 34@. This is how it's done for .CM (CLEO Missions)

    Implemented in 4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:p3scope:compilerscope:languagetype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions