Home /Modules/collections/SLStack.sleela

SLStack.sleela

lib/collections/SLStack.sleela · 104 line(s) · sleela

<< lib/collections/SLStack.sleela
   SLeeLa Standard Library Definition.
   Definition: A concrete, String-valued LIFO stack backed by the VM-managed
   growable native array. push appends; pop and peek address the top. This
   first implementation deliberately avoids Generics. >>
#sleela 2.5
material;
< Unified SLeeLa standard-library front-end object. >
< Family: collections. Native VM/OS work crosses an explicit bridge. >
class SLStack {
  << ---- 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 real origin where publicly known. >>
  String metaVersion = "1.1.0";
  int metaId = 840;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-11-04";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-09-28";
  < 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; }

  << Backing storage uses the VM-managed, growable String array. size is the
     logical height; values[size-1] is the top of the stack. >>
  String[] values = new String[0];
  int size = 0;

  < Push one value onto the top. Freed capacity is reused after pops. >
  void push(String value) {
    if (size < arrayLength(values)) {
      arraySet(values, size, value);
    } else {
      arrayPush(values, value);
    }
    size = size + 1;
  }

  < Return the top value without removing it; null when empty. >
  String peek() {
    if (size == 0) { return null; }
    return arrayGet(values, size - 1);
  }

  < Remove and return the top value; null when empty. >
  String pop() {
    if (size == 0) { return null; }
    size = size - 1;
    String top = arrayGet(values, size);
    arraySet(values, size, null);
    return top;
  }

  < Compatibility convenience: push defers to add for a uniform collection API. >
  void add(String value) { push(value); }

  < Compatibility convenience: discard the top item, if present. >
  void remove() {
    if (size > 0) {
      size = size - 1;
      arraySet(values, size, null);
    }
  }

  < Drop every item; capacity remains available for reuse. >
  void clear() {
    while (size > 0) {
      size = size - 1;
      arraySet(values, size, null);
    }
  }
  int length() { return size; }
  boolean empty() { return size == 0; }
}