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.

1  
sclass VStack is Steppable, IVStack {
2  
  new L<Computable> stack;
3  
  O latestResult;
4  
  transient O newResult;
5  
  
6  
  // the null sentinel replaces a null function result
7  
  // when stored in latestResult
8  
  sclass NullSentinel {}
9  
  static new NullSentinel nullSentinel;
10  
11  
  *() {}
12  
  *(Computable computation) { push(computation); }
13  
  *(Iterable<Computable> l) { pushAll(l); }
14  
  
15  
  // A is what the function returns
16  
  interface Computable<A> {
17  
    public void step(VStack stack, O subComputationResult);
18  
  }
19  
  
20  
  private O deSentinel(O o) {
21  
    ret o instanceof NullSentinel ? null : o;
22  
  }
23  
  
24  
  private O sentinel(O o) {
25  
    ret o == null ? nullSentinel : o;
26  
  }
27  
  
28  
  // called by computations or users to start a subroutine
29  
  public void push(Computable computation) {
30  
    stack.add(assertNotNull(computation));
31  
  }
32  
  
33  
  // perform a computation step. returns false iff done
34  
  public bool step() {
35  
    if (empty(stack)) false;
36  
    newResult = null;
37  
    last(stack).step(this, result());
38  
    latestResult = newResult;
39  
    newResult = null;
40  
    true;
41  
  }
42  
  
43  
  // called from a computation to return a value
44  
  public void _return(O value) {
45  
    newResult = sentinel(value);
46  
    pop();
47  
  }
48  
  
49  
  // called from a computation to tail-call another routine
50  
  public void tailCall aka replace(Computable computation) {
51  
    pop();
52  
    stack.add(computation);
53  
  }
54  
55  
  // all-in-one evaluation function - call on an empty stack 
56  
  <A> A compute(Computable<A> computation) {
57  
    if (computation == null) null;
58  
    push(computation);
59  
    stepAll(this);
60  
    ret (A) latestResult;
61  
  }
62  
  
63  
  // return result of just completed computation or sub-computation
64  
  public O result aka subResult() {
65  
    ret deSentinel(latestResult);
66  
  }
67  
68  
  bool hasSubResult() { ret latestResult != null; }
69  
  
70  
  void pushAll(Iterable<Computable> l) {
71  
    fOr (Computable c : l)
72  
      push(c);
73  
  }
74  
  
75  
  void add(Runnable r) {
76  
    if (r == null) ret;
77  
    push((stack, subComputationResult) -> r.run());
78  
  }
79  
  
80  
  // caller of current function
81  
  Computable caller() {
82  
    ret nextToLast(stack);
83  
  }
84  
  
85  
  protected void pop() {
86  
    removeLast(stack);
87  
  }
88  
  
89  
  bool isEmpty() { ret empty(stack); }
90  
}

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: 375 / 857
Version history: 43 change(s)
Referenced in: [show references]