Skip to content

Commit

Permalink
Merge pull request #51 from spoofax-shell/refactor-inputresult
Browse files Browse the repository at this point in the history
[RDY] Refactor InputResult out of AbstractSpoofaxResult hierarchy
  • Loading branch information
gfokkema authored Jun 17, 2016
2 parents 4c8302f + 9956a4f commit b85700e
Show file tree
Hide file tree
Showing 24 changed files with 527 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import org.metaborg.spoofax.shell.core.DynSemEvaluationStrategy;
import org.metaborg.spoofax.shell.core.IEvaluationStrategy;
import org.metaborg.spoofax.shell.core.IInterpreterLoader;
import org.metaborg.spoofax.shell.functions.AEvalFunction;
import org.metaborg.spoofax.shell.functions.ATransformFunction;
import org.metaborg.spoofax.shell.functions.AnalyzeFunction;
import org.metaborg.spoofax.shell.functions.EvaluateFunction;
import org.metaborg.spoofax.shell.functions.FailableFunction;
import org.metaborg.spoofax.shell.functions.IFunctionFactory;
import org.metaborg.spoofax.shell.functions.InputFunction;
import org.metaborg.spoofax.shell.functions.PEvalFunction;
import org.metaborg.spoofax.shell.functions.PTransformFunction;
import org.metaborg.spoofax.shell.functions.ParseFunction;
import org.metaborg.spoofax.shell.invoker.ICommandInvoker;
Expand All @@ -35,6 +34,7 @@
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.IResultFactory;
import org.metaborg.spoofax.shell.output.IResultVisitor;
import org.metaborg.spoofax.shell.output.ISpoofaxTermResult;
import org.metaborg.spoofax.shell.output.InputResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.TransformResult;
Expand Down Expand Up @@ -94,8 +94,6 @@ protected void bindFactories() {
.implement(TransformResult.class, Names.named("parsed"), TransformResult.Parsed.class)
.implement(TransformResult.class,
Names.named("analyzed"), TransformResult.Analyzed.class)
.implement(EvaluateResult.class, Names.named("parsed"), EvaluateResult.Parsed.class)
.implement(EvaluateResult.class, Names.named("analyzed"), EvaluateResult.Analyzed.class)
.build(IResultFactory.class));

install(new FactoryModuleBuilder()
Expand All @@ -109,10 +107,9 @@ protected void bindFactories() {
PTransformFunction.class)
.implement(new TypeLiteral<FailableFunction<AnalyzeResult, TransformResult, IResult>>() { },
ATransformFunction.class)
.implement(new TypeLiteral<FailableFunction<ParseResult, EvaluateResult, IResult>>() { },
PEvalFunction.class)
.implement(new TypeLiteral<FailableFunction<AnalyzeResult, EvaluateResult, IResult>>() { },
AEvalFunction.class)
.implement(new TypeLiteral<FailableFunction<ISpoofaxTermResult<?>,
EvaluateResult, IResult>>() { },
EvaluateFunction.class)
.build(IFunctionFactory.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ private FailableFunction<String, AnalyzeResult, IResult> analyzeFunction() {
}

private FailableFunction<String, EvaluateResult, IResult> pEvaluateFunction() {
return parseFunction().kleisliCompose(functionFactory.createPEvalFunction(project, lang));
return parseFunction()
.kleisliCompose(functionFactory.createEvaluateFunction(project, lang));
}

private FailableFunction<String, EvaluateResult, IResult> aEvaluateFunction() {
return analyzeFunction().kleisliCompose(functionFactory.createAEvalFunction(project, lang));
return analyzeFunction()
.kleisliCompose(functionFactory.createEvaluateFunction(project, lang));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.metaborg.meta.lang.dynsem.interpreter.terms.ITerm;
import org.metaborg.spoofax.core.terms.ITermFactoryService;
import org.metaborg.spoofax.shell.core.IInterpreterLoader.InterpreterLoadException;
import org.metaborg.spoofax.shell.output.AnalyzeResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.util.StrategoUtil;
import org.spoofax.interpreter.core.Tools;
import org.spoofax.interpreter.terms.IStrategoAppl;
Expand All @@ -42,8 +40,8 @@ public class DynSemEvaluationStrategy implements IEvaluationStrategy {

/**
* Construct a new {@link DynSemEvaluationStrategy}. This does not yet load the interpreter for
* the language. Rather, this is done when first invoking
* {@link #evaluate(AnalyzeResult, IContext)} or {@link #evaluate(ParseResult, IContext)}.
* the language. Rather, this is done when invoking {@link #evaluate(IStrategoTerm, IContext)}
* for the first time.
*
* @param interpLoader
* The loader for a generated DynSem interpreter.
Expand All @@ -63,14 +61,8 @@ public String name() {
}

@Override
public IStrategoTerm evaluate(ParseResult parsed, IContext context) throws MetaborgException {
return evaluate(parsed.ast().get(), context.language());
}

@Override
public IStrategoTerm evaluate(AnalyzeResult analyzed, IContext context)
throws MetaborgException {
return evaluate(analyzed.ast().get(), context.language());
public IStrategoTerm evaluate(IStrategoTerm term, IContext context) throws MetaborgException {
return evaluate(term, context.language());
}

private IStrategoTerm evaluate(IStrategoTerm input, ILanguageImpl langImpl)
Expand Down Expand Up @@ -168,8 +160,7 @@ private void initialize(ILanguageImpl langImpl) throws InterpreterLoadException
initializeExecutionEnvironment();
}

private void initializeExecutionEnvironment()
throws InterpreterLoadException {
private void initializeExecutionEnvironment() throws InterpreterLoadException {
ITermFactory termFactory = termFactService.getGeneric();
IStrategoConstructor termConstr = termFactory.makeConstructor("ShellInit", 0);
IStrategoAppl shellInitAppl = termFactory.makeAppl(termConstr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.metaborg.core.MetaborgException;
import org.metaborg.core.context.IContext;
import org.metaborg.spoofax.shell.output.AnalyzeResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.spoofax.interpreter.terms.IStrategoTerm;

/**
Expand All @@ -18,19 +16,11 @@ public interface IEvaluationStrategy {

/**
* Evaluate the given Stratego term using this strategy.
* @param parsed The parsed input.
* @param term The input term.
* @param context The {@link IContext}.
* @return The output Stratego term.
* @throws MetaborgException When evaluation fails for one reason or another.
*/
IStrategoTerm evaluate(ParseResult parsed, IContext context) throws MetaborgException;
IStrategoTerm evaluate(IStrategoTerm term, IContext context) throws MetaborgException;

/**
* Evaluate the given Stratego term using this strategy.
* @param analyzed The analyzed input.
* @param context The {@link IContext}.
* @return The output Stratego term.
* @throws MetaborgException When evaluation fails for one reason or another.
*/
IStrategoTerm evaluate(AnalyzeResult analyzed, IContext context) throws MetaborgException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import org.metaborg.core.project.IProject;
import org.metaborg.spoofax.core.shell.ShellFacet;
import org.metaborg.spoofax.shell.core.IEvaluationStrategy;
import org.metaborg.spoofax.shell.output.AnalyzeResult;
import org.metaborg.spoofax.shell.output.EvaluateResult;
import org.metaborg.spoofax.shell.output.FailOrSuccessResult;
import org.metaborg.spoofax.shell.output.FailResult;
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.IResultFactory;
import org.metaborg.spoofax.shell.output.ISpoofaxTermResult;
import org.spoofax.interpreter.terms.IStrategoTerm;

import com.google.inject.Inject;
Expand All @@ -22,7 +23,8 @@
/**
* Creates an {@link EvaluateResult} from a given {@link AnalyzeResult}.
*/
public class AEvalFunction extends ContextualSpoofaxFunction<AnalyzeResult, EvaluateResult> {
public class EvaluateFunction extends ContextualSpoofaxFunction<ISpoofaxTermResult<?>,
EvaluateResult> {
private final Map<String, IEvaluationStrategy> evaluationStrategies;

/**
Expand All @@ -40,7 +42,7 @@ public class AEvalFunction extends ContextualSpoofaxFunction<AnalyzeResult, Eval
* The {@link ILanguageImpl} to which this command applies.
*/
@Inject
public AEvalFunction(Map<String, IEvaluationStrategy> evaluationStrategies,
public EvaluateFunction(Map<String, IEvaluationStrategy> evaluationStrategies,
IContextService contextService, IResultFactory resultFactory,
@Assisted IProject project, @Assisted ILanguageImpl lang) {
super(contextService, resultFactory, project, lang);
Expand All @@ -49,14 +51,18 @@ public AEvalFunction(Map<String, IEvaluationStrategy> evaluationStrategies,

@Override
protected FailOrSuccessResult<EvaluateResult, IResult>
applyThrowing(IContext context, AnalyzeResult a) throws Exception {
applyThrowing(IContext context, ISpoofaxTermResult<?> a) throws Exception {
if (!a.ast().isPresent()) {
return FailOrSuccessResult.failed(new FailResult(a));
}
ShellFacet facet = context.language().facet(ShellFacet.class);
if (facet == null) {
throw new MetaborgException("Cannot find the shell facet.");
}

IEvaluationStrategy evalStrategy = evaluationStrategies.get(facet.getEvaluationMethod());
IStrategoTerm result = evalStrategy.evaluate(a, context);
IStrategoTerm result = evalStrategy.evaluate(a.ast().get(), context);

return FailOrSuccessResult.ofSpoofaxResult(resultFactory.createEvaluateResult(a, result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface FailableFunction<In, Success extends IResult, Fail extends IRes
* The return type of a successful result.
*/
default <NewSuccess extends IResult> FailableFunction<In, NewSuccess, Fail>
kleisliCompose(FailableFunction<Success, NewSuccess, Fail> other) {
kleisliCompose(FailableFunction<? super Success, NewSuccess, Fail> other) {
Objects.requireNonNull(other);
return a -> this.apply(a).flatMap(other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.metaborg.spoofax.shell.output.AnalyzeResult;
import org.metaborg.spoofax.shell.output.EvaluateResult;
import org.metaborg.spoofax.shell.output.IResult;
import org.metaborg.spoofax.shell.output.ISpoofaxTermResult;
import org.metaborg.spoofax.shell.output.InputResult;
import org.metaborg.spoofax.shell.output.ParseResult;
import org.metaborg.spoofax.shell.output.TransformResult;
Expand Down Expand Up @@ -69,22 +70,13 @@ public interface IFunctionFactory {
ITransformAction action);

/**
* Factory method for creating a {@link PEvalFunction} from a {@link ParseFunction}.
* Factory method for creating a {@link EvaluateFunction}.
* @param project The associated {@link IProject}
* @param lang The associated {@link ILanguageImpl}
* @return an {@link PEvalFunction}
*/
FailableFunction<ParseResult, EvaluateResult, IResult>
createPEvalFunction(IProject project, ILanguageImpl lang);

/**
* Factory method for creating an {@link AEvalFunction} from an {@link AnalyzeFunction}.
* @param project The associated {@link IProject}
* @param lang The associated {@link ILanguageImpl}
* @return an {@link AEvalFunction}
*/
FailableFunction<AnalyzeResult, EvaluateResult, IResult>
createAEvalFunction(IProject project, ILanguageImpl lang);
FailableFunction<ISpoofaxTermResult<?>, EvaluateResult, IResult>
createEvaluateFunction(IProject project, ILanguageImpl lang);

/**
* Factory method for creating a {@link CommandBuilder}.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@

import org.apache.commons.vfs2.FileObject;
import org.metaborg.core.unit.IUnit;
import org.metaborg.spoofax.core.stratego.IStrategoCommon;
import org.metaborg.spoofax.shell.commands.IReplCommand;
import org.spoofax.interpreter.terms.IStrategoTerm;

/**
* Represents an {@link AbstractSpoofaxResult} as returned by the {@link IReplCommand}.
* Wraps Spoofax {@link IUnit} of various types.
* @param <T> the wrapped subtype of {@link IUnit}
*/
public abstract class AbstractSpoofaxResult<T extends IUnit> implements ISpoofaxResult<IUnit> {
private final IStrategoCommon common;
public abstract class AbstractSpoofaxResult<T extends IUnit> implements ISpoofaxResult<T> {
private final T unit;

/**
* Constructor for an {@link AbstractSpoofaxResult}.
* @param common the {@link IStrategoCommon} service
* Constructor for an {@link AbstractResult}.
* @param unit the wrapped {@link IUnit}
*/
public AbstractSpoofaxResult(IStrategoCommon common, T unit) {
this.common = common;
public AbstractSpoofaxResult(T unit) {
this.unit = unit;
}

Expand All @@ -34,21 +28,4 @@ public FileObject source() {
public T unit() {
return unit;
}

/**
* Returns a textual representation of a term.
* @param term the term
* @return a string
*/
public StyledText toString(IStrategoTerm term) {
return new StyledText(common.toString(term));
}

@Override
public StyledText styled() {
if (!valid() || !ast().isPresent()) {
return new StyledText(messages().toString());
}
return toString(ast().get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.metaborg.spoofax.shell.output;

import org.metaborg.core.unit.IUnit;
import org.metaborg.spoofax.core.stratego.IStrategoCommon;
import org.spoofax.interpreter.terms.IStrategoTerm;

/**
* Represents an {@link AbstractResult} as returned by the {@link SpoofaxCommand}.
* Wraps Spoofax {@link IUnit} of various types. Additionally, stores an AST.
* @param <T> the wrapped subtype of {@link IUnit}
*/
//@formatter:off
public abstract class AbstractSpoofaxTermResult<T extends IUnit>
extends AbstractSpoofaxResult<T> implements ISpoofaxTermResult<T> {
//@formatter:on
private final IStrategoCommon common;

/**
* Constructor for an {@link AbstractResult}.
* @param common the {@link IStrategoCommon} service.
* @param unit the wrapped {@link IUnit}.
*/
public AbstractSpoofaxTermResult(IStrategoCommon common, T unit) {
super(unit);
this.common = common;
}

/**
* Returns a textual representation of a term.
* @param term the term
* @return a string
*/
public StyledText toString(IStrategoTerm term) {
return new StyledText(common.toString(term));
}

@Override
public StyledText styled() {
if (!valid() || !ast().isPresent()) {
return new StyledText(messages().toString());
}
return toString(ast().get());
}

}
Loading

0 comments on commit b85700e

Please sign in to comment.