Search in sources :

Example 36 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class ScopeGraphComponent method solve.

private boolean solve(CGDirectEdge c) {
    ITerm sourceScopeRep = unifier().findRecursive(c.getSourceScope());
    if (!sourceScopeRep.isGround()) {
        return false;
    }
    Scope sourceScope = Scope.matcher().match(sourceScopeRep, unifier()).orElseThrow(() -> new TypeException("Expected a scope but got " + sourceScopeRep));
    return findScope(c.getTargetScope()).map(targetScope -> {
        scopeGraph.addDirectEdge(sourceScope, c.getLabel(), targetScope);
        return true;
    }).orElseGet(() -> {
        scopeGraph.addIncompleteDirectEdge(sourceScope, c.getLabel(), c.getTargetScope());
        return true;
    });
}
Also used : TypeException(mb.nabl2.solver.TypeException) ASolver(mb.nabl2.solver.ASolver) ITerm(mb.nabl2.terms.ITerm) CGDirectEdge(mb.nabl2.constraints.scopegraph.CGDirectEdge) IScopeGraphConstraint(mb.nabl2.constraints.scopegraph.IScopeGraphConstraint) IEsopScopeGraph(mb.nabl2.scopegraph.esop.IEsopScopeGraph) Scope(mb.nabl2.scopegraph.terms.Scope) CGExportEdge(mb.nabl2.constraints.scopegraph.CGExportEdge) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) CGRef(mb.nabl2.constraints.scopegraph.CGRef) SolveResult(mb.nabl2.solver.ISolver.SolveResult) SolverCore(mb.nabl2.solver.SolverCore) CGDecl(mb.nabl2.constraints.scopegraph.CGDecl) ISolver(mb.nabl2.solver.ISolver) Label(mb.nabl2.scopegraph.terms.Label) SeedResult(mb.nabl2.solver.ISolver.SeedResult) CGImportEdge(mb.nabl2.constraints.scopegraph.CGImportEdge) Optional(java.util.Optional) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) TypeException(mb.nabl2.solver.TypeException)

Example 37 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class Args method build.

public static ITerm build(Args args) {
    List<ITerm> paramTerms = args.getParams();
    ITerm paramsTerm;
    if (paramTerms.size() == 1) {
        paramsTerm = paramTerms.get(0);
    } else {
        paramsTerm = B.newTuple(paramTerms);
    }
    return args.getType().map(typeTerm -> B.newAppl(PARAMS_AND_TYPE, paramsTerm, typeTerm)).orElseGet(() -> B.newAppl(PARAMS, paramsTerm));
// @formatter:on
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Serial(org.immutables.serial.Serial) Value(org.immutables.value.Value) ITerm(mb.nabl2.terms.ITerm) M(mb.nabl2.terms.matching.TermMatch.M) Optional(java.util.Optional) IMatcher(mb.nabl2.terms.matching.TermMatch.IMatcher) ITerm(mb.nabl2.terms.ITerm)

Example 38 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class AnalysisPrimitive method call.

public Optional<? extends IStrategoTerm> call(IScopeGraphUnit unit, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    final StrategoTerms strategoTerms = new StrategoTerms(factory);
    final List<ITerm> terms = sterms.stream().skip(1).map(strategoTerms::fromStratego).map(ConstraintTerms::specialize).collect(Collectors.toList());
    final ITerm term = ConstraintTerms.specialize(strategoTerms.fromStratego(sterm));
    Optional<? extends ITerm> result = call(unit, term, terms);
    return result.map(ConstraintTerms::explicate).map(strategoTerms::toStratego);
}
Also used : StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITerm(mb.nabl2.terms.ITerm)

Example 39 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class ScopeGraphLogPrimitive method call.

@Override
public Optional<ITerm> call(IScopeGraphContext<?> context, ITerm term, List<ITerm> terms) throws InterpreterException {
    final ITerm messageTerm = terms.get(0);
    final String message = M.stringValue().match(messageTerm).orElseGet(() -> messageTerm.toString());
    logger.log(level(context), "{}", message);
    return fatal() ? Optional.empty() : Optional.of(term);
}
Also used : ITerm(mb.nabl2.terms.ITerm)

Example 40 with ITerm

use of mb.nabl2.terms.ITerm in project nabl by metaborg.

the class StrategoTerms method toStratego.

// to
public IStrategoTerm toStratego(ITerm term) {
    IStrategoTerm strategoTerm = term.match(Terms.cases(// @formatter:off
    appl -> {
        List<IStrategoTerm> args = appl.getArgs().stream().map(arg -> toStratego(arg)).collect(Collectors.toList());
        IStrategoTerm[] argArray = args.toArray(new IStrategoTerm[args.size()]);
        return appl.getOp().equals(Terms.TUPLE_OP) ? termFactory.makeTuple(argArray) : termFactory.makeAppl(termFactory.makeConstructor(appl.getOp(), appl.getArity()), argArray);
    }, list -> toStrategoList(list), string -> termFactory.makeString(string.getValue()), integer -> termFactory.makeInt(integer.getValue()), blob -> new StrategoBlob(blob.getValue()), var -> {
        throw new IllegalArgumentException("Cannot convert specialized terms to Stratego.");
    }));
    strategoTerm = putAttachments(strategoTerm, term.getAttachments());
    return strategoTerm;
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) Arrays(java.util.Arrays) IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) Function1(org.metaborg.util.functions.Function1) ListTerms(mb.nabl2.terms.ListTerms) Builder(com.google.common.collect.ImmutableClassToInstanceMap.Builder) ITerm(mb.nabl2.terms.ITerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) Collectors(java.util.stream.Collectors) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoReal(org.spoofax.interpreter.terms.IStrategoReal) List(java.util.List) Terms(mb.nabl2.terms.Terms) Lists(com.google.common.collect.Lists) IListTerm(mb.nabl2.terms.IListTerm) Optional(java.util.Optional) LinkedList(java.util.LinkedList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

ITerm (mb.nabl2.terms.ITerm)79 Test (org.junit.Test)24 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)19 Scope (mb.nabl2.scopegraph.terms.Scope)18 SolveResult (mb.nabl2.solver.ISolver.SolveResult)16 Optional (java.util.Optional)15 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)15 ITermVar (mb.nabl2.terms.ITermVar)15 Label (mb.nabl2.scopegraph.terms.Label)14 Map (java.util.Map)12 Set (java.util.Set)10 MessageContent (mb.nabl2.constraints.messages.MessageContent)10 SolverCore (mb.nabl2.solver.SolverCore)9 IUnifier (mb.nabl2.terms.unification.IUnifier)9 Function1 (org.metaborg.util.functions.Function1)9 Lists (com.google.common.collect.Lists)8 ISolution (mb.nabl2.solver.ISolution)8 HashMultimap (com.google.common.collect.HashMultimap)7 Multimap (com.google.common.collect.Multimap)7 IConstraint (mb.nabl2.constraints.IConstraint)7