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).

!752

please include function bigint.

// callable function/method names
static L<S> callable = litlist("bigint", "compareTo");

p {
  printStructure(eval([[ bigint("123") ]]));
  boolean oops = false;
  try {
    eval([[ print("test") ]]); // not callable
    oops = true;
  } catch (Exception e) { /* ok */ }
  assertFalse(oops);
  assertEquals(-1, eval([[ bigint("123").compareTo(bigint("234")) ]]));
  assertEquals(1, eval([[ bigint("567").compareTo(bigint("-123")) ]]));
  
  print("All OK!");
}

static O eval(S text) {
  L e = jparse(text, "exp");
  printStructure(e);
  L<S> tok = javaTok(text); // XX: better get from parser
  
  ret evalExp(e, tok);
}

static O evalExp(L e, L<S> tok) {
  // e == [1, 9, "call", "<identifier> ( <explist_opt> )", [1, 3, "identifier"], [5, 7, "quoted"]]
  
  S cl = getString(e, 2);
  
  if (eq(cl, "quoted"))
    ret unquote(get(tok, (int) get(e, 0)));
    
  if (eq(cl, "call")) {
    ret evalCall(e, tok, getMainClass());
  }
  
  if (eq(cl, "methodcall")) {
    L e_exp = cast get(e, 4); // submatch 1
    L e_call = cast get(e, 5); // submatch 2
    
    O obj = evalExp(e_exp, tok);
    ret evalCall(e_call, tok, obj);
  }
  
  throw fail("woot exp: " + structure(e));
}

static O evalCall(L e, L<S> tok, O obj) {
  L e_name = cast get(e, 4); // submatch 1
  L e_args = cast get(e, 5); // submatch 2
  
  S fname = get(tok, (int) get(e_name, 0));
  L args = evalArgs(e_args, tok);
    
  ret callMethod(obj, fname, args);
}

static L evalArgs(L e, L<S> tok) {
  ret litlist(evalExp(e, tok)); // todo...
}

static L jparse(S text, S className) {
  O parser = run("#1002369");
  //setOpt(parser, "debug", true);
  O parseResult = call(parser, "jparse", text);
  ret (L) call(parseResult, "explain", className);
}

static O callMethod(O obj, S fname, L args) {
  boolean mayCall = mayCallFunction(fname);
  if (!hasMethodNamed(obj, fname))
    throw fail("Method not defined in " + getClassName(obj) + (mayCall ? "" : ", also not callable") + ": " + fname);
    
  if (!mayCallFunction(fname))
    throw fail("Not allowed to call method: " + fname);
    
  ret call(obj, fname, toObjectArray(args));
}

static boolean mayCallFunction(S fname) {
  ret callable.contains(fname);
}

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