Home /Modules/vm/basic-vm.sleela

basic-vm.sleela

lib/vm/tutorials/examples/basic-vm.sleela · 97 line(s) · sleela

< SLeeLa VM tutorial example: basic complete VM request. >
#sleela 2.5
material;
class BasicVMTutorialExample {
  < ---- 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 = 82;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-10-09";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-02";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 7;
  < 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; }
    SleelaVMSource source;
    SleelaVMOutput output;
    function configure() {
        source.options = SleelaVMOptions.basicComplete();
        source.memory = SleelaVMMemoryOptions.complete();
        source.runtime = SleelaVMRuntimeOptions.complete();
        source.configureDynamicMemoryGuard();
    }
    function describe() { output.validate(); }
}

< SLeeLa VM tutorial example: drive the secondary VM runtime end to end. >
< Builds an SLVM, emits a canonical-opcode program (2 + 3), runs it on the >
< native SLSleelaVM substrate through the secondary VM, and reads the result. >
class SecondaryVMRuntimeExample {
    SLVM vm;
    int result;

    void main() {
        vm = new SLVM();
        vm.configure("tutorial");
        vm.boot(65536, 65536);

        SLVMModule m = vm.loader().beginModule("tutorial", 4096, 4096, 1024);
        < OP_CONST 2 >
        m.emit(1, 2);
        < OP_CONST 3 >
        m.emit(1, 3);
        < OP_ADD >
        m.emit(8, 0);
        < OP_PRINT >
        m.emit(27, 0);
        < OP_HALT >
        m.emit(48, 0);

        < One class with a 5-opcode main() method starting at index 0. >
        SLVMClass app = new SLVMClass();
        app.configure(0, "App", -1, 0);
        SLVMMethod entry = new SLVMMethod();
        entry.configure("main", "()V", 0, 5, 0, 0, true, false);
        app.addMethod(entry);
        vm.loader().defineClass(app);
        vm.loader().finishModule(0, "main");

        vm.install(m);
        vm.run(1000);
        < 5 >
        result = vm.engine().printed();
        print("secondary-vm result = " + result);
    }
}