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.

!7

cprint FreezeStackDemo {
  transient bool shouldFreeze;
  
  start-thread {
    print(myFunc(5));
    print(myFunc_v2(5));
    set shouldFreeze;
    try {
      myFunc_v2(5);
    } catch FreezeStackException e {
      print("Frozen stack:");
      pnlStruct(e.stack);
      new VStack stack;
      stack.pushAll(reversed(e.stack));
      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);
  }

  class FreezeStackException extends Exception {
    L<VStack.Computable> stack = new L;

    void add(VStack.Computable entry) { stack.add(entry); }
  }

  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_v2(int n) throws FreezeStackException {
    try {
      if (n >= 100) n = 100;
      if (n <= 0) if (shouldFreeze) throw freezeStack(); else ret "base";
      ret n + " " + myFunc_v2(n-1);
    } on fail FreezeStackException e {
      e.add(new myFunc_vstack(n));
    }
  }

  FreezeStackException freezeStack() throws FreezeStackException {
    throw new FreezeStackException;
  }
}

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: 189 / 305
Version history: 19 change(s)
Referenced in: [show references]