Skip to content

Declaration of custom local variables #32

@x87

Description

@x87

Background

Currently it's possible to assign a custom name to the local variable using CONST..END:

const
  x = 0@
end

then declare a type for this variable using VAR..END or the shorter form of declaration:

int x = 5

Proposal

Get rid of the const declaration step when the shorter form of declaration is used, i.e. given an expression like:

int x = 5

the compiler should assign x to the first available local variable index (0) and compile it as 0@ = 5. then if another variable gets declared (e.g. int y = 6) it gets assigned to the next index (1), and so on:

int x   // 0@
int y   // 1@
x = y   // 0085: 0@ = 1@

Known Limitations

  • the compiler is yet unable to detect if the opcode belongs to a particular script in the MAIN section (it's possible that some code in the middle of a script belongs to another script)
create_thread @a
create_thread @b

:a
int x = 0  // 0@ = 0
jump @a1

:b1
int x = 0 // 1@ = 0 error, should be 0@
end_thread

:b
jump @b1

:a1
end_thread
  • there is no look-ahead for local variables so it's not possible to identify if a particular index has already been used by the scripter or not.

Conclusion

Given the limitations, for the first iteration of this feature:

  • only allow the local variable declaration in a CLEO script

  • always assign the first declared variable to 0@, the second one to 1@, etc. The compiler would still validate if the number of defined variables exceeds the limit for the script

  • it's possible to use indexed local variables (0@, 1@, etc) and they may possibly clash with declared variables:

int x = 0
0@ = 1
wait x // wait 1
  • the compiler would throw an error if both forms of local variables are used. the first used form controls (i.e. if you have 0@ in the script first you would be unable to use custom variables as described above, and vice versa)

  • the documentation should clearly state that the order of variables assignment is undetermined and may change in the future (i.e. you should not assume that the first custom variable is always 0@)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions