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

61
LINES

< > BotCompany Repo | #1030472 // Freeze Stack Demo [OK]

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

Transpiled version (3823L) is out of date.

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

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030472
Snippet name: Freeze Stack Demo [OK]
Eternal ID of this version: #1030472/20
Text MD5: 748fdf7c0872ae8df7370e104f18064b
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:47:25
Source code size: 1512 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 119 / 221
Version history: 19 change(s)
Referenced in: [show references]