Operation -> Assigment

This commit is contained in:
jeffcheasey88 2023-05-30 11:19:55 +02:00
parent 77f075abdc
commit 56ce2b7235
6 changed files with 56 additions and 13 deletions

View file

@ -23,6 +23,8 @@ public class Function extends JavaElement{
private List<Variable> parameters; private List<Variable> parameters;
private String exceptions; private String exceptions;
private boolean constructor;
private List<JavaElement> childs; private List<JavaElement> childs;
public Function(){ public Function(){
@ -73,6 +75,7 @@ public class Function extends JavaElement{
if(this.returnType == null){ if(this.returnType == null){
this.returnType = value; this.returnType = value;
if(values.hasNext()) value = values.next(); if(values.hasNext()) value = values.next();
else constructor = true;
} }
if(this.name == null){ if(this.name == null){
this.name = value; this.name = value;
@ -95,6 +98,7 @@ public class Function extends JavaElement{
private void body(String content, CleanerPool cleaner) throws Exception{ private void body(String content, CleanerPool cleaner) throws Exception{
content = cleaner.unzip(content, (s,p) -> s);
while(!(content.replaceAll("\\s+", "").isEmpty())){ while(!(content.replaceAll("\\s+", "").isEmpty())){
System.out.println("BODY "+content); System.out.println("BODY "+content);
@ -134,7 +138,7 @@ public class Function extends JavaElement{
String param = ""; String param = "";
for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+""; for(Variable v : this.parameters) param+=","+v.getType()+" "+v.getName()+"";
if(!param.isEmpty()) param = param.substring(1); if(!param.isEmpty()) param = param.substring(1);
System.out.println(start+Modifier.toString(modifier)+" "+returnType+" "+name+"("+param+") "+exceptions+"{"); System.out.println(start+Modifier.toString(modifier)+" "+(constructor ? "" : returnType+" ")+name+"("+param+") "+exceptions+"{");
for(JavaElement child : this.childs) child.show(tab+1); for(JavaElement child : this.childs) child.show(tab+1);
System.out.println(start+"}"); System.out.println(start+"}");
} }

View file

@ -14,7 +14,7 @@ import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
public class JavaParser{ public class JavaParser{
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java"); File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Variable.java");
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
JavaParser parser = new JavaParser(reader); JavaParser parser = new JavaParser(reader);

View file

@ -16,7 +16,7 @@ public class Variable extends JavaElement{
private int modifier; private int modifier;
private String name; private String name;
private String type; private String type;
private Variable value; //Change into operation private Variable value; //Change into operation or JavaElement
public Variable(){} public Variable(){}
@ -126,7 +126,7 @@ public class Variable extends JavaElement{
public void show(int tab){ public void show(int tab){
String start = ""; String start = "";
for(int i = 0; i < tab; i++) start+="\t"; for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":" = "+value+";")); System.out.println(start+Modifier.toString(modifier)+(modifier > 0 ? " ":"")+type+" "+name+(value == null ? ";":" = "+value+";"));
} }
public static class Value extends Variable{ public static class Value extends Variable{

View file

@ -0,0 +1,37 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool;
import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class AssigmentOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\s]+)\\s*=\\s*([^;]+);).*$");
private String variable;
private String value;
public AssigmentOperation(){}
@Override
public int parse(String content, CleanerPool cleaner) throws Exception{
Matcher matcher = PATTERN.matcher(content);
matcher.matches();
this.variable = matcher.group(2);
this.value = matcher.group(3);
return matcher.group(1).length();
}
@Override
public void show(int tab){
String start = "";
for(int i = 0; i < tab; i++) start+="\t";
System.out.println(start+variable+" = "+value+";");
}
}

View file

@ -10,6 +10,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
import be.jeffcheasey88.peeratcode.parser.java.Variable; import be.jeffcheasey88.peeratcode.parser.java.Variable;
import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner; import be.jeffcheasey88.peeratcode.parser.java.CleanerPool.Cleaner;
//Singleton
public class OperationFactory{ public class OperationFactory{
private Map<Pattern, Class<? extends JavaElement>> patterns; private Map<Pattern, Class<? extends JavaElement>> patterns;
@ -17,23 +18,23 @@ public class OperationFactory{
public OperationFactory(){ public OperationFactory(){
this.patterns = new LinkedHashMap<>(); this.patterns = new LinkedHashMap<>();
this.patterns.put(Pattern.compile("^\\s*return\\s+[^;]*;.*$"), ReturnOperation.class); this.patterns.put(Pattern.compile("^\\s*return\\s*[^;]*;.*$"), ReturnOperation.class);
this.patterns.put(Pattern.compile("^\\s*([^;=]*;).*$"), MethodCallOperation.class); this.patterns.put(Pattern.compile("^\\s*([^;=]+;).*$"), MethodCallOperation.class);
//if not types ?? this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;.*$"), Variable.class);
this.patterns.put(Pattern.compile("^\\s*[^\\s]*\\s+[^\\s]*\\s+=\\s+[^\\s]*;.*$"), Variable.class); this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s*=\\s*[^;]+;.*$"), AssigmentOperation.class);
} }
/* /*
* variable * variable OK
* assignation * assignation OK
* exec method * exec method OK
* for * for
* do while * do while
* while * while
* if * if
* switch case * switch case
* return * return OK (without just return)
* continue * continue
* break * break
* try catch finally * try catch finally
@ -46,6 +47,7 @@ public class OperationFactory{
* *
* assert ?? * assert ??
* Goto ?? * Goto ??
* Const ??
*/ */
public JavaElement buildOperation(String content) throws Exception{ public JavaElement buildOperation(String content) throws Exception{

View file

@ -8,7 +8,7 @@ import be.jeffcheasey88.peeratcode.parser.java.JavaElement;
public class ReturnOperation extends JavaElement{ public class ReturnOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s+([^;]*);)\\s*$"); private static Pattern PATTERN = Pattern.compile("^(\\s*return\\s*([^;]*);)\\s*$");
private String value; private String value;