|
@@ -3,6 +3,9 @@ package migl.lisp;
|
|
|
import java.math.BigInteger;
|
|
|
import java.util.Stack;
|
|
|
|
|
|
+import migl.util.ConsList;
|
|
|
+import migl.util.ConsListFactory;
|
|
|
+
|
|
|
public class LispImpl implements Lisp {
|
|
|
|
|
|
@Override
|
|
@@ -13,7 +16,6 @@ public class LispImpl implements Lisp {
|
|
|
* E = bool | entier | decimal | string
|
|
|
* F = '('X*')'
|
|
|
*/
|
|
|
- // TODO Auto-generated method stub
|
|
|
Stack<String> explode = this.explode(expr);
|
|
|
while(explode.size() > 0) {
|
|
|
String val = explode.remove(0);
|
|
@@ -46,14 +48,21 @@ public class LispImpl implements Lisp {
|
|
|
String[] tmp = expr.split("[ |\t]");
|
|
|
Stack<String> result = new Stack<>();
|
|
|
for(String str : tmp) {
|
|
|
- if(str.charAt(0) == '(') {
|
|
|
- result.add("" + str.charAt(0));
|
|
|
- result.add(str.substring(1));
|
|
|
- } else if(str.charAt(str.length() - 1) == ')'){
|
|
|
- result.add(str.substring(0, str.length() - 2));
|
|
|
- result.add("" + str.charAt(str.length() - 1));
|
|
|
- } else {
|
|
|
- result.add(str);
|
|
|
+ if(str.length() > 0) {
|
|
|
+ if(str.charAt(0) == '(') {
|
|
|
+ result.add("" + str.charAt(0));
|
|
|
+ if(str.charAt(str.length() - 1) == ')') {
|
|
|
+ result.add(str.substring(1, str.length() - 1));
|
|
|
+ result.add("" + str.charAt(str.length() - 1));
|
|
|
+ } else {
|
|
|
+ result.add(str.substring(1));
|
|
|
+ }
|
|
|
+ } else if(str.charAt(str.length() - 1) == ')'){
|
|
|
+ result.add(str.substring(0, str.length() - 2));
|
|
|
+ result.add("" + str.charAt(str.length() - 1));
|
|
|
+ } else {
|
|
|
+ result.add(str);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
@@ -86,12 +95,22 @@ public class LispImpl implements Lisp {
|
|
|
return val;
|
|
|
}
|
|
|
|
|
|
- private Object parseList(Stack<String> pile) {
|
|
|
- String val = "";
|
|
|
- while(pile.size() > 0 && val.equals(")")) {
|
|
|
-
|
|
|
+ private Object parseList(Stack<String> pile) throws LispError {
|
|
|
+ ConsList<Object> list = ConsListFactory.nil();
|
|
|
+ String val = pile.remove(0);
|
|
|
+ while(pile.size() > 0 && !val.equals(")")) {
|
|
|
+ if(val.equals("(")) {
|
|
|
+ list = list.append(this.parseList(pile));
|
|
|
+ } else {
|
|
|
+ list = list.append(this.parseElement(val));
|
|
|
+ }
|
|
|
+ val = pile.remove(0);
|
|
|
}
|
|
|
- return null;
|
|
|
+ //Si arret cause pile vide erreur
|
|
|
+ if(!val.equals(")")) {
|
|
|
+ throw new LispError("Invalid Format");
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
}
|