Search in sources :

Example 1 with Const

use of catdata.fql.decl.TransExp.Const in project fql by CategoricalData.

the class TransChecker method visit.

@SuppressWarnings("unused")
@Override
public Pair<String, String> visit(FQLProgram env, Const e) {
    InstExp src = env.insts.get(e.src);
    if (src == null) {
        throw new RuntimeException("Missing instance " + e.src);
    }
    InstExp dst = env.insts.get(e.dst);
    if (dst == null) {
        throw new RuntimeException("Missing instance " + e.src);
    }
    if (!(src instanceof InstExp.Const)) {
        throw new RuntimeException(e.src + " is not a constant.");
    }
    if (!(dst instanceof InstExp.Const)) {
        throw new RuntimeException(e.dst + " is not a constant.");
    }
    InstExp.Const src0 = (InstExp.Const) src;
    InstExp.Const dst0 = (InstExp.Const) dst;
    SigExp srct = src0.type(env);
    SigExp dstt = dst0.type(env);
    if (!srct.equals(dstt)) {
        throw new RuntimeException("Instances not of same type on " + e + " are " + srct + " and " + dstt);
    }
    Signature sig = srct.toSig(env);
    List<Pair<String, List<Pair<Object, Object>>>> bbb = e.objs;
    try {
        new Transform(new Instance(sig, src0.data), new Instance(sig, dst0.data), bbb);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getLocalizedMessage());
    }
    return new Pair<>(e.src, e.dst);
}
Also used : InstExp(catdata.fql.decl.InstExp) FQLException(catdata.fql.FQLException) SigExp(catdata.fql.decl.SigExp) Instance(catdata.fql.decl.Instance) Const(catdata.fql.decl.TransExp.Const) Signature(catdata.fql.decl.Signature) Transform(catdata.fql.decl.Transform) Pair(catdata.Pair)

Example 2 with Const

use of catdata.fql.decl.TransExp.Const in project fql by CategoricalData.

the class FqlCodeEditor method vedit.

public void vedit() {
    FQLProgram init = tryParse(topArea.getText());
    if (init == null) {
        respArea.setText(toDisplay);
        return;
    }
    if (init.lines.isEmpty()) {
        return;
    }
    String which = null;
    int start = -1;
    int offs = topArea.getCaretPosition();
    int end = -1;
    int i = 0;
    int pos = 0;
    for (String k : init.lines.keySet()) {
        Integer v = init.lines.get(k);
        if (v < offs && v > start) {
            start = v;
            which = k;
            pos = i;
        }
        i++;
    }
    if (which == null) {
        throw new RuntimeException();
    }
    int j = 0;
    for (String k : init.lines.keySet()) {
        if (j == pos + 1) {
            end = init.lines.get(k);
            break;
        }
        j++;
    }
    if (end == -1) {
        end = topArea.getText().length();
    }
    InstExp ie = init.insts.get(which);
    TransExp te = init.transforms.get(which);
    if ((ie == null && te == null) || (ie != null && !(ie instanceof InstExp.Const)) || (te != null && !(te instanceof Const))) {
        respArea.setText("Cannot visually edit " + which + ": only constant instances or transforms are visually editable.");
        return;
    }
    try {
        if (ie != null) {
            InstExp.Const iec = (InstExp.Const) ie;
            InstExp.Const n = new InstanceEditor(which, iec.sig.toSig(init), iec).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "instance " + which + " = " + n + " : " + n.sig + "\n\n";
            topArea.replaceRange(newText, start, end);
        } else {
            Const iec = (Const) te;
            if (iec == null) {
                throw new RuntimeException("Anomaly: please report");
            }
            InstExp.Const s = (InstExp.Const) init.insts.get(iec.src);
            InstExp.Const t = (InstExp.Const) init.insts.get(iec.dst);
            Const n = new TransformEditor(which, init.insts.get(iec.src).type(init).toSig(init), iec, s, t).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "transform " + which + " = " + n + " : " + n.src + " -> " + n.dst + "\n\n";
            topArea.replaceRange(newText, start, end);
        }
    } catch (FQLException fe) {
        fe.printStackTrace();
        respArea.setText(fe.getLocalizedMessage());
    }
}
Also used : InstExp(catdata.fql.decl.InstExp) FQLException(catdata.fql.FQLException) TransExp(catdata.fql.decl.TransExp) Const(catdata.fql.decl.TransExp.Const) InstanceEditor(catdata.fql.decl.InstanceEditor) FQLProgram(catdata.fql.decl.FQLProgram) TransformEditor(catdata.fql.decl.TransformEditor)

Example 3 with Const

use of catdata.fql.decl.TransExp.Const in project fql by CategoricalData.

the class FqlCodeEditor method format.

public void format() {
    String input = topArea.getText();
    FQLProgram p = tryParse(input);
    if (p == null) {
        respArea.setText(toDisplay);
        return;
    }
    if (input.contains("//") || input.contains("/*")) {
        int x = JOptionPane.showConfirmDialog(null, "Formatting will erase all comments - continue?", "Continue?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (x != JOptionPane.YES_OPTION) {
            return;
        }
    }
    // order does not contain enums or drops
    StringBuilder sb = new StringBuilder();
    for (String k : p.enums.keySet()) {
        Type t = p.enums.get(k);
        if (!(t instanceof Type.Enum)) {
            continue;
        }
        Type.Enum e = (Type.Enum) t;
        sb.append("enum ").append(k).append(" = ").append(e.printFull());
        sb.append("\n\n");
    }
    for (String k : p.order) {
        Pair<String, Object> o = get(p, k);
        sb.append(o.first).append(" ").append(k).append(" = ").append(o.second);
        if (o.second instanceof InstExp.Const) {
            InstExp.Const c = (InstExp.Const) o.second;
            sb.append(" : ").append(c.sig);
        } else if (o.second instanceof MapExp.Const) {
            MapExp.Const c = (MapExp.Const) o.second;
            sb.append(" : ").append(c.src).append(" -> ").append(c.dst);
        } else if (o.second instanceof Const) {
            Const c = (Const) o.second;
            sb.append(" : ").append(c.src).append(" -> ").append(c.dst);
        }
        sb.append("\n\n");
    }
    if (!p.drop.isEmpty()) {
        sb.append("drop ").append(PrettyPrinter.sep0(" ", p.drop)).append("\n\n");
    }
    topArea.setText(sb.toString().trim());
    topArea.setCaretPosition(0);
}
Also used : Const(catdata.fql.decl.TransExp.Const) InstExp(catdata.fql.decl.InstExp) MapExp(catdata.fql.decl.MapExp) Type(catdata.fql.decl.Type) FQLProgram(catdata.fql.decl.FQLProgram)

Aggregations

InstExp (catdata.fql.decl.InstExp)3 Const (catdata.fql.decl.TransExp.Const)3 FQLException (catdata.fql.FQLException)2 FQLProgram (catdata.fql.decl.FQLProgram)2 Pair (catdata.Pair)1 Instance (catdata.fql.decl.Instance)1 InstanceEditor (catdata.fql.decl.InstanceEditor)1 MapExp (catdata.fql.decl.MapExp)1 SigExp (catdata.fql.decl.SigExp)1 Signature (catdata.fql.decl.Signature)1 TransExp (catdata.fql.decl.TransExp)1 Transform (catdata.fql.decl.Transform)1 TransformEditor (catdata.fql.decl.TransformEditor)1 Type (catdata.fql.decl.Type)1