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

79
LINES

< > BotCompany Repo | #1030986 // VStack [backup before restructuring]

JavaX fragment (include)

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

Author comment

Began life as a copy of #1030376

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1030986
Snippet name: VStack [backup before restructuring]
Eternal ID of this version: #1030986/1
Text MD5: c9822bca9533b008cf477ebf9d8f4db2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-18 14:55:05
Source code size: 2137 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 93 / 111
Referenced in: [show references]