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

157
LINES

< > BotCompany Repo | #1004241 // Coroutines (include)

JavaX fragment (include)

1  
!include #1007935 // isTrue for coroutines
2  
3  
static O evalAll(L l) {
4  
  O result = null;
5  
  for (O o : unnull(l))
6  
    result = eval(o);
7  
  ret result;
8  
}
9  
10  
static O eval(O o) {
11  
  // for more convenient code making (string literals)
12  
  if (o instanceof S)
13  
    ret (S) o;
14  
  ret callOpt(o);
15  
}
16  
17  
static int stepAll_steps;
18  
static bool stepAll_verbose;
19  
20  
// step until state = null
21  
static O stepAll(O o) {
22  
  O result = null;
23  
  while (o != null) {
24  
    ++stepAll_steps;
25  
    if (stepAll_verbose)
26  
      print("step " + stepAll_steps + ": " + structure(o));
27  
    Pair p = cast call(o, "step");
28  
    result = p.a;
29  
    o = p.b;
30  
  }
31  
  ret result;
32  
}
33  
34  
static Pair doStep(O o) {
35  
  if (stepAll_verbose)
36  
    print("doStep " + structure(o));
37  
38  
  if (o == null) ret pair(null, null);
39  
  
40  
  if (hasMethod(o, "step"))
41  
    ret (Pair) call(o, "step");
42  
    
43  
  // it's something simple, let's just eval
44  
  ret pair(eval(o), null);
45  
}
46  
47  
sclass Block {
48  
  L statements;
49  
  
50  
  *() {}
51  
  *(L *statements) {}
52  
  
53  
  O get() {
54  
    ret evalAll(statements);
55  
  }
56  
  
57  
  class Step {
58  
    int i;
59  
    Block block = Block.this; // for structure()
60  
61  
    *() {}
62  
    *(int *i) {}
63  
    
64  
    Pair step() {
65  
      Pair p = doStep(statements.get(i));
66  
      O nextStep = i+1 < l(statements) ? new Step(i+1) : null;
67  
      ret pair(p.a, _block(p.b, nextStep));
68  
    }
69  
  }
70  
  
71  
  // result + new state
72  
  Pair step() {
73  
    ret isEmpty(statements) ? new Pair(null, null) : new Step().step();
74  
  }
75  
}
76  
77  
static O _block(O... statements) {
78  
  L l = withoutNulls(newList(statements));
79  
  if (isEmpty(l)) null;
80  
  if (l(l) == 1) ret first(l);
81  
  ret new Block(l);
82  
}
83  
84  
sclass ResultFinder {
85  
  O exp;
86  
  Var dest;
87  
  
88  
  *() {}
89  
  *(O *exp, Var *dest) {}
90  
  
91  
  O step() {
92  
    Pair p = doStep(exp);
93  
    if (p.b == null) {
94  
      dest.set(p.a);
95  
      ret p;
96  
    } else
97  
      // keep evaluating until done
98  
      ret pair(null, _block(p.b, this));
99  
  }
100  
}
101  
102  
sclass If {
103  
  O a, b, c; // condition, then, else
104  
  
105  
  *() {}
106  
  *(O *a, O *b) {}
107  
  *(O *a, O *b, O *c) {}
108  
  
109  
  O get() {
110  
    ret eval(isTrue(a) ? b : c);
111  
  }
112  
  
113  
  // rewrite so it's steppable
114  
  Pair step() {
115  
    final new Var result;
116  
    ret doStep(_block(
117  
      new ResultFinder(a, result),
118  
      new O {
119  
        O step() {
120  
          ret doStep(booleanValue(result.get()) ? b : c);
121  
        }
122  
      }));
123  
  }
124  
}
125  
126  
static O _if(O a, O b) {
127  
  ret new If(a, b);
128  
}
129  
130  
static O _if(O a, O b, O c) {
131  
  ret new If(a, b, c);
132  
}
133  
134  
sclass While {
135  
  O condition, code;
136  
  
137  
  *() {}
138  
  *(O *condition, O *code) {}
139  
  
140  
  void get() {
141  
    while (isTrue(condition))
142  
      eval(code);
143  
  }
144  
  
145  
  // rewrite so it's steppable
146  
  Pair step() {
147  
    ret doStep(_if(condition, _block(code, this)));
148  
  }
149  
}
150  
151  
static O _while(O a, O code) {
152  
  ret new While(a, code);
153  
}
154  
155  
static O _for(O a, O b, O c, O code) {
156  
  ret _block(a, _while(b, _block(code, c)));
157  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004241
Snippet name: Coroutines (include)
Eternal ID of this version: #1004241/4
Text MD5: c75282bc93c9f0d94a58f2a85ab77422
Author: stefan
Category: javax / coroutines
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-06 17:49:46
Source code size: 2923 bytes / 157 lines
Pitched / IR pitched: No / No
Views / Downloads: 771 / 1174
Version history: 3 change(s)
Referenced in: [show references]