forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKastFrontEnd.java
More file actions
119 lines (110 loc) · 4.19 KB
/
KastFrontEnd.java
File metadata and controls
119 lines (110 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2012-2016 K Team. All Rights Reserved.
package org.kframework.kast;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import org.kframework.attributes.Source;
import org.kframework.compile.ExpandMacros;
import org.kframework.kompile.CompiledDefinition;
import org.kframework.kore.K;
import org.kframework.unparser.ToKast;
import org.kframework.main.FrontEnd;
import org.kframework.utils.Stopwatch;
import org.kframework.utils.errorsystem.KEMException;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.Environment;
import org.kframework.utils.file.FileUtil;
import org.kframework.utils.file.JarInfo;
import org.kframework.utils.file.KompiledDir;
import org.kframework.utils.inject.CommonModule;
import org.kframework.utils.inject.DefinitionScope;
import org.kframework.utils.inject.JCommanderModule;
import org.kframework.utils.inject.JCommanderModule.ExperimentalUsage;
import org.kframework.utils.inject.JCommanderModule.Usage;
import scala.Option;
import java.io.File;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.kframework.kore.KORE.*;
public class KastFrontEnd extends FrontEnd {
public static List<Module> getModules() {
List<Module> modules = new ArrayList<>();
modules.add(new KastModule());
modules.add(new JCommanderModule());
modules.add(new CommonModule());
return modules;
}
private final KastOptions options;
private final Stopwatch sw;
private final KExceptionManager kem;
private final FileUtil files;
private final Map<String, String> env;
private final Provider<File> kompiledDir;
private final Provider<CompiledDefinition> compiledDef;
private final DefinitionScope scope;
@Inject
KastFrontEnd(
KastOptions options,
@Usage String usage,
@ExperimentalUsage String experimentalUsage,
Stopwatch sw,
KExceptionManager kem,
JarInfo jarInfo,
@Environment Map<String, String> env,
FileUtil files,
@KompiledDir Provider<File> kompiledDir,
Provider<CompiledDefinition> compiledDef,
DefinitionScope scope) {
super(kem, options.global, usage, experimentalUsage, jarInfo, files);
this.options = options;
this.sw = sw;
this.kem = kem;
this.files = files;
this.env = env;
this.kompiledDir = kompiledDir;
this.compiledDef = compiledDef;
this.scope = scope;
}
/**
*
* @return true if the application terminated normally; false otherwise
*/
@Override
public int run() {
scope.enter(kompiledDir.get());
try {
Reader stringToParse = options.stringToParse();
Source source = options.source();
CompiledDefinition def = compiledDef.get();
org.kframework.kore.Sort sort = options.sort;
if (sort == null) {
if (env.get("KRUN_SORT") != null) {
sort = Sort(env.get("KRUN_SORT"));
} else {
sort = def.programStartSymbol;
}
}
org.kframework.definition.Module mod;
if (options.module == null) {
mod = def.programParsingModuleFor(def.mainSyntaxModuleName(), kem).get();
} else {
Option<org.kframework.definition.Module> mod2 = def.programParsingModuleFor(options.module, kem);
if (mod2.isEmpty()) {
throw KEMException.innerParserError("Module " + options.module + " not found. Specify a module with -m.");
}
mod = mod2.get();
}
K parsed = def.getParser(mod, sort, kem).apply(FileUtil.read(stringToParse), source);
if (options.expandMacros) {
parsed = new ExpandMacros(mod, kem, files, options.global, def.kompileOptions).expand(parsed);
}
System.out.println(ToKast.apply(parsed));
sw.printTotal("Total");
return 0;
} finally {
scope.exit();
}
}
}