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

79
LINES

< > BotCompany Repo | #1017020 // Patterns Step 1 [OK]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7473L/53K).

!7

srecord DollarVarPattern(S pat) {}
srecord Definition(O lhs, O rhs) {}
srecord PatternMatches(O pat, O s) {}
srecord Input(S s) {}

sS defs = [[
A cup of water = A cup with water inside
A cup of Peter = A cup that belongs to Peter
A cup of glass = A cup that consists of glass
A cup of style = A cup that has a lot of style
]];

static new LinkedHashSet objects;

p-exp {
  objects.add(Input("a cup of joe"));
  
  for (S s : tlft(defs)) {
    L<S> l = splitAtJavaToken(s, "=");
    objects.add(Definition(first(l), second(l)));
  }
  
  int initialObjects = l(objects);
  long startTime = now();
  int n = -1;
  while (l(objects) > n) {
    n = l(objects);
    L<Definition> defs = instancesOf(Definition, objects);
    for (Definition a : defs)
      for (Definition b : defs)
        if (a.lhs instanceof S && b.lhs instanceof S) {
          S pat = combineToDollarVarPattern((S) a.lhs, (S) b.lhs);
          if (containsDollarVars(pat))
            objects.add(DollarVarPattern(pat));
        }
        
    for (DollarVarPattern pat : instancesOf(DollarVarPattern, objects)) {
      for (Definition a : defs) if (a.lhs instanceof S) {
        SS match = matchDollarVarsIC(pat.pat, (S) a.lhs);
        if (match != null)
          generalizeDefinition(a, match);
      }
    }
    
    for (Input i : instancesOf(Input, objects))
      for (Definition a : defs) if (a.lhs instanceof DollarVarPattern) {
        SS match = matchDollarVarsIC(((DollarVarPattern) a.lhs).pat, i.s);
        if (match != null)
          specializeDefinition(a, match);
      }
  }
  long time = now()-startTime;
  
  pnlStruct(objects);
  print("\n" + n2(l(objects)-initialObjects, "rewrite") + " in " + (now()-startTime) + " ms");
  
  final Input in = firstInstanceOf(Input, objects);
  if (in != null) {
    L<Definition> definitions = [Definition d : instancesOf(Definition, objects) | eqic_gen(d.lhs, in.s)];
    print("Got " + n2(definitions, "definition") + " for input " + quote(in.s) + (empty(definitions) ? "." : ":"));
    printStructLinesIndent(definitions);
    assertEquals(4, l(definitions));
  }
}

svoid generalizeDefinition(Definition def, SS match) {
  SS rev = reverseCIMap(match);
  S lhs = transformEachTokenUsingMap((S) def.lhs, rev);
  S rhs = transformEachTokenUsingMap((S) def.rhs, rev);
  addIfNotNull(objects, Definition(DollarVarPattern(lhs), DollarVarPattern(rhs)));
}

svoid specializeDefinition(Definition def, SS match) {
  S lhs = transformEachTokenUsingMap(((DollarVarPattern) def.lhs).pat, match);
  S rhs = transformEachTokenUsingMap(((DollarVarPattern) def.rhs).pat, match);
  addIfNotNull(objects, Definition(lhs, rhs));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017020
Snippet name: Patterns Step 1 [OK]
Eternal ID of this version: #1017020/29
Text MD5: aea38056d2a535418fa3bf68d5befe57
Transpilation MD5: 69cb33a5c5b48cade23319487c9bd12a
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-07-10 22:18:57
Source code size: 2725 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 347 / 862
Version history: 28 change(s)
Referenced in: [show references]