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

157
LINES

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

JavaX fragment (include)

!include #1007935 // isTrue for coroutines

static O evalAll(L l) {
  O result = null;
  for (O o : unnull(l))
    result = eval(o);
  ret result;
}

static O eval(O o) {
  // for more convenient code making (string literals)
  if (o instanceof S)
    ret (S) o;
  ret callOpt(o);
}

static int stepAll_steps;
static bool stepAll_verbose;

// step until state = null
static O stepAll(O o) {
  O result = null;
  while (o != null) {
    ++stepAll_steps;
    if (stepAll_verbose)
      print("step " + stepAll_steps + ": " + structure(o));
    Pair p = cast call(o, "step");
    result = p.a;
    o = p.b;
  }
  ret result;
}

static Pair doStep(O o) {
  if (stepAll_verbose)
    print("doStep " + structure(o));

  if (o == null) ret pair(null, null);
  
  if (hasMethod(o, "step"))
    ret (Pair) call(o, "step");
    
  // it's something simple, let's just eval
  ret pair(eval(o), null);
}

sclass Block {
  L statements;
  
  *() {}
  *(L *statements) {}
  
  O get() {
    ret evalAll(statements);
  }
  
  class Step {
    int i;
    Block block = Block.this; // for structure()

    *() {}
    *(int *i) {}
    
    Pair step() {
      Pair p = doStep(statements.get(i));
      O nextStep = i+1 < l(statements) ? new Step(i+1) : null;
      ret pair(p.a, _block(p.b, nextStep));
    }
  }
  
  // result + new state
  Pair step() {
    ret isEmpty(statements) ? new Pair(null, null) : new Step().step();
  }
}

static O _block(O... statements) {
  L l = withoutNulls(newList(statements));
  if (isEmpty(l)) null;
  if (l(l) == 1) ret first(l);
  ret new Block(l);
}

sclass ResultFinder {
  O exp;
  Var dest;
  
  *() {}
  *(O *exp, Var *dest) {}
  
  O step() {
    Pair p = doStep(exp);
    if (p.b == null) {
      dest.set(p.a);
      ret p;
    } else
      // keep evaluating until done
      ret pair(null, _block(p.b, this));
  }
}

sclass If {
  O a, b, c; // condition, then, else
  
  *() {}
  *(O *a, O *b) {}
  *(O *a, O *b, O *c) {}
  
  O get() {
    ret eval(isTrue(a) ? b : c);
  }
  
  // rewrite so it's steppable
  Pair step() {
    final new Var result;
    ret doStep(_block(
      new ResultFinder(a, result),
      new O {
        O step() {
          ret doStep(booleanValue(result.get()) ? b : c);
        }
      }));
  }
}

static O _if(O a, O b) {
  ret new If(a, b);
}

static O _if(O a, O b, O c) {
  ret new If(a, b, c);
}

sclass While {
  O condition, code;
  
  *() {}
  *(O *condition, O *code) {}
  
  void get() {
    while (isTrue(condition))
      eval(code);
  }
  
  // rewrite so it's steppable
  Pair step() {
    ret doStep(_if(condition, _block(code, this)));
  }
}

static O _while(O a, O code) {
  ret new While(a, code);
}

static O _for(O a, O b, O c, O code) {
  ret _block(a, _while(b, _block(code, c)));
}

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: 675 / 1070
Version history: 3 change(s)
Referenced in: [show references]