[AST] operation search

This commit is contained in:
jeffcheasey88 2023-10-17 04:01:53 +02:00
parent 035ff99edf
commit e405d8e380
15 changed files with 128 additions and 10 deletions

View file

@ -1,10 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java;
import java.util.function.Function;
public class Operation extends JavaElement{
public abstract class Operation extends JavaElement{
public static interface OperationContainer{
public static interface OperationContainer{
void addOperation(Operation operation);
@ -12,9 +10,4 @@ public static interface OperationContainer{
public Operation(){}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
return null;
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
import be.jeffcheasey88.peeratcode.parser.java.Value;
@ -21,4 +24,9 @@ public class AssignOperation extends Operation{
return right;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
return finder.apply(left) ? (E)left : finder.apply(right) ? (E)right : null;
}
}

View file

@ -1,9 +1,17 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
public class BreakOperation extends Operation{
public BreakOperation(){}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
return null;
}
}

View file

@ -1,7 +1,15 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
public class ContinueOperation extends Operation{
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
return null;
}
}

View file

@ -1,7 +1,9 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.List;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Value;
import be.jeffcheasey88.peeratcode.parser.java.Variable;
@ -40,4 +42,18 @@ public class ForOperation extends OperationBag{
return this.updates;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
for(Variable variable : this.init_vars){
if(finder.apply(variable)) return (E)variable;
}
for(Value value : this.init_values){
if(finder.apply(value)) return (E)value;
}
if(finder.apply(condition)) return (E)condition;
for(Value value : this.updates){
if(finder.apply(value)) return (E)value;
}
return super.find(finder);
}
}

View file

@ -1,6 +1,9 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Value;
public class ForeachOperation extends OperationBag{
@ -29,4 +32,9 @@ public class ForeachOperation extends OperationBag{
return this.iterator;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(iterator)) return (E)iterator;
return super.find(finder);
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Value;
public class IfOperation extends OperationBag{
@ -14,4 +17,9 @@ public class IfOperation extends OperationBag{
return this.condition;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(condition)) return (E)condition;
return super.find(finder);
}
}

View file

@ -1,8 +1,10 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.List;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.Token;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
import be.jeffcheasey88.peeratcode.parser.java.Value;
@ -36,4 +38,14 @@ public class MethodCallOperation extends Operation{
return this.parameters;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
if(finder.apply(start)) return (E)start;
if(finder.apply(previous)) return (E) previous;
for(Value param : this.parameters){
if(finder.apply(param)) return (E)param;
}
return null;
}
}

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
@ -27,4 +28,12 @@ public class OperationBag extends Operation implements VariableContainer, Operat
this.elements.add(operation);
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
for(JavaElement element : this.elements){
if(finder.apply(element)) return (E)element;
}
return null;
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
import be.jeffcheasey88.peeratcode.parser.java.Value;
@ -15,4 +18,9 @@ public class ReturnOperation extends Operation{
return this.value;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
return finder.apply(value) ? (E)value : null;
}
}

View file

@ -1,7 +1,15 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
public class SwitchOperation extends Operation{
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
return null;
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Value;
public class SynchronizedOperation extends OperationBag{
@ -15,4 +18,9 @@ public class SynchronizedOperation extends OperationBag{
return this.value;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(value)) return (E)value;
return super.find(finder);
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Operation;
import be.jeffcheasey88.peeratcode.parser.java.Value;
@ -15,4 +18,9 @@ public class ThrowOperation extends Operation{
return this.value;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
return finder.apply(value) ? (E)value : null;
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Variable;
public class TryOperation extends OperationBag{
@ -18,5 +21,10 @@ public class TryOperation extends OperationBag{
public Variable getResource(){
return this.resource;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(resource)) return (E)resource;
return super.find(finder);
}
}

View file

@ -1,5 +1,8 @@
package be.jeffcheasey88.peeratcode.parser.java.operation;
import java.util.function.Function;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Value;
public class WhileOperation extends OperationBag{
@ -14,4 +17,9 @@ public class WhileOperation extends OperationBag{
return this.condition;
}
@Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(condition)) return (E)condition;
return super.find(finder);
}
}