Home /Modules/opcodes/SLOpcodeGroup.sleela

SLOpcodeGroup.sleela

lib/opcodes/running/SLOpcodeGroup.sleela · 139 line(s) · sleela

<< lib/opcodes/running/SLOpcodeGroup.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLOpcodeGroup, a named, ordered group of opcodes that runs
     as a single cohesive unit within a program.
   
   Role: where SLOpcodeStream is the whole verbatim program, a group is a smaller
     labelled run of opcodes that belong together — a loop body, a critical
     section, a protocol handshake, a struct build. A group preserves order
     internally (1..k) and can be run on its own, warmed ahead of time, or folded
     into a stream. Grouping gives a developer a way to reason about and gate
     opcodes in meaningful clusters instead of one flat sequence.
   
   Family: opcodes.running. Native VM/OS work crosses an explicit bridge. >>
#sleela 2.5
material;
< Unified SLeeLa standard-library front-end object. >
class SLOpcodeGroup {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10187;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-05";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  static final int MAX_MEMBERS = 4096;

  < the group's name, e.g. "critical-section" >
  String label;
  < the ordered opcodes in this group >
  SLOpcodeBase[] members;
  int count;
  < how many members have been warmed (pre-armed) >
  int warmed;
  int state;
  String lastError;

  void init(String name) {
    label = name;
    members = new SLOpcodeBase[MAX_MEMBERS];
    count = 0;
    warmed = 0;
    state = 0;
    lastError = "";
  }

  < Append an opcode to the group in order. Fail-closed on bound/inactive op. >
  boolean add(SLOpcodeBase op) {
    if (op == null) { lastError = "null opcode"; return false; }
    if (count >= MAX_MEMBERS) { lastError = "group full"; return false; }
    members[count] = op;
    count = count + 1;
    return true;
  }

  boolean valid() {
    if (count < 1) { lastError = "empty group"; return false; }
    return true;
  }

  void enable() { if (valid()) { state = 1; } }
  void disable() { state = 0; }
  boolean active() { return state == 1 && valid(); }
  int size() { return count; }
  String name() { return label; }

  < Warm the group: pre-arm every member so it is ready to fire without a >
  < per-opcode arming cost at run time. Returns the number warmed. >
  int warm() {
    warmed = 0;
    for (int i = 0; i < count; i = i + 1) {
      if (members[i] != null) { members[i].arm(); warmed = warmed + 1; }
    }
    return warmed;
  }

  boolean isWarmed() { return warmed == count && count > 0; }

  < Append this group's opcodes, in order, onto a stream so the whole program >
  < runs them as part of its verbatim sequence. >
  boolean emitInto(SLOpcodeStream stream) {
    if (stream == null || !active()) { lastError = "inactive group or no stream"; return false; }
    for (int i = 0; i < count; i = i + 1) {
      if (!stream.append(members[i])) { lastError = stream.lastError; return false; }
    }
    return true;
  }

  < Run the group directly against a VM frame, in order. Returns the final >
  < instruction pointer, or a negative value on halt/rejection. >
  int run(int frame) {
    if (!active()) { return -1; }
    int ip = 0;
    for (int i = 0; i < count; i = i + 1) {
      ip = members[i].step(frame);
      if (ip < 0) { return ip; }
    }
    return ip;
  }

  SLOpcodeBase at(int index) {
    if (index < 0 || index >= count) { return null; }
    return members[index];
  }
}