Home /Modules/opcodes/SLOpcodeRegistrar.sleela

SLOpcodeRegistrar.sleela

lib/opcodes/governance/SLOpcodeRegistrar.sleela · 154 line(s) · sleela

<< lib/opcodes/governance/SLOpcodeRegistrar.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLOpcodeRegistrar, the BEFORE-phase authority a developer
     adds to a .sleela source so that opcodes are not run raw into the VM without
     consideration.
   
   Discretion the user described:
     A developer *may* technically run opcodes raw into the VM. Usually, though,
     we want a Registrar to consider the program from A to B before a series of
     opcodes is run upon it. Adding the Registrar to a source therefore checks
     that the opcodes are in order at all unto themselves, or rejects them, and
     may pause the program as "unrest".
   
   "In order unto themselves" — the Registrar's self-consistency checks, applied
     to the opcode sequence A..B with no reference to any outer program yet:
       1. every opcode is a valid, armed, canonical opcode (0..102);
       2. the sequence terminates — exactly one terminal (OP_HALT) and it is last,
          OR the sequence is a well-formed fragment explicitly marked open-ended;
       3. no opcode appears after the terminal (nothing runs past HALT);
       4. the sequence is non-empty.
     A clean pass ADMITs. A terminal-placement problem PAUSEs as unrest (it may be
     a fragment awaiting composition). A structural violation REJECTs.
   
   Family: opcodes.governance. Native VM/OS work crosses an explicit bridge. >>
#sleela 2.5
material;
< Unified SLeeLa standard-library front-end object. >
class SLOpcodeRegistrar {
  < ---- 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 = 10191;
  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; }
  < canonical OP_HALT >
  static final int HALT_CODE = 48;

  < the .sleela source this registrar was added to >
  String sourceName;
  int state;
  < true when the source is a composable fragment >
  boolean allowOpenEnded;
  SLGovVerdict lastVerdict;

  void configure(String source) {
    sourceName = source;
    state = 1;
    allowOpenEnded = false;
  }

  void permitOpenEnded() { allowOpenEnded = true; }
  void enable() { state = 1; }
  void disable() { state = 0; }
  boolean active() { return state == 1; }

  < Consider the whole program A -> B before any opcode runs. Returns the >
  < discretionary verdict; the orchestrator honours it before DURING begins. >
  SLGovVerdict consider(SLOpcodeStream stream) {
    SLGovVerdict v = new SLGovVerdict();
    if (!active() || stream == null) {
      v.configure(SLGovVerdict.REJECT, "registrar inactive or no program", -1, SLGovPhase.BEFORE);
      lastVerdict = v;
      return v;
    }

    int n = stream.size();
    if (n < 1) {
      v.configure(SLGovVerdict.REJECT, "empty program A..B", -1, SLGovPhase.BEFORE);
      lastVerdict = v;
      return v;
    }

    int haltAt = -1;
    for (int i = 0; i < n; i = i + 1) {
      SLOpcodeBase op = stream.ops[i];
      < (1) validity + armed >
      if (op == null || !op.armed()) {
        v.configure(SLGovVerdict.REJECT, "unarmed or missing opcode", i, SLGovPhase.BEFORE);
        lastVerdict = v;
        return v;
      }
      int code = op.opcode();
      if (code < 0 || code > SLOpcodeBase.CANONICAL_COUNT - 1) {
        v.configure(SLGovVerdict.REJECT, "non-canonical opcode out of 0..102", i, SLGovPhase.BEFORE);
        lastVerdict = v;
        return v;
      }
      < (3) nothing after the terminal >
      if (haltAt >= 0) {
        v.configure(SLGovVerdict.REJECT, "opcode present after OP_HALT", i, SLGovPhase.BEFORE);
        lastVerdict = v;
        return v;
      }
      if (code == HALT_CODE) { haltAt = i; }
    }

    < (2) termination discipline >
    if (haltAt < 0) {
      if (allowOpenEnded) {
        v.configure(SLGovVerdict.ADMIT, "open-ended fragment admitted for composition", -1, SLGovPhase.BEFORE);
      } else {
        < A program with no terminal is held as unrest, not rejected outright. >
        v.configure(SLGovVerdict.PAUSE, "no OP_HALT: program does not terminate (unrest)", n - 1, SLGovPhase.BEFORE);
      }
      lastVerdict = v;
      return v;
    }
    if (haltAt != n - 1) {
      v.configure(SLGovVerdict.PAUSE, "OP_HALT is not the final opcode (unrest)", haltAt, SLGovPhase.BEFORE);
      lastVerdict = v;
      return v;
    }

    v.configure(SLGovVerdict.ADMIT, "program is in order unto itself A..B", -1, SLGovPhase.BEFORE);
    lastVerdict = v;
    return v;
  }
}