improved parser + added rewriter to Main (currently not work correctly)
This commit is contained in:
parent
203845dcca
commit
651e3aed40
@ -1,6 +1,8 @@
|
||||
package lsystem;
|
||||
|
||||
import lsystem.engine.Parser;
|
||||
import lsystem.engine.Rewrite;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
@ -8,16 +10,17 @@ import java.util.Scanner;
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
String axiom = null;
|
||||
Parser parser = null;
|
||||
final List<String> rules = new ArrayList<>();
|
||||
int nbIterations = 0;
|
||||
while(parser == null || parser.isCorrect()) {
|
||||
while(parser == null || !parser.isCorrect()) {
|
||||
if(parser != null)
|
||||
System.out.println("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer");
|
||||
System.out.println("Axiome: ");
|
||||
String axiom = scanner.next();
|
||||
axiom = scanner.next();
|
||||
System.out.println("Règles: (\"finish\" quand vous avez fini): ");
|
||||
List<String> rules = new ArrayList<>();
|
||||
while(rules.isEmpty() || !rules.get(rules.size() - 1).equals("finish")) {
|
||||
rules.add(scanner.next());
|
||||
}
|
||||
@ -26,6 +29,10 @@ public class Main {
|
||||
nbIterations = scanner.nextInt();
|
||||
parser = new Parser(axiom, rules,nbIterations);
|
||||
}
|
||||
System.out.println("Réécriture, veuillez patientez...");
|
||||
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), nbIterations);
|
||||
final String word = rewriter.rewrite();
|
||||
System.out.println(word);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
package lsystem.engine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lsystem.Type;
|
||||
import lsystem.utils.Pair;
|
||||
|
||||
public class Parser {
|
||||
|
||||
@ -17,6 +19,11 @@ public class Parser {
|
||||
this.nbIterations = nbIterations;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if axiom and rules given by user respect the syntax
|
||||
* @return true if the syntax is correct
|
||||
*/
|
||||
public boolean isCorrect(){
|
||||
if (nbIterations < 1) {
|
||||
System.out.println("Erreur, nombre d'itérations insuffisant (plus petit que 1)");
|
||||
@ -29,15 +36,24 @@ public class Parser {
|
||||
return bl;
|
||||
}
|
||||
|
||||
|
||||
private boolean isCorrect(String stringToCheck, Type type) {
|
||||
char old = ' ';
|
||||
int bracket = 0;
|
||||
boolean equalsSymbolFound = false;
|
||||
for (int i = 0; i > stringToCheck.length(); i++){
|
||||
char temp = stringToCheck.charAt(i);
|
||||
if (temp == '[')
|
||||
bracket++;
|
||||
if(temp ==']')
|
||||
if(temp == ']')
|
||||
bracket--;
|
||||
if(temp == '=') {
|
||||
if(!equalsSymbolFound)
|
||||
equalsSymbolFound = true;
|
||||
else
|
||||
// only one '=' allowed
|
||||
return false;
|
||||
}
|
||||
if(old == '.'){
|
||||
for(int y = (type == Type.RULE ? 0 : 1); y < 12; y++){
|
||||
if(temp == validChars[y])
|
||||
@ -55,4 +71,17 @@ public class Parser {
|
||||
return bracket == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by {@link Rewrite}
|
||||
* @return a list of rules with the left and right sides separated by a {@link lsystem.utils.Pair Pair}
|
||||
*/
|
||||
public List<Pair<String, String>> parseRules() {
|
||||
List<Pair<String, String>> rules = new ArrayList<>();
|
||||
this.rules.forEach(rule -> {
|
||||
String[] str = rule.split("=");
|
||||
rules.add(new Pair<String, String>(str[0], str[1]));
|
||||
});
|
||||
return rules;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,11 @@
|
||||
package lsystem.utils;
|
||||
|
||||
/**
|
||||
* tuple containing 2 unknown type elements
|
||||
*
|
||||
* @param <U> left
|
||||
* @param <K> right
|
||||
*/
|
||||
public class Pair<U, K> {
|
||||
|
||||
private final U left;
|
||||
@ -37,4 +43,9 @@ public class Pair<U, K> {
|
||||
public int hashCode() {
|
||||
return 31 + left.hashCode() * right.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + left + ", " + right + ")";
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user