Skip to content

Commit

Permalink
Replace bunch of ifs with switch on TermType
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jun 22, 2020
1 parent 930f008 commit 227f78d
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions org.spoofax.terms/src/org/spoofax/terms/TermFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,17 @@ public IStrategoTerm withAnnotations(IStrategoTerm term, @Nullable IStrategoList
return term;
}

// Ordered from most likely to least likely:
if (TermUtils.isAppl(term)) {
return buildAppl(((IStrategoAppl)term).getConstructor(), term.getAllSubterms(), term, annotations);
} else if (TermUtils.isList(term)) {
return buildList(term.getAllSubterms(), term, annotations);
} else if (TermUtils.isString(term)) {
return buildString(((IStrategoString)term).stringValue(), term, annotations);
} else if (TermUtils.isInt(term)) {
return buildInt(((IStrategoInt)term).intValue(), term, annotations);
} else if (TermUtils.isTuple(term)) {
return buildTuple(term.getAllSubterms(), term, annotations);
} else if (TermUtils.isReal(term)) {
return buildReal(((IStrategoReal)term).realValue(), term, annotations);
} else if (term instanceof IStrategoPlaceholder) {
return buildPlaceholder(((IStrategoPlaceholder)term).getTemplate(), term, annotations);
switch(term.getType()) {
case APPL: return buildAppl(((IStrategoAppl)term).getConstructor(), term.getAllSubterms(), term, annotations);
case LIST: return buildList(term.getAllSubterms(), term, annotations);
case INT: return buildInt(((IStrategoInt)term).intValue(), term, annotations);
case REAL: return buildReal(((IStrategoReal)term).realValue(), term, annotations);
case STRING: return buildString(((IStrategoString)term).stringValue(), term, annotations);
case TUPLE: return buildTuple(term.getAllSubterms(), term, annotations);
case PLACEHOLDER: return buildPlaceholder(((IStrategoPlaceholder)term).getTemplate(), term, annotations);
default: throw new UnsupportedOperationException(
"Unable to annotate term of type " + term.getClass().getName() + " in " + getClass().getName());
}

throw new UnsupportedOperationException(
"Unable to annotate term of type " + term.getClass().getName() + " in " + getClass().getName());
}

@Override
Expand Down

0 comments on commit 227f78d

Please sign in to comment.