Skip to content

Commit

Permalink
parser: Use the java 21 where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaredecimal committed Jul 6, 2024
1 parent 988853f commit fe26e09
Showing 1 changed file with 42 additions and 55 deletions.
97 changes: 42 additions & 55 deletions src/com/erlava/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ public List<AST> parse() {
while (!match(TokenType.EOF)) {
AST expr = declaration();

if (expr instanceof ImportAst) {
ImportAst _import = (ImportAst) expr;
if (expr instanceof ImportAst _import) {
_import
.getNodes()
.stream()
Expand Down Expand Up @@ -305,8 +304,7 @@ private AST _processImport() {
List<AST> found = new LinkedList<>();
String lookup = "";
for (AST node : nodes) { // Keep the for loop because I have to remove the method from the list
if (node instanceof MethodAST) { // after I locate and convert it.
MethodAST method = (MethodAST) node;
if (node instanceof MethodAST method) { // after I locate and convert it.
boolean found_it = false;
if (array.contains(method.getName())) { // Find it here
found.add(node);
Expand Down Expand Up @@ -594,36 +592,39 @@ private AST multiplicative() {
AST result = unary();

while (true) {
// 2 * 6 / 3
if (match(TokenType.STAR)) {
result = new BinaryAST(result, unary(), '*', line(), currentLine());
continue;
}
if (match(TokenType.SLASH)) {
result = new BinaryAST(result, unary(), '/', line(), currentLine());
continue;
}

if (match(TokenType.CC)) {
Token expr = consume(TokenType.VAR, "Expected a id after `::`");
result = new InstanceFieldAccess(result, expr.getText());
continue;
}

if (match(TokenType.BANG)) {
result = new ProcessCallAST(result, unary(), line(), currentLine());
}

if (match(TokenType.BAR)) {
result = new ConsAST(result, unary(), line(), currentLine());
}

if (match(TokenType.GTGT)) {
result = new PointShiftAST(result, expression());
continue;
}
Object _result = switch(get(0).getType()) {
case STAR -> {
match(TokenType.STAR);
yield new BinaryAST(result, unary(), '*', line(), currentLine());
}
case SLASH -> {
match(TokenType.SLASH);
yield new BinaryAST(result, unary(), '/', line(), currentLine());
}
case CC -> {
match(TokenType.CC);
Token expr = consume(TokenType.VAR, "Expected a id after `::`");
yield new InstanceFieldAccess(result, expr.getText());
}
case BANG -> {
match(TokenType.BANG);
yield new ProcessCallAST(result, unary(), line(), currentLine());
}
case BAR -> {
match(TokenType.BAR);
yield new ConsAST(result, unary(), line(), currentLine());
}
case GTGT -> {
match(TokenType.GTGT);
yield new PointShiftAST(result, expression());
}
default -> get(0);
};

break;
if (_result instanceof AST res) {
result = res;
} else
break;
}

return result;
Expand Down Expand Up @@ -1131,35 +1132,21 @@ private AST buildCall(String module, String method, ArrayList<AST> args) {
return new CallAST(new RemoteAST(new ConstantAST(new BarleyAtom(module)), new ConstantAST(new BarleyAtom(method)), line(), currentLine()), args, line(), currentLine());
}

public static AST buildCallV(String module, String method, ArrayList<AST> args) {
return new CallAST(
new RemoteAST(new ConstantAST(new BarleyAtom(module)),
new ConstantAST(new BarleyAtom(method)),
0,
""),
args,
0,
"");
}

private void emitVariable(String name) {
emulator.set(name, new VariableInfo(new BarleyNull(), 0));
}

private Pattern pattern(AST ast) {
if (ast instanceof ExtractBindAST) {
return new VariablePattern(ast.toString());
} else if (ast instanceof ConstantAST) {
return new ConstantPattern(ast.execute());
} else if (ast instanceof BindAST) {
return new ConstantPattern(ast.execute());
} else if (ast instanceof ListAST) {
return new ListPattern(((ListAST) ast).getArray());
} else if (ast instanceof ConsAST) {
ConsAST cons = (ConsAST) ast;
return new ConsPattern(cons.getLeft().toString(), cons.getRight().toString());

return switch (ast) {
case ExtractBindAST extract -> new VariablePattern(extract.toString());
case ConstantAST constant -> new ConstantPattern(constant.execute());
case BindAST bind -> new ConstantPattern(bind.execute());
case ListAST list -> new ListPattern(list.getArray());
case ConsAST cons -> new ConsPattern(cons.getLeft().toString(), cons.getRight().toString());
default -> null;
};
return null;
}

private LinkedList<Pattern> pattern(ListPattern pattern) {
Expand Down

0 comments on commit fe26e09

Please sign in to comment.