use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class OplParser method program.
public static Program<OplExp> program(String s) {
List<Triple<String, Integer, OplExp>> ret = new LinkedList<>();
List decls = (List) program.parse(s);
sugarForNat = false;
for (Object d : decls) {
org.jparsec.functors.Pair pr = (org.jparsec.functors.Pair) d;
Tuple3 decl = (Tuple3) pr.b;
toProgHelper(pr.a.toString(), s, ret, decl);
}
sugarForNat = false;
return new Program<>(ret, null);
}
use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class OplParser method toMapping.
private static OplExp toMapping(Object c) {
Tuple3 t = (Tuple3) c;
Tuple3 aa = (Tuple3) t.b;
Tuple3 a = (Tuple3) aa.b;
Tuple3 b = (Tuple3) aa.c;
// Tuple3 b = (Tuple3) t.c;
List<String> imports = aa.a == null ? new LinkedList<>() : (List<String>) ((org.jparsec.functors.Pair) aa.a).b;
List<Tuple3> sorts = (List<Tuple3>) a.b;
List<Tuple3> symbols = (List<Tuple3>) b.b;
Map<String, String> sorts0 = new HashMap<>();
Map<String, Pair<OplCtx<String, String>, OplTerm<String, String>>> symbols0 = new HashMap<>();
for (Tuple3 z : sorts) {
String p = (String) z.a;
if (sorts0.containsKey(p)) {
throw new DoNotIgnore("Duplicate sort: " + p);
}
String q = (String) z.c;
sorts0.put(p, q);
}
for (Tuple3 z : symbols) {
String p = (String) z.a;
if (sorts0.containsKey(p)) {
throw new DoNotIgnore("Duplicate symbol: " + p);
}
org.jparsec.functors.Pair ppp = (org.jparsec.functors.Pair) z.c;
Tuple3 q = (Tuple3) ppp.a;
List<Tuple3> ctx = q == null ? new LinkedList<>() : (List<Tuple3>) q.b;
List<Pair<String, String>> ctx0 = new LinkedList<>();
Set<String> seen = new HashSet<>();
for (Object uu : ctx) {
String name;
String type = null;
if (uu instanceof Tuple3) {
Tuple3 u = (Tuple3) uu;
name = (String) u.a;
type = (String) u.c;
} else {
name = (String) uu;
}
if (seen.contains(name)) {
throw new DoNotIgnore("Duplicate var: " + name);
}
seen.add(name);
ctx0.add(new Pair<>(name, type));
}
OplCtx ccc = new OplCtx<>(ctx0);
symbols0.put(p, new Pair<>(ccc, toTerm(ccc.names(), null, ppp.b, false)));
}
Tuple4 x = (Tuple4) t.c;
String src0 = (String) x.b;
String dst0 = (String) x.d;
OplMapping ret = new OplMapping(sorts0, symbols0, src0, dst0);
ret.imports = new HashSet<>(imports);
return ret;
}
use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class OplParser method toModel2.
private static OplExp toModel2(Object o) {
if (!o.toString().contains("tables")) {
throw new RuntimeException();
}
Tuple3 t = (Tuple3) o;
Tuple3 b = (Tuple3) t.b;
org.jparsec.functors.Pair c = (org.jparsec.functors.Pair) t.c;
Tuple3 y = (Tuple3) b.a;
List<Tuple3> sorts = (List<Tuple3>) y.b;
Map<String, List<String>> sorts0 = new HashMap<>();
for (Tuple3 x : sorts) {
String s = x.a.toString();
List<String> s0 = (List<String>) x.c;
if (sorts0.containsKey(s)) {
throw new DoNotIgnore("Duplicate sort: " + s);
}
sorts0.put(s, s0);
}
Map<String, Map<String, String>> symbols0 = new HashMap<>();
Tuple3 z = (Tuple3) b.b;
List<Tuple3> q = (List<Tuple3>) z.b;
Tuple3 z0 = (Tuple3) b.c;
q.addAll((Collection<? extends Tuple3>) z0.b);
for (Tuple3 r : q) {
List<Tuple5> u = (List<Tuple5>) r.c;
String fname = (String) r.a;
if (symbols0.containsKey(fname)) {
throw new DoNotIgnore("Duplicate symbol " + fname);
}
Map<String, String> toadd = new HashMap<>();
for (Tuple5 e : u) {
String args = (String) e.b;
String ret = (String) e.d;
if (toadd.containsKey(args)) {
throw new DoNotIgnore("Duplicate argument at " + args);
}
toadd.put(args, ret);
}
symbols0.put(fname, toadd);
}
return new OplGround(sorts0, symbols0, c.b.toString());
}
use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class OplParser method toTerm.
private static OplTerm toTerm(Collection vars, Collection consts, Object a, boolean suppressError) {
if (a instanceof List) {
List<String> aa = (List<String>) a;
if (aa.isEmpty()) {
throw new RuntimeException();
}
OplTerm head = toTerm(vars, consts, aa.get(0), suppressError);
// List<String> bb = new LinkedList<>(aa); Collections.reverse(list)
for (int j = 1; j < aa.size(); j++) {
head = new OplTerm(aa.get(j), Util.singList(head));
}
return head;
}
if (a instanceof String) {
String a0 = (String) a;
try {
int i = Integer.parseInt(a0);
if (sugarForNat) {
return OplTerm.natToTerm(i);
}
} catch (Exception e) {
}
if (vars != null && vars.contains(a0)) {
return new OplTerm(a0);
} else if (vars != null && !vars.contains(a0) || consts != null && consts.contains(a0)) {
return new OplTerm(a0, new LinkedList<>());
} else if (consts == null || vars == null) {
return new OplTerm(a0);
} else if (suppressError) {
return new OplTerm(a0, new LinkedList<>());
}
throw new DoNotIgnore(a + " is neither a bound variable nor a (0-ary) constant ");
}
if (a instanceof Tuple5) {
Tuple5 t = (Tuple5) a;
String f = (String) t.c;
List<OplTerm> l0 = new LinkedList<>();
l0.add(toTerm(vars, consts, t.b, suppressError));
l0.add(toTerm(vars, consts, t.d, suppressError));
return new OplTerm(f, l0);
}
Tuple3 t = (Tuple3) a;
String f = (String) t.a;
List<Object> l = (List<Object>) t.c;
List<OplTerm> l0 = l.stream().map(x -> toTerm(vars, consts, x, suppressError)).collect(Collectors.toList());
return new OplTerm(f, l0);
}
use of org.jparsec.functors.Tuple3 in project fql by CategoricalData.
the class OplParser method toCtx.
private static OplCtx<String, String> toCtx(List<Tuple3> fa) {
List<Pair<String, String>> ret = new LinkedList<>();
if (fa == null) {
return new OplCtx<>();
}
for (Object tt : fa) {
if (tt instanceof Tuple3) {
Tuple3 t = (Tuple3) tt;
ret.add(new Pair<>(t.a.toString(), t.c.toString()));
} else {
ret.add(new Pair<>((String) tt, null));
}
}
return new OplCtx<>(ret);
}
Aggregations