Search in sources :

Example 1 with TOption

use of org.spoofax.terms.typesmart.types.TOption 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)1 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)1 SortType (org.spoofax.terms.typesmart.types.SortType)1 TList (org.spoofax.terms.typesmart.types.TList)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