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

90
LINES

< > BotCompany Repo | #1030376 // VStack - a virtual stack for reified, steppable computations

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (8070L) is out of date.

sclass VStack is Steppable, IVStack {
  new L<Computable> stack;
  O latestResult;
  transient O newResult;
  
  // the null sentinel replaces a null function result
  // when stored in latestResult
  sclass NullSentinel {}
  static new NullSentinel nullSentinel;

  *() {}
  *(Computable computation) { push(computation); }
  *(Iterable<Computable> l) { pushAll(l); }
  
  // A is what the function returns
  interface Computable<A> {
    public void step(VStack stack, O subComputationResult);
  }
  
  private O deSentinel(O o) {
    ret o instanceof NullSentinel ? null : o;
  }
  
  private O sentinel(O o) {
    ret o == null ? nullSentinel : o;
  }
  
  // called by computations or users to start a subroutine
  public void push(Computable computation) {
    stack.add(assertNotNull(computation));
  }
  
  // perform a computation step. returns false iff done
  public bool step() {
    if (empty(stack)) false;
    newResult = null;
    last(stack).step(this, result());
    latestResult = newResult;
    newResult = null;
    true;
  }
  
  // called from a computation to return a value
  public void _return(O value) {
    newResult = sentinel(value);
    pop();
  }
  
  // called from a computation to tail-call another routine
  public void tailCall aka replace(Computable computation) {
    pop();
    stack.add(computation);
  }

  // all-in-one evaluation function - call on an empty stack 
  <A> A compute(Computable<A> computation) {
    if (computation == null) null;
    push(computation);
    stepAll(this);
    ret (A) latestResult;
  }
  
  // return result of just completed computation or sub-computation
  public O result aka subResult() {
    ret deSentinel(latestResult);
  }

  bool hasSubResult() { ret latestResult != null; }
  
  void pushAll(Iterable<Computable> l) {
    fOr (Computable c : l)
      push(c);
  }
  
  void add(Runnable r) {
    if (r == null) ret;
    push((stack, subComputationResult) -> r.run());
  }
  
  // caller of current function
  Computable caller() {
    ret nextToLast(stack);
  }
  
  protected void pop() {
    removeLast(stack);
  }
  
  bool isEmpty() { ret empty(stack); }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030376
Snippet name: VStack - a virtual stack for reified, steppable computations
Eternal ID of this version: #1030376/44
Text MD5: 80ece66abd4cce8245dc6139ac77da87
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-01 23:12:23
Source code size: 2235 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 376 / 858
Version history: 43 change(s)
Referenced in: #1030382 - VStack - a virtual stack for reified computations [backup before NullSentinel]
#1030986 - VStack [backup before restructuring]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#1035381 - BStack - a virtual stack with backtracking capability (extension of VStack, OK)