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

87
LINES

< > BotCompany Repo | #1002385 // Java Eval Test :-)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1002L/7K/22K).

1  
!752
2  
3  
please include function bigint.
4  
5  
// callable function/method names
6  
static L<S> callable = litlist("bigint", "compareTo");
7  
8  
p {
9  
  printStructure(eval([[ bigint("123") ]]));
10  
  boolean oops = false;
11  
  try {
12  
    eval([[ print("test") ]]); // not callable
13  
    oops = true;
14  
  } catch (Exception e) { /* ok */ }
15  
  assertFalse(oops);
16  
  assertEquals(-1, eval([[ bigint("123").compareTo(bigint("234")) ]]));
17  
  assertEquals(1, eval([[ bigint("567").compareTo(bigint("-123")) ]]));
18  
  
19  
  print("All OK!");
20  
}
21  
22  
static O eval(S text) {
23  
  L e = jparse(text, "exp");
24  
  printStructure(e);
25  
  L<S> tok = javaTok(text); // XX: better get from parser
26  
  
27  
  ret evalExp(e, tok);
28  
}
29  
30  
static O evalExp(L e, L<S> tok) {
31  
  // e == [1, 9, "call", "<identifier> ( <explist_opt> )", [1, 3, "identifier"], [5, 7, "quoted"]]
32  
  
33  
  S cl = getString(e, 2);
34  
  
35  
  if (eq(cl, "quoted"))
36  
    ret unquote(get(tok, (int) get(e, 0)));
37  
    
38  
  if (eq(cl, "call")) {
39  
    ret evalCall(e, tok, getMainClass());
40  
  }
41  
  
42  
  if (eq(cl, "methodcall")) {
43  
    L e_exp = cast get(e, 4); // submatch 1
44  
    L e_call = cast get(e, 5); // submatch 2
45  
    
46  
    O obj = evalExp(e_exp, tok);
47  
    ret evalCall(e_call, tok, obj);
48  
  }
49  
  
50  
  throw fail("woot exp: " + structure(e));
51  
}
52  
53  
static O evalCall(L e, L<S> tok, O obj) {
54  
  L e_name = cast get(e, 4); // submatch 1
55  
  L e_args = cast get(e, 5); // submatch 2
56  
  
57  
  S fname = get(tok, (int) get(e_name, 0));
58  
  L args = evalArgs(e_args, tok);
59  
    
60  
  ret callMethod(obj, fname, args);
61  
}
62  
63  
static L evalArgs(L e, L<S> tok) {
64  
  ret litlist(evalExp(e, tok)); // todo...
65  
}
66  
67  
static L jparse(S text, S className) {
68  
  O parser = run("#1002369");
69  
  //setOpt(parser, "debug", true);
70  
  O parseResult = call(parser, "jparse", text);
71  
  ret (L) call(parseResult, "explain", className);
72  
}
73  
74  
static O callMethod(O obj, S fname, L args) {
75  
  boolean mayCall = mayCallFunction(fname);
76  
  if (!hasMethodNamed(obj, fname))
77  
    throw fail("Method not defined in " + getClassName(obj) + (mayCall ? "" : ", also not callable") + ": " + fname);
78  
    
79  
  if (!mayCallFunction(fname))
80  
    throw fail("Not allowed to call method: " + fname);
81  
    
82  
  ret call(obj, fname, toObjectArray(args));
83  
}
84  
85  
static boolean mayCallFunction(S fname) {
86  
  ret callable.contains(fname);
87  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002385
Snippet name: Java Eval Test :-)
Eternal ID of this version: #1002385/1
Text MD5: 64d0c779ea29dc56f7d948f803bdbbcc
Transpilation MD5: d2d0be9966da42d7b96d242352a7bb4f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-19 01:02:25
Source code size: 2309 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 593 / 879
Referenced in: [show references]