Skip to content

Commit 58c55d5

Browse files
committed
Remove EvaluateUnit and replace with wrapped {Parse,Analyze}Result in EvaluateResult
1 parent 41fe255 commit 58c55d5

File tree

4 files changed

+90
-44
lines changed

4 files changed

+90
-44
lines changed

org.metaborg.spoofax.shell.core/src/main/java/org/metaborg/spoofax/shell/commands/EvaluateCommand.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import org.metaborg.core.language.ILanguageImpl;
1010
import org.metaborg.core.project.IProject;
1111
import org.metaborg.spoofax.core.shell.ShellFacet;
12-
import org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit;
13-
import org.metaborg.spoofax.core.unit.ISpoofaxParseUnit;
14-
import org.metaborg.spoofax.core.unit.UnitWrapper;
15-
import org.metaborg.spoofax.shell.core.EvaluateUnit;
1612
import org.metaborg.spoofax.shell.core.IEvaluationStrategy;
1713
import org.metaborg.spoofax.shell.hooks.IResultHook;
1814
import org.metaborg.spoofax.shell.invoker.ICommandFactory;
@@ -61,12 +57,7 @@ public EvaluateResult performEvaluation(IContext context, ParseResult parsed,
6157
throws MetaborgException {
6258
AnalyzeResult analyzed = analyzeCommand.analyze(parsed);
6359
IStrategoTerm ast = evalStrategy.evaluate(analyzed, context);
64-
// TODO: Normally this is done in the unit service, but since this is my own Unit this
65-
// is not possible.
66-
EvaluateUnit<ISpoofaxAnalyzeUnit> unit =
67-
new EvaluateUnit<ISpoofaxAnalyzeUnit>(((UnitWrapper) analyzed.unit()).unit, ast,
68-
context, analyzed.unit());
69-
return unitFactory.createEvaluateResult(unit);
60+
return unitFactory.createEvaluateResult(analyzed, ast);
7061
}
7162
}
7263

@@ -79,10 +70,7 @@ public EvaluateResult performEvaluation(IContext context, ParseResult parsed,
7970
IEvaluationStrategy evalStrategy)
8071
throws MetaborgException {
8172
IStrategoTerm ast = evalStrategy.evaluate(parsed, context);
82-
EvaluateUnit<ISpoofaxParseUnit> unit =
83-
new EvaluateUnit<ISpoofaxParseUnit>(((UnitWrapper) parsed.unit()).unit, ast,
84-
context, parsed.unit());
85-
return unitFactory.createEvaluateResult(unit);
73+
return unitFactory.createEvaluateResult(parsed, ast);
8674
}
8775
}
8876

@@ -103,8 +91,7 @@ public EvaluateResult performEvaluation(IContext context, ParseResult parsed,
10391
@Inject
10492
// CHECKSTYLE.OFF: |
10593
public EvaluateCommand(IContextService contextService, ICommandFactory commandFactory,
106-
IResultFactory unitFactory,
107-
IResultHook resultHook,
94+
IResultFactory unitFactory, IResultHook resultHook,
10895
@Assisted IProject project, @Assisted ILanguageImpl lang,
10996
@Assisted boolean analyzed) {
11097
// CHECKSTYLE.ON: |

org.metaborg.spoofax.shell.core/src/main/java/org/metaborg/spoofax/shell/core/ReplModule.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import org.metaborg.spoofax.shell.invoker.ICommandFactory;
1717
import org.metaborg.spoofax.shell.invoker.ICommandInvoker;
1818
import org.metaborg.spoofax.shell.invoker.SpoofaxCommandInvoker;
19+
import org.metaborg.spoofax.shell.output.EvaluateResult;
1920
import org.metaborg.spoofax.shell.output.IResultFactory;
2021

2122
import com.google.common.io.Files;
2223
import com.google.inject.Provides;
2324
import com.google.inject.Singleton;
2425
import com.google.inject.assistedinject.FactoryModuleBuilder;
2526
import com.google.inject.multibindings.MapBinder;
27+
import com.google.inject.name.Names;
2628

2729
/**
2830
* Client library bindings.
@@ -73,7 +75,10 @@ protected void configure() {
7375
bindEvalStrategies(evalStrategyBinder);
7476

7577
install(new FactoryModuleBuilder().build(ICommandFactory.class));
76-
install(new FactoryModuleBuilder().build(IResultFactory.class));
78+
install(new FactoryModuleBuilder()
79+
.implement(EvaluateResult.class, Names.named("parsed"), EvaluateResult.Parsed.class)
80+
.implement(EvaluateResult.class, Names.named("analyzed"), EvaluateResult.Analyzed.class)
81+
.build(IResultFactory.class));
7782
}
7883

7984
/**
@@ -84,12 +89,13 @@ protected void configure() {
8489
* @param projectService
8590
* the Spoofax {@link ISimpleProjectService}
8691
* @return an {@link IProject}
87-
* @throws MetaborgException when creating a project failed
92+
* @throws MetaborgException
93+
* when creating a project failed
8894
*/
8995
@Provides
9096
protected IProject project(IResourceService resourceService,
9197
ISimpleProjectService projectService)
92-
throws MetaborgException {
98+
throws MetaborgException {
9399
FileObject resolve = resourceService.resolve(Files.createTempDir());
94100
return projectService.create(resolve);
95101
}
Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,99 @@
11
package org.metaborg.spoofax.shell.output;
22

3-
import java.util.Collections;
43
import java.util.List;
54
import java.util.Optional;
65

76
import org.metaborg.core.context.IContext;
87
import org.metaborg.core.messages.IMessage;
8+
import org.metaborg.core.unit.IUnit;
99
import org.metaborg.spoofax.core.stratego.IStrategoCommon;
1010
import org.metaborg.spoofax.shell.commands.EvaluateCommand;
11-
import org.metaborg.spoofax.shell.core.EvaluateUnit;
1211
import org.spoofax.interpreter.terms.IStrategoTerm;
1312

13+
import com.google.inject.Inject;
1414
import com.google.inject.assistedinject.Assisted;
15-
import com.google.inject.assistedinject.AssistedInject;
1615

1716
/**
1817
* The result of the execution of an {@link EvaluateCommand}.
1918
*/
20-
public class EvaluateResult extends AbstractSpoofaxResult<EvaluateUnit<?>> {
19+
public abstract class EvaluateResult extends AbstractSpoofaxResult<IUnit> {
20+
private AbstractSpoofaxResult<?> wrappedDelegate;
21+
private IStrategoTerm result;
2122

2223
/**
23-
* Create a {@link EvaluateResult}.
24-
*
25-
* @param common
26-
* the {@link IStrategoCommon} service
27-
* @param unit
28-
* the wrapped {@link EvaluateUnit}
24+
* The result of the evaluation of an analyzed AST.
2925
*/
30-
@AssistedInject
31-
public EvaluateResult(IStrategoCommon common, @Assisted EvaluateUnit<?> unit) {
32-
super(common, unit);
26+
public static class Analyzed extends EvaluateResult {
27+
28+
/**
29+
* Create a {@link EvaluateResult}.
30+
*
31+
* @param common
32+
* the {@link IStrategoCommon} service
33+
* @param analyzed
34+
* the wrapped {@link AnalyzeResult}.
35+
* @param result
36+
* the result of the evaluation.
37+
*/
38+
@Inject
39+
public Analyzed(IStrategoCommon common, @Assisted AnalyzeResult analyzed,
40+
@Assisted IStrategoTerm result) {
41+
super(common, analyzed, result);
42+
}
43+
}
44+
45+
/**
46+
* The result of the evaluation of a parsed, but not analyzed, AST.
47+
*/
48+
public static class Parsed extends EvaluateResult {
49+
50+
/**
51+
* Create a {@link EvaluateResult}.
52+
*
53+
* @param common
54+
* the {@link IStrategoCommon} service
55+
* @param parsed
56+
* the wrapped {@link ParseResult}.
57+
* @param result
58+
* the result of the evaluation.
59+
*/
60+
@Inject
61+
public Parsed(IStrategoCommon common, @Assisted ParseResult parsed,
62+
@Assisted IStrategoTerm result) {
63+
super(common, parsed, result);
64+
}
65+
}
66+
67+
private <T extends IUnit> EvaluateResult(IStrategoCommon common,
68+
AbstractSpoofaxResult<T> wrappedResult,
69+
IStrategoTerm result) {
70+
super(common, wrappedResult.unit());
71+
this.wrappedDelegate = wrappedResult;
72+
this.result = result;
3373
}
3474

3575
@Override
3676
public Optional<IStrategoTerm> ast() {
37-
return Optional.of(unit().ast());
77+
return Optional.of(result);
3878
}
3979

4080
@Override
41-
public Optional<IContext> context() {
42-
return Optional.of(unit().context());
81+
public StyledText styled() {
82+
return toString(ast().get());
4383
}
4484

4585
@Override
46-
public List<IMessage> messages() {
47-
return Collections.emptyList();
86+
public boolean valid() {
87+
return wrappedDelegate.valid();
4888
}
4989

5090
@Override
51-
public StyledText styled() {
52-
return toString(unit().ast());
91+
public Optional<IContext> context() {
92+
return wrappedDelegate.context();
5393
}
5494

5595
@Override
56-
public boolean valid() {
57-
return true;
96+
public List<IMessage> messages() {
97+
return wrappedDelegate.messages();
5898
}
5999
}

org.metaborg.spoofax.shell.core/src/main/java/org/metaborg/spoofax/shell/output/IResultFactory.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import org.metaborg.spoofax.core.unit.ISpoofaxInputUnit;
99
import org.metaborg.spoofax.core.unit.ISpoofaxParseUnit;
1010
import org.metaborg.spoofax.core.unit.ISpoofaxTransformUnit;
11-
import org.metaborg.spoofax.shell.core.EvaluateUnit;
11+
import org.spoofax.interpreter.terms.IStrategoTerm;
12+
13+
import com.google.inject.name.Named;
1214

1315
/**
1416
* Factory that creates {@link ISpoofaxResult}.
@@ -65,9 +67,20 @@ InputResult createInputResult(ILanguageImpl lang, FileObject file, String source
6567

6668
/**
6769
* Create an {@link EvaluateResult} that can be passed to the Repl.
68-
* @param unit the wrapped {@link EvaluateUnit}
70+
* @param parsed the wrapped {@link ParseResult}
71+
* @param result the result of the evaluation.
72+
* @return a {@link EvaluateResult}
73+
*/
74+
@Named("parsed")
75+
EvaluateResult createEvaluateResult(ParseResult parsed, IStrategoTerm result);
76+
77+
/**
78+
* Create an {@link EvaluateResult} that can be passed to the Repl.
79+
* @param analyzed the wrapped {@link AnalyzeResult}
80+
* @param result the result of the evaluation.
6981
* @return a {@link EvaluateResult}
7082
*/
71-
EvaluateResult createEvaluateResult(EvaluateUnit<?> unit);
83+
@Named("analyzed")
84+
EvaluateResult createEvaluateResult(AnalyzeResult analyzed, IStrategoTerm result);
7285

7386
}

0 commit comments

Comments
 (0)