Search in sources :

Example 1 with SortType

use of org.spoofax.terms.typesmart.types.SortType in project spoofax by metaborg.

the class Typesmart method processSignature.

private void processSignature(IStrategoTerm sigDecls) {
    for (IStrategoTerm decl : sigDecls) {
        String declName = ((IStrategoAppl) decl).getName();
        if (!declName.equals("Constructors")) {
            continue;
        }
        next_constr: for (IStrategoTerm constr : decl.getSubterm(0)) {
            String kind = ((IStrategoAppl) constr).getName();
            String cname;
            IStrategoAppl typeTerm;
            if (kind.equals("OpDeclInj") || kind.equals("ExtOpDeclInj")) {
                cname = "";
                typeTerm = (IStrategoAppl) constr.getSubterm(0);
            } else {
                cname = ((IStrategoString) constr.getSubterm(0)).stringValue();
                typeTerm = (IStrategoAppl) constr.getSubterm(1);
            }
            List<SortType> sortTypes;
            if (typeTerm.getName().equals("ConstType")) {
                // no constructor arguments
                sortTypes = new ArrayList<>(1);
                SortType t = extractSortType(typeTerm.getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else if (typeTerm.getName().equals("FunType")) {
                IStrategoTerm[] argTypes = typeTerm.getSubterm(0).getAllSubterms();
                sortTypes = new ArrayList<>(argTypes.length + 1);
                for (IStrategoTerm argType : argTypes) {
                    SortType t = extractSortType(argType.getSubterm(0));
                    if (t == null) {
                        continue next_constr;
                    }
                    sortTypes.add(t);
                }
                SortType t = extractSortType(typeTerm.getSubterm(1).getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else {
                throw new IllegalArgumentException("Found constructor declaration in unexpected format " + constr);
            }
            addConstructorSignature(cname, sortTypes);
        }
    }
}
Also used : SortType(org.spoofax.terms.typesmart.types.SortType) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ArrayList(java.util.ArrayList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ArrayList(java.util.ArrayList) TList(org.spoofax.terms.typesmart.types.TList) LinkedList(java.util.LinkedList) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 2 with SortType

use of org.spoofax.terms.typesmart.types.SortType in project spoofax by metaborg.

the class Typesmart method extractSortType.

private SortType extractSortType(IStrategoTerm sort) {
    String kind = ((IStrategoAppl) sort).getName();
    if (kind.equals("SortList") || kind.equals("SortListTl") || kind.equals("SortVar")) {
        logger.error("Unsupported Stratego signature: " + sort);
        return TAny.instance;
    } else if (kind.equals("SortTuple")) {
        IStrategoTerm[] kids = sort.getSubterm(0).getAllSubterms();
        SortType[] sorts = new SortType[kids.length];
        for (int i = 0; i < kids.length; i++) {
            sorts[i] = extractSortType(kids[i]);
        }
        return new TTuple(sorts);
    }
    if (sort.getSubterm(0).getTermType() != IStrategoTerm.STRING) {
        throw new IllegalArgumentException("Found type in unexpected format " + sort);
    }
    String sortName = ((IStrategoString) sort.getSubterm(0)).stringValue();
    if (kind.equals("SortNoArgs") && sortName.equals(SortType.LEXICAL_SORT)) {
        return TLexical.instance;
    } else if (kind.equals("SortNoArgs") && sortName.equals(SortType.ANY_SORT)) {
        return TAny.instance;
    } else if (kind.equals("SortNoArgs")) {
        return new TSort(sortName);
    } else if (kind.equals("Sort") && sortName.equals("List")) {
        SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
        return t == null ? null : new TList(t);
    } else if (kind.equals("Sort") && sortName.equals("Option")) {
        SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
        return t == null ? null : new TOption(t);
    } else if (kind.equals("SortVar")) {
        return null;
    } else {
        throw new IllegalArgumentException("Found type in unexpected format " + sort);
    }
}
Also used : SortType(org.spoofax.terms.typesmart.types.SortType) TList(org.spoofax.terms.typesmart.types.TList) TOption(org.spoofax.terms.typesmart.types.TOption) TSort(org.spoofax.terms.typesmart.types.TSort) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) TTuple(org.spoofax.terms.typesmart.types.TTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Aggregations

IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)2 SortType (org.spoofax.terms.typesmart.types.SortType)2 TList (org.spoofax.terms.typesmart.types.TList)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)1 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)1 TOption (org.spoofax.terms.typesmart.types.TOption)1 TSort (org.spoofax.terms.typesmart.types.TSort)1 TTuple (org.spoofax.terms.typesmart.types.TTuple)1