feat: uploaded versions for jack
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2021 Exercism
|
||||
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class Token {
|
||||
|
||||
static class OpDefToken extends Token {
|
||||
|
||||
static OpDefToken opDefTokenFromString(final String string) {
|
||||
final String trimmedLine = string.substring(2, string.length() - 2);
|
||||
final int newOpEnd = trimmedLine.indexOf(" ");
|
||||
|
||||
if (newOpEnd == -1) {
|
||||
throw new IllegalArgumentException("Incomplete operation definition");
|
||||
}
|
||||
|
||||
final Token newOpToken = Token.fromString(trimmedLine.substring(0, newOpEnd)).get(0);
|
||||
|
||||
if (!(newOpToken instanceof OpToken)) {
|
||||
throw new IllegalArgumentException("Cannot redefine numbers");
|
||||
}
|
||||
|
||||
final List<Token> newOpDefTokens = Token.fromString(trimmedLine.substring(newOpEnd + 1));
|
||||
return new OpDefToken(((OpToken) newOpToken).getOp(), newOpDefTokens);
|
||||
}
|
||||
|
||||
private final String newOp;
|
||||
|
||||
private final List<Token> newOpDefTokens;
|
||||
|
||||
private OpDefToken(final String newOp, final List<Token> newOpDefTokens) {
|
||||
this.newOp = newOp;
|
||||
this.newOpDefTokens = newOpDefTokens;
|
||||
}
|
||||
|
||||
String getNewOp() {
|
||||
return newOp;
|
||||
}
|
||||
|
||||
List<Token> getNewOpDefTokens() {
|
||||
return newOpDefTokens;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class OpToken extends Token {
|
||||
|
||||
private final String op;
|
||||
|
||||
OpToken(final String op) {
|
||||
this.op = op;
|
||||
}
|
||||
|
||||
String getOp() {
|
||||
return op;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class IntToken extends Token {
|
||||
|
||||
private final int rawValue;
|
||||
|
||||
IntToken(final int rawValue) {
|
||||
this.rawValue = rawValue;
|
||||
}
|
||||
|
||||
int getRawValue() {
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static List<Token> fromString(final String string) {
|
||||
if (string.startsWith(":")) {
|
||||
return Collections.singletonList(OpDefToken.opDefTokenFromString(string));
|
||||
} else if (string.matches("[A-z+/*\\-]+(?:-[A-z+/*\\-]+)*")) {
|
||||
return Collections.singletonList(new OpToken(string.toLowerCase()));
|
||||
} else if (string.matches("-?\\d+")) {
|
||||
return Collections.singletonList(new IntToken(Integer.parseInt(string)));
|
||||
} else {
|
||||
return Arrays.stream(string.split(" "))
|
||||
.map(Token::fromString)
|
||||
.flatMap(List::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user