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

66
LINES

< > BotCompany Repo | #1002867 // class EvalTransform (eval preprocessor)

JavaX fragment (include)

static class EvalTransform {
  int varCount;
  Exception exception;
  boolean recurse = true;
  
  Lisp newVar() {
    ret lisp("$e" + (++varCount));
  }
  
  Lisp transformEval(Lisp tree, L<Lisp> out) {
    if (tree.is("[]", 2) && tree.get(0).is("eval", 0) && tree.get(1).is("()", 1)) {
      Lisp exp = tree.get(1).get(0);
      if (recurse)
        exp = transformEval(exp, out);
      Lisp var = newVar();
      out.add(lisp("[]", var, "=").addAll(nlUnroll(exp)));
      ret var;
    }
    
    // recurse
    
    Lisp n = new Lisp(tree.head);
    for (Lisp t : tree)
      n.add(transformEval(t, out));
    ret n;
  }
  
  Lisp evalTransformRule(Lisp rule) {
    try {
      L<Lisp> ops = snlSplitOps(rule);
      
      // expand unconditional rule
      if (ops == null || ops.isEmpty() || !ops.get(0).is("if *", 1))
        ops = litlist(lisp("then *", rule));
        
      for (int i = 0; i < l(ops); i++) {
        Lisp op = ops.get(i).get(0);
        new L<Lisp> out;
        Lisp op2 = transformEval(op, out);
        if (out.isEmpty()) continue;
        for (Lisp l : out) {
          ops.add(i, lisp(i == 0 ? "if *" : "and *", l));
          ++i;
        }
        ops.set(i, lisp(i == l(ops)-1 ? "then *" : "and *", op2));
      }
      ret snlJoinOps(ops);
    } catch (Exception e) {
      exception = e;
      ret rule;
    }
  }
  
  Lisp evalTransformQuestion(Lisp question) {
    try {
      new L<Lisp> out;
      Lisp tree = transformEval(question, out);
      for (int i = l(out)-1; i >= 0; i--)
        tree = lisp("[]", out.get(i), "and", tree);
      ret tree;
    } catch (Exception e) {
      exception = e;
      ret question;
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002867
Snippet name: class EvalTransform (eval preprocessor)
Eternal ID of this version: #1002867/1
Text MD5: 703f58b235f7bbbf36ae31186c03640e
Author: stefan
Category: eleu / nl / prolog
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-11 00:30:03
Source code size: 1736 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 690 / 1699
Referenced in: #1003872 - Integrating #759 in One Program
#1003874 - Backup of #759 Before Integration
#1004091 - 759 with new loadClasses (spike)
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)