Skip to content

Commit 85c011d

Browse files
committed
Add files and enhance config
1 parent f5fed41 commit 85c011d

4 files changed

Lines changed: 38 additions & 101 deletions

File tree

.idea/workspace.xml

Lines changed: 20 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

diagrams/PeekableStream.png

267 KB
Loading

diagrams/exception.png

295 KB
Loading

src/main/java/ko/carbonel/compiler/config/Config.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static void main(String[] args) {
7070
return;
7171
}
7272
IntStream.range(0, args.length).forEach(i -> parseArg(args[i], i));
73-
switch ((Operation) arguments.get(ArgumentName.OP).getValue()) {
73+
switch (Config.<Operation>get(ArgumentName.OP).getValue()) {
7474
case AST -> runAST();
7575
case PARSE -> runParser();
7676
case LEX -> runLexer();
@@ -85,7 +85,7 @@ public static void main(String[] args) {
8585
}
8686

8787
private static void runAST() {
88-
switch ((Target) get(ArgumentName.TARGET).getValue()) {
88+
switch (Config.<Target>get(ArgumentName.TARGET).getValue()) {
8989
case ATTR_TINY_RUST -> Runners.TINY_RUST_AST_PARSER.run();
9090
default -> throw new IllegalStateException("Unexpected value: " + get(ArgumentName.TARGET));
9191
}
@@ -96,7 +96,7 @@ private static void printHelpAdvanced() {
9696
}
9797

9898
private static void runParser() {
99-
switch ((Target) get(ArgumentName.TARGET).getValue()) {
99+
switch (Config.<Target>get(ArgumentName.TARGET).getValue()) {
100100
case TINY_RUST -> Runners.TINY_RUST_PARSER.run();
101101
case BNF -> Runners.BNF_PARSER.run();
102102
case ATTR_BNF -> Runners.ATTR_BNF_PARSER.run();
@@ -106,15 +106,15 @@ private static void runParser() {
106106
}
107107

108108
private static void runLexer() {
109-
switch ((Target) get(ArgumentName.TARGET).getValue()) {
109+
switch (Config.<Target>get(ArgumentName.TARGET).getValue()) {
110110
case TINY_RUST, ATTR_TINY_RUST -> Runners.TINY_RUST_LEXER.run();
111111
case BNF, ATTR_BNF -> Runners.BNF_LEXER.run();
112112
default -> throw new IllegalStateException("Unexpected value: " + get(ArgumentName.TARGET));
113113
}
114114
}
115115

116116
private static void runGenerator() {
117-
switch ((Target) get(ArgumentName.TARGET).getValue()) {
117+
switch (Config.<Target>get(ArgumentName.TARGET).getValue()) {
118118
case TINY_RUST -> new TinyRustGenerator().generate();
119119
case ATTR_TINY_RUST -> new TinyRustAttrGenerator().generate();
120120
case BNF -> new BNFParserGenerator().generate();
@@ -133,15 +133,15 @@ private static void printHelp() {
133133
public static void parseArg(String args, int idx) {
134134
SuperSecretClass.doSuperSecretThing(args);
135135
if (idx == 0 && isNotCustomArg(args)) {
136-
arguments.get(ArgumentName.INPUT_RS_FILE).setValue(args);
136+
get(ArgumentName.INPUT_RS_FILE).setValue(args);
137137
return;
138138
}
139139
if (idx == 1 && isNotCustomArg(args)) {
140-
arguments.get(ArgumentName.OUTPUT_ASM_FILE).setValue(args);
140+
get(ArgumentName.OUTPUT_ASM_FILE).setValue(args);
141141
return;
142142
}
143143
try {
144-
arguments.get(ArgumentName.valueOf(
144+
get(ArgumentName.valueOf(
145145
args.substring(1, args.contains("=") ? args.indexOf("=") : args.length()).toUpperCase())
146146
).setValue(args);
147147
} catch (IllegalArgumentException e) {
@@ -153,8 +153,13 @@ private static boolean isNotCustomArg(String args) {
153153
return !args.startsWith("-") && !args.startsWith("+");
154154
}
155155

156-
public static Argument<?> get(ArgumentName name) {
157-
return arguments.get(name);
156+
public static <T> Argument<T> get(ArgumentName name) {
157+
try{
158+
//noinspection unchecked
159+
return (Argument<T>) arguments.get(name);
160+
} catch (ClassCastException e) {
161+
throw new IllegalArgumentException("Argument " + name + " is not of expected type.");
162+
}
158163
}
159164

160165
public static void setBool(ArgumentName name, boolean value) {
@@ -167,18 +172,18 @@ public static void setBool(ArgumentName name, boolean value) {
167172
}
168173

169174
public static boolean getBool(ArgumentName name) {
170-
return (Boolean) arguments.get(name).getValue();
175+
return Config.<Boolean>get(name).getValue();
171176
}
172177

173178
public static Comparator<? super UnannotatedRule> sorter() {
174-
return switch ((SortFun) get(ArgumentName.SORT_FUN).getValue()) {
179+
return switch (Config.<SortFun>get(ArgumentName.SORT_FUN).getValue()) {
175180
case ALPHABETIC_SORT -> new AlphabeticRuleSorter();
176181
case START_AWARE_SORT -> new StartAwareRuleSorter();
177182
};
178183
}
179184

180185
public static Language lang() {
181-
return switch ((Language.Values) get(ArgumentName.LANG).getValue()) {
186+
return switch (Config.<Language.Values>get(ArgumentName.LANG).getValue()) {
182187
case ENGLISH -> new ENLanguage();
183188
case TEACHER -> new TeacherLanguage();
184189
};

0 commit comments

Comments
 (0)