|
@@ -1,5 +1,8 @@
|
|
package migl.lisp;
|
|
package migl.lisp;
|
|
|
|
|
|
|
|
+import java.math.BigInteger;
|
|
|
|
+import java.util.Stack;
|
|
|
|
+
|
|
public class LispImpl implements Lisp {
|
|
public class LispImpl implements Lisp {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -11,10 +14,18 @@ public class LispImpl implements Lisp {
|
|
* F = '('X*')'
|
|
* F = '('X*')'
|
|
*/
|
|
*/
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
- String[] explode = this.explode(expr);
|
|
|
|
- for(String str : explode) {
|
|
|
|
- if(str.equals("(")){
|
|
|
|
-
|
|
|
|
|
|
+ Stack<String> explode = this.explode(expr);
|
|
|
|
+ while(explode.size() > 0) {
|
|
|
|
+ String val = explode.remove(0);
|
|
|
|
+ //Analyse le type d'expression
|
|
|
|
+ if(val.equals("(")){
|
|
|
|
+ return this.parseList(explode);
|
|
|
|
+ } else {
|
|
|
|
+ //Element seul
|
|
|
|
+ if(explode.size() > 1) {
|
|
|
|
+ throw new LispError("Invalid format");
|
|
|
|
+ }
|
|
|
|
+ return this.parseElement(val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@@ -27,35 +38,71 @@ public class LispImpl implements Lisp {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- *
|
|
|
|
- * @param expr
|
|
|
|
- * @return
|
|
|
|
|
|
+ * Découpe un string pour l'analyse Lisp
|
|
|
|
+ * @param expr L'expression à analyser
|
|
|
|
+ * @return L'expression découper
|
|
*/
|
|
*/
|
|
- private String[] explode(String expr) {
|
|
|
|
- //Separe au niveau des espaces et des tabulations
|
|
|
|
|
|
+ private Stack<String> explode(String expr) {
|
|
String[] tmp = expr.split("[ |\t]");
|
|
String[] tmp = expr.split("[ |\t]");
|
|
- //Compte le nombre de parenthese
|
|
|
|
- int compteur = 0;
|
|
|
|
- for(String str : tmp) {
|
|
|
|
- if(str.charAt(0) == '(' || str.charAt(str.length() - 1) == ')') {
|
|
|
|
- compteur++;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- //Creation du tableau de resultat
|
|
|
|
- String[] result = new String[tmp.length + compteur];
|
|
|
|
- int index = 0;
|
|
|
|
|
|
+ Stack<String> result = new Stack<>();
|
|
for(String str : tmp) {
|
|
for(String str : tmp) {
|
|
if(str.charAt(0) == '(') {
|
|
if(str.charAt(0) == '(') {
|
|
- result[index++] = "" + str.charAt(0);
|
|
|
|
- result[index++] = str.substring(1);
|
|
|
|
|
|
+ result.add("" + str.charAt(0));
|
|
|
|
+ result.add(str.substring(1));
|
|
} else if(str.charAt(str.length() - 1) == ')'){
|
|
} else if(str.charAt(str.length() - 1) == ')'){
|
|
- result[index++] = str.substring(0, str.length() - 2);
|
|
|
|
- result[index++] = "" + str.charAt(str.length() - 1);
|
|
|
|
|
|
+ result.add(str.substring(0, str.length() - 2));
|
|
|
|
+ result.add("" + str.charAt(str.length() - 1));
|
|
} else {
|
|
} else {
|
|
- result[index++] = str;
|
|
|
|
|
|
+ result.add(str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private Object parseElement(String val) {
|
|
|
|
+ switch(val.charAt(0)) {
|
|
|
|
+ case '-':
|
|
|
|
+ case '0':
|
|
|
|
+ case '1':
|
|
|
|
+ case '2':
|
|
|
|
+ case '3':
|
|
|
|
+ case '4':
|
|
|
|
+ case '5':
|
|
|
|
+ case '6':
|
|
|
|
+ case '7':
|
|
|
|
+ case '8':
|
|
|
|
+ case '9':
|
|
|
|
+ try {
|
|
|
|
+ BigInteger i = BigInteger.valueOf((Integer.parseInt(val)));
|
|
|
|
+ return i;
|
|
|
|
+ } catch(NumberFormatException ex) {
|
|
|
|
+ //Rien
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Double d = Double.parseDouble(val);
|
|
|
|
+ return d;
|
|
|
|
+ } catch(NumberFormatException ex) {
|
|
|
|
+ //Rien
|
|
|
|
+ }
|
|
|
|
+ return val;
|
|
|
|
+ case '#':
|
|
|
|
+ try {
|
|
|
|
+ LispBoolean lbool = LispBoolean.valueOf(val);
|
|
|
|
+ return lbool;
|
|
|
|
+ } catch(IllegalArgumentException ex) {
|
|
|
|
+ //Rien
|
|
|
|
+ }
|
|
|
|
+ return val;
|
|
|
|
+ default:
|
|
|
|
+ return val;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Object parseList(Stack<String> pile) {
|
|
|
|
+ for(String s : pile) {
|
|
|
|
+ System.out.println(s);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|