//JDK 1.5 !!!
import java.util.Scanner;// Classe Scanner (Lecture)
import java.io.File;
import java.util.regex.MatchResult;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
public class Log2Java {
public static void main(String[] args) {
//Scanner clavier=new Scanner(System.in)
boolean trouvé=true;
MatchResult res;
if (args.length<1){
System.out.println("Utilisation : Log2Java fichier [-noSource]");
System.out.println("-noSource est optionel et permet de désactiver la sortie de la source...");
return;
}
File fich;
fich=new File(args[0]);
//Scanner fichier=new Scanner(System.in);
Scanner fichier;
try{
fichier=new Scanner(fich);
}catch(java.io.FileNotFoundException e){
System.out.println("fichier non trouvé");
return;
}
fichier.useDelimiter("\n");
System.out.println("import java.util.Scanner;\nScanner clavier=new Scanner(System.in);");
while(fichier.hasNextLine()){
//System.out.println(fichier.nextLine());
trouvé=true;
if (fichier.hasNext("\\W*SI (.*)ALORS(?://.*)?")){
//System.out.println("ok");
res=fichier.match();
if (res.groupCount()<1){
System.out.println("***ERREUR : Manque la condition !***");
}else {
String b;
b=res.group(1);
//b=b.replaceAll("\\bet\\b","&&");
b=conversion(b);
System.out.println("if("+b+"){");
}
}else if (fichier.hasNext("\\W*POUR (\\w*) DE (\\w*) A (\\w*)(?: PAR (.*))? FAIRE")){
res=fichier.match();
if (res.groupCount()<3){
System.out.println("***ERREUR : Manque une partie !!!***");
}else {
String variable,initialisation,fin;
variable=res.group(1);
initialisation=res.group(2);
fin=res.group(3);
if (res.group(4)!=null){
String pas=res.group(4);
int a=(Integer.decode(initialisation)-Integer.decode(fin));
System.out.println("for("+variable+"="+initialisation+";"+variable+((a<=0)?"<=":">=")+fin+";"+variable+"+="+pas+"){");
}else{
System.out.println("for("+variable+"="+initialisation+";"+variable+"<="+fin+";"+variable+"++){");
}
}
}else if (fichier.hasNext("\\W*TANTQUE (.*) FAIRE")){
res=fichier.match();
if (res.groupCount()>0){
System.out.println("while("+conversion(res.group(1))+"){");
}
}else if(fichier.hasNext("\\W*AFFICHER (.*)")){
res=fichier.match();
if (res.groupCount()>0){
System.out.println("System.out.println("+conversion(res.group(1))+");");
}
}else if(fichier.hasNext("\\W*FIN.*")){
System.out.println("}");
}else if(fichier.hasNext("\\W*SINON")){
System.out.println("}else{");
}else if(fichier.hasNext("\\W*LIRE (\\w*)")){
res=fichier.match();
System.out.println(res.group(1)+"=clavier.nextLine();");
}else if(fichier.hasNext("\\W*(\\w*)\\W*:entier")){
res=fichier.match();
if (res.groupCount()==1){
System.out.println("int "+res.group(1)+";");
}
}else if(fichier.hasNext("\\W*(\\w*)\\W*:chaîne")){
res=fichier.match();
if (res.groupCount()==1)System.out.println("String "+res.group(1)+";" );
}else if(fichier.hasNext("\\W*(\\w*)\\W*:bouléen")){
res=fichier.match();
if (res.groupCount()==1)System.out.println("boolean "+res.group(1)+";");
}{
trouvé=false;
}
if (args.length==1){
if (trouvé){
System.out.println("CS>>"+fichier.nextLine());
}else{
String tmp=fichier.nextLine();
System.out.println(conversion(tmp)+";");
System.out.println("CS>>"+tmp);
}
}else{
fichier.nextLine();
}
}
}
public static String conversion(String quoi){
quoi=quoi.replaceAll("\\bET\\b","&&");
quoi=quoi.replaceAll("\\bOU\\b","||");
quoi=quoi.replaceAll("\\bNON\\b","!");
quoi=quoi.replaceAll("=","==");
quoi=quoi.replaceAll("<-","=");
Scanner chaine=new Scanner(quoi);
MatchResult res;
if (chaine.hasNext(".*RACINE\\((.*)\\)")){
//System.out.println("ok" );
res=chaine.match();
//System.out.println("******"+res.group(1) );
quoi=quoi.replaceAll("RACINE\\(.*\\)","Math.sqrt("+res.group(1)+")");
}
return quoi;
}
}