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).

1  
!7
2  
3  
cprint FreezeStackDemo {
4  
  transient bool shouldFreeze;
5  
  
6  
  start-thread {
7  
    print(myFunc(5));
8  
    print(myFunc_hibernatable(5));
9  
    set shouldFreeze;
10  
    try {
11  
      myFunc_hibernatable(5);
12  
    } catch Hib e {
13  
      print("Frozen stack:");
14  
      pnlStruct(e.stack);
15  
      VStack stack = e.asVStack();
16  
      stepAllWith(stack, r {
17  
        print(jreplace(struct(stack), "FreezeStackDemo$myFunc_vstack", "myFunc"));
18  
      });
19  
      print(stack.latestResult);
20  
    }
21  
  }
22  
23  
  S myFunc(int n) {
24  
    if (n >= 100) n = 100;
25  
    if (n <= 0) ret "base";
26  
    ret n + " " + myFunc(n-1);
27  
  }
28  
29  
  // Hibernation event
30  
  sclass Hib extends Exception {
31  
    L<VStack.Computable> stack = new L;
32  
33  
    void add(VStack.Computable entry) { stack.add(entry); }
34  
    VStack asVStack() { ret new VStack(reversed(stack)); }
35  
  }
36  
37  
  srecord myFunc_vstack(int n) implements VStack.Computable<S> {
38  
    public void step(VStack stack, O subResult) {
39  
      if (stack.hasSubResult())
40  
        stack._return(n + " " + subResult);
41  
      else {
42  
        if (n >= 100) n = 100;
43  
        if (n <= 0) ret with stack._return("base");
44  
        stack.push(new myFunc_vstack(n-1));
45  
      }
46  
    }
47  
  }
48  
49  
  S myFunc_hibernatable(int n) throws Hib {
50  
    try {
51  
      if (n >= 100) n = 100;
52  
      if (n <= 0) if (shouldFreeze) hibernate(); else ret "base";
53  
      ret n + " " + myFunc_hibernatable(n-1);
54  
    } on fail Hib e {
55  
      e.add(new myFunc_vstack(n));
56  
    }
57  
  }
58  
59  
  Hib hibernate() throws Hib {
60  
    throw new Hib;
61  
  }
62  
}

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: 190 / 275
Version history: 2 change(s)
Referenced in: [show references]