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

86
LINES

< > BotCompany Repo | #1002746 // SNL Logic Engine 3 (C is a Language)

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

Libraryless. Click here for Pure Java version (4316L/28K/89K).

!759

static S codeSnippetID = "#1002745";
static Lisp code;
static new L<Lisp> memory;

static new L<S> log;

p {
  code = snlToTree(loadSnippet(codeSnippetID));
  print(getProgramID() + ": parse result = " + code);
  load("memory");
}

static Lisp snlMatchFirst(Lisp pat, L<Lisp> list, SNLMatches m) {
  for (Lisp l : list)
    if (snlMatch2(pat, l, m))
      ret l;
  ret null;
}

static Lisp scanMem(Lisp pat, SNLMatches m) {
  ret snlMatchFirst(pat, memory, m);
}

static Lisp scanMem(S pat, SNLMatches m) {
  ret snlMatchFirst(snlToTree_cached(pat), memory, m);
}

static void memorize(Lisp l) {
  if (!memory.contains(l)) {
    memory.add(l);
    save("memory");
  }
}

static boolean yo(Lisp snl, SNLMatches m) {
  if (scanMem(snl, m) != null)
    ret true;

  if (snlMatch2("i < don't < know < X", snl, m))
    ret scanMem("* < said < X < is < *", m) == null; // TODO: vars probably don't work

  ret false;
}

!include #1002750 // new LThread

static Lisp runSingle(Lisp statement, Lisp code) {
  log.add("runSingle " + code);
  LThread thread = new LThread(code);
  thread.log = log;
  thread.statements.add(statement);
  print("Statements: " + structure(thread.statements));
  ret thread.run() ? first(thread.output) : null;
}

static Lisp runMulti(Lisp statement, Lisp code) {
  assertTrue(isJuxta(code));
  for (Lisp sub : code) {
    Lisp l = runSingle(statement, sub);
    if (l != null) ret l;
  }
  ret null;
}

answer {
  if (!attn()) ret null;
  
  if (match("log " + parseSnippetID(programID()), s))
    ret slackSnippet(fromLines(log));
  
  if (match("memory " + parseSnippetID(programID()), s))
    ret slackSnippet(structureLines(memory));
  
  log.clear();
  
  // convert to SNL first
  s = or((S) callOpt/*Safe*/(getBot ("#1002700"), "naiveSNL", s), s);
  Lisp in = snlToTree(s);
  
  Lisp statement = snlToTree("user < says < [" + snlFromTree(in) + "]");
  Lisp out = isJuxta(code) ? runMulti(statement, code) : runSingle(statement, code);
  if (out != null)
    ret "[SNL: " + snlFromTree(in) + "]\n" + snlToText(out);
}

Author comment

Began life as a copy of #1002733

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: #1002746
Snippet name: SNL Logic Engine 3 (C is a Language)
Eternal ID of this version: #1002746/1
Text MD5: 33202dfba0496e02fb2a9d3c9d418801
Transpilation MD5: 8160b929123682e078758341e7538de1
Author: stefan
Category: eleu / nl
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-19 21:56:11
Source code size: 2135 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 691 / 655
Referenced in: [show references]