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)

1  
static class EvalTransform {
2  
  int varCount;
3  
  Exception exception;
4  
  boolean recurse = true;
5  
  
6  
  Lisp newVar() {
7  
    ret lisp("$e" + (++varCount));
8  
  }
9  
  
10  
  Lisp transformEval(Lisp tree, L<Lisp> out) {
11  
    if (tree.is("[]", 2) && tree.get(0).is("eval", 0) && tree.get(1).is("()", 1)) {
12  
      Lisp exp = tree.get(1).get(0);
13  
      if (recurse)
14  
        exp = transformEval(exp, out);
15  
      Lisp var = newVar();
16  
      out.add(lisp("[]", var, "=").addAll(nlUnroll(exp)));
17  
      ret var;
18  
    }
19  
    
20  
    // recurse
21  
    
22  
    Lisp n = new Lisp(tree.head);
23  
    for (Lisp t : tree)
24  
      n.add(transformEval(t, out));
25  
    ret n;
26  
  }
27  
  
28  
  Lisp evalTransformRule(Lisp rule) {
29  
    try {
30  
      L<Lisp> ops = snlSplitOps(rule);
31  
      
32  
      // expand unconditional rule
33  
      if (ops == null || ops.isEmpty() || !ops.get(0).is("if *", 1))
34  
        ops = litlist(lisp("then *", rule));
35  
        
36  
      for (int i = 0; i < l(ops); i++) {
37  
        Lisp op = ops.get(i).get(0);
38  
        new L<Lisp> out;
39  
        Lisp op2 = transformEval(op, out);
40  
        if (out.isEmpty()) continue;
41  
        for (Lisp l : out) {
42  
          ops.add(i, lisp(i == 0 ? "if *" : "and *", l));
43  
          ++i;
44  
        }
45  
        ops.set(i, lisp(i == l(ops)-1 ? "then *" : "and *", op2));
46  
      }
47  
      ret snlJoinOps(ops);
48  
    } catch (Exception e) {
49  
      exception = e;
50  
      ret rule;
51  
    }
52  
  }
53  
  
54  
  Lisp evalTransformQuestion(Lisp question) {
55  
    try {
56  
      new L<Lisp> out;
57  
      Lisp tree = transformEval(question, out);
58  
      for (int i = l(out)-1; i >= 0; i--)
59  
        tree = lisp("[]", out.get(i), "and", tree);
60  
      ret tree;
61  
    } catch (Exception e) {
62  
      exception = e;
63  
      ret question;
64  
    }
65  
  }
66  
}

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: 691 / 1700
Referenced in: [show references]