Skip to content

Add new opcodes with pointer functionality #3154

@DigitalCodeCrafter

Description

@DigitalCodeCrafter

kOS currently has no built-in mechanism for general pointer-like or indirect data access.
The only workaround is wrapping data inside lists and manually passing references, which is limited and cannot refer to stack positions or nested data structures.

By adding a PointerValue and 3 new opcodes, simple pointer functionality can be achieved:

Proposed new opcodes

  • CREATEPTR n: constructs a pointer from n stack elements
  • GETPTR: resolves a pointer and pushes its resolution
  • SETPTR: writes a value through a pointer

Pointer consist of a list of segments:

  • First segment:
    • scalar int: absolute stack index
    • $ prefixed string: local variable name
  • Remaining segments: keys / indices for nested structures (lists, lexicons, etc.)

This enables pointers to reference:

  • Stack values
  • variables dynamically by name
  • list elements
  • nested data structures of arbitrary depth

Optional (quality-of-life):

  • ALLOCATE n: advances the stack pointer by pushing a known number of nulls.
  • FREE n: quickly pops a known number of elements from the stack.
  • STACKPTR: pushes the current stack pointer (stack length).
  • INSTPTR: pushes the current instruction pointer.

All proposed opcodes are fully backwards-compatible and inert unless explicitly used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions