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

1  
!7
2  
3  
srecord DollarVarPattern(S pat) {}
4  
srecord Definition(O lhs, O rhs) {}
5  
srecord PatternMatches(O pat, O s) {}
6  
srecord Input(S s) {}
7  
8  
sS defs = [[
9  
A cup of water = A cup with water inside
10  
A cup of Peter = A cup that belongs to Peter
11  
A cup of glass = A cup that consists of glass
12  
A cup of style = A cup that has a lot of style
13  
]];
14  
15  
static new LinkedHashSet objects;
16  
17  
p-exp {
18  
  objects.add(Input("a cup of joe"));
19  
  
20  
  for (S s : tlft(defs)) {
21  
    L<S> l = splitAtJavaToken(s, "=");
22  
    objects.add(Definition(first(l), second(l)));
23  
  }
24  
  
25  
  int initialObjects = l(objects);
26  
  long startTime = now();
27  
  int n = -1;
28  
  while (l(objects) > n) {
29  
    n = l(objects);
30  
    L<Definition> defs = instancesOf(Definition, objects);
31  
    for (Definition a : defs)
32  
      for (Definition b : defs)
33  
        if (a.lhs instanceof S && b.lhs instanceof S) {
34  
          S pat = combineToDollarVarPattern((S) a.lhs, (S) b.lhs);
35  
          if (containsDollarVars(pat))
36  
            objects.add(DollarVarPattern(pat));
37  
        }
38  
        
39  
    for (DollarVarPattern pat : instancesOf(DollarVarPattern, objects)) {
40  
      for (Definition a : defs) if (a.lhs instanceof S) {
41  
        SS match = matchDollarVarsIC(pat.pat, (S) a.lhs);
42  
        if (match != null)
43  
          generalizeDefinition(a, match);
44  
      }
45  
    }
46  
    
47  
    for (Input i : instancesOf(Input, objects))
48  
      for (Definition a : defs) if (a.lhs instanceof DollarVarPattern) {
49  
        SS match = matchDollarVarsIC(((DollarVarPattern) a.lhs).pat, i.s);
50  
        if (match != null)
51  
          specializeDefinition(a, match);
52  
      }
53  
  }
54  
  long time = now()-startTime;
55  
  
56  
  pnlStruct(objects);
57  
  print("\n" + n2(l(objects)-initialObjects, "rewrite") + " in " + (now()-startTime) + " ms");
58  
  
59  
  final Input in = firstInstanceOf(Input, objects);
60  
  if (in != null) {
61  
    L<Definition> definitions = [Definition d : instancesOf(Definition, objects) | eqic_gen(d.lhs, in.s)];
62  
    print("Got " + n2(definitions, "definition") + " for input " + quote(in.s) + (empty(definitions) ? "." : ":"));
63  
    printStructLinesIndent(definitions);
64  
    assertEquals(4, l(definitions));
65  
  }
66  
}
67  
68  
svoid generalizeDefinition(Definition def, SS match) {
69  
  SS rev = reverseCIMap(match);
70  
  S lhs = transformEachTokenUsingMap((S) def.lhs, rev);
71  
  S rhs = transformEachTokenUsingMap((S) def.rhs, rev);
72  
  addIfNotNull(objects, Definition(DollarVarPattern(lhs), DollarVarPattern(rhs)));
73  
}
74  
75  
svoid specializeDefinition(Definition def, SS match) {
76  
  S lhs = transformEachTokenUsingMap(((DollarVarPattern) def.lhs).pat, match);
77  
  S rhs = transformEachTokenUsingMap(((DollarVarPattern) def.rhs).pat, match);
78  
  addIfNotNull(objects, Definition(lhs, rhs));
79  
}

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: 534 / 1313
Version history: 28 change(s)
Referenced in: [show references]