Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

62
LINES

< > BotCompany Repo | #1030478 // Hibernate Stack Demo [v2]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1059K of libraries. Click here for Pure Java version (3960L/20K).

!7

cprint FreezeStackDemo {
  transient bool shouldFreeze;
  
  start-thread {
    print(myFunc(5));
    print(myFunc_hibernatable(5));
    set shouldFreeze;
    try {
      myFunc_hibernatable(5);
    } catch Hib e {
      print("Frozen stack:");
      pnlStruct(e.stack);
      VStack stack = e.asVStack();
      stepAllWith(stack, r {
        print(jreplace(struct(stack), "FreezeStackDemo$myFunc_vstack", "myFunc"));
      });
      print(stack.latestResult);
    }
  }

  S myFunc(int n) {
    if (n >= 100) n = 100;
    if (n <= 0) ret "base";
    ret n + " " + myFunc(n-1);
  }

  // Hibernation event
  sclass Hib extends Exception {
    L<VStack.Computable> stack = new L;

    void add(VStack.Computable entry) { stack.add(entry); }
    VStack asVStack() { ret new VStack(reversed(stack)); }
  }

  srecord myFunc_vstack(int n) implements VStack.Computable<S> {
    public void step(VStack stack, O subResult) {
      if (stack.hasSubResult())
        stack._return(n + " " + subResult);
      else {
        if (n >= 100) n = 100;
        if (n <= 0) ret with stack._return("base");
        stack.push(new myFunc_vstack(n-1));
      }
    }
  }

  S myFunc_hibernatable(int n) throws Hib {
    try {
      if (n >= 100) n = 100;
      if (n <= 0) if (shouldFreeze) hibernate(); else ret "base";
      ret n + " " + myFunc_hibernatable(n-1);
    } on fail Hib e {
      e.add(new myFunc_vstack(n));
    }
  }

  Hib hibernate() throws Hib {
    throw new Hib;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030478
Snippet name: Hibernate Stack Demo [v2]
Eternal ID of this version: #1030478/3
Text MD5: 08148b379c23f3df74c4f8c45049dcec
Transpilation MD5: 363ed023390a449907fa6a4a3cb422e5
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-12-23 05:50:03
Source code size: 1477 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 189 / 274
Version history: 2 change(s)
Referenced in: [show references]