Преглед изворни кода

Réecriture parser lisp element pour ne plus avoir de code smell

Arthur Brandao пре 6 година
родитељ
комит
32f471a816
2 измењених фајлова са 102 додато и 3 уклоњено
  1. 6 3
      src/migl/lisp/LispImpl.java
  2. 96 0
      src/migl/lisp/util/LispElement.java

+ 6 - 3
src/migl/lisp/LispImpl.java

@@ -5,11 +5,12 @@ import java.util.Stack;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import migl.lisp.util.LispElement;
 import migl.util.ConsList;
 import migl.util.ConsListFactory;
 
 public class LispImpl implements Lisp {
-
+	
 	@Override
 	public Object parse(String expr) throws LispError {
 		//Analyse l'expression
@@ -27,7 +28,8 @@ public class LispImpl implements Lisp {
 			if(explode.size() > 0) {
 				throw new LispError("Invalid Format");
 			}
-			return this.parseElement(val);
+			//return this.parseElement(val);
+			return LispElement.valueOf(val).get().value;
 		}
 	}
 
@@ -154,7 +156,8 @@ public class LispImpl implements Lisp {
 				//list = list.append(this.parse("(" + val));
 				list = list.append(this.parseList(pile));
 			} else {
-				list = list.append(this.parseElement(val));
+				//list = list.append(this.parseElement(val));
+				list = list.append(LispElement.valueOf(val).get().value);
 			}
 			val = pile.remove(0);
 		}

+ 96 - 0
src/migl/lisp/util/LispElement.java

@@ -0,0 +1,96 @@
+package migl.lisp.util;
+
+import java.math.BigInteger;
+
+import migl.lisp.LispBoolean;
+
+public class LispElement {
+
+	/**
+	 * Le conteneur de la valeur de l'element
+	 */
+	private final LispElementValue value;
+
+	/* --- Constructeurs privés des differents elements --- */
+
+	/**
+	 * Creation d'un element de type BigInt
+	 * @param val
+	 */
+	private LispElement(BigInteger val) {
+		this.value = new LispElementValue<>(val);
+	}
+
+	/**
+	 * Creation d'un element de type double
+	 * @param val
+	 */
+	private LispElement(double val) {
+		this.value = new LispElementValue<>(val);
+	}
+
+	/**
+	 * Creation d'un element de type LispBoolean
+	 * @param val
+	 */
+	private LispElement(LispBoolean val) {
+		this.value = new LispElementValue<>(val);
+	}
+
+	/**
+	 * Creation d'un element de type String
+	 * @param val
+	 */
+	private LispElement(String val) {
+		this.value = new LispElementValue<>(val);
+	}
+
+	/**
+	 * Retourne le conteneur de l'element
+	 * @return
+	 */
+	public LispElementValue get() {
+		return this.value;
+	}
+	
+	/**
+	 * Parse un element
+	 * @param elt
+	 * @return
+	 */
+	public static LispElement valueOf(String elt) {
+		try {
+			return new LispElement(new BigInteger(elt));
+		} catch(NumberFormatException ex) {
+			//Rien
+		}
+		try {
+			return new LispElement(Double.valueOf(elt));
+		} catch(NumberFormatException ex) {
+			//Rien
+		}
+		try {
+			return new LispElement(LispBoolean.valueOf(elt));
+		} catch(IllegalArgumentException ex) {
+			//Rien
+		}
+		return new LispElement(elt);
+	}
+
+	/**
+	 * Conteneur de la valeur d'un element
+	 * @author Arthur Brandao
+	 *
+	 * @param <E> Le type de l'element
+	 */
+	public class LispElementValue<E> {
+
+		public final E value;
+
+		public LispElementValue(E val){
+			this.value = val;
+		}
+
+	}
+
+}