Quellcode durchsuchen

Prise en charde des objets de type LispLisp et Cons

Arthur Brandao vor 6 Jahren
Ursprung
Commit
5ffe56de4b
1 geänderte Dateien mit 58 neuen und 7 gelöschten Zeilen
  1. 58 7
      src/migl/lisp/LispElement.java

+ 58 - 7
src/migl/lisp/LispElement.java

@@ -3,6 +3,8 @@ package migl.lisp;
 import java.math.BigInteger;
 import java.util.Map;
 
+import migl.util.Cons;
+
 import java.util.HashMap;
 
 /**
@@ -39,7 +41,7 @@ public class LispElement {
 	 * @return
 	 */
 	public BigInteger toInt() {
-		if(this.value.getClass() == BigInteger.class) {
+		if(this.isInt()) {
 			return (BigInteger) this.value;
 		}
 		throw new IllegalStateException("Not an integer");
@@ -48,7 +50,7 @@ public class LispElement {
 	/**
 	 * Retourne la valeur d'un element sous forme de nombre
 	 * 
-	 * Si l'element ne peut pas être converti en Double
+	 * @throws IllegalStateException Si l'element ne peut pas être converti en Double
 	 * @return
 	 */
 	public double toNumber() {
@@ -64,11 +66,11 @@ public class LispElement {
 	/**
 	 * Retourne la valeur d'un element sous forme de boolean
 	 * 
-	 * Si l'element ne peut pas être converti en boolean
+	 * @throws IllegalStateException Si l'element ne peut pas être converti en boolean
 	 * @return
 	 */
 	public boolean toBoolean() {
-		if(this.value.getClass() == LispBoolean.class) {
+		if(this.isBoolean()) {
 			LispBoolean lb = (LispBoolean) this.value;
 			return lb.value();
 		}
@@ -78,16 +80,42 @@ public class LispElement {
 	/**
 	 * Retourne la valeur d'un element sous forme de string
 	 * 
-	 * Si l'element ne peut pas être converti en String
+	 * @throws IllegalStateException Si l'element ne peut pas être converti en String
 	 * @return
 	 */
 	public String toStr() {
-		if(this.value.getClass() == String.class) {
+		if(this.isStr()) {
 			return (String) this.value;
 		}
 		throw new IllegalStateException("Not a String");
 	}
 	
+	/**
+	 * Retourne la valeur d'un element sous forme d'une liste
+	 * 
+	 * @throws IllegalStateException
+	 * @return
+	 */
+	public LispList toList() {
+		if(this.isList()) {
+			return (LispList) this.value;
+		}
+		throw new IllegalStateException("Not a List");
+	}
+	
+	/**
+	 * Retourne la valeur d'un element sous forme d'une cons
+	 * 
+	 * @throws IllegalStateException
+	 * @return
+	 */
+	public Cons<?, ?> toCons() {
+		if(this.isCons()) {
+			return (Cons<?, ?>) this.value;
+		}
+		throw new IllegalStateException("Not a Cons");
+	}
+	
 	/**
 	 * Indique si l'element est un entier
 	 * @return
@@ -119,6 +147,22 @@ public class LispElement {
 	public boolean isStr() {
 		return this.value.getClass() == String.class;
 	}
+	
+	/**
+	 * Indique si l'element est une liste
+	 * @return
+	 */
+	public boolean isList() {
+		return this.value.getClass() == LispList.class;
+	}
+	
+	/**
+	 * Indique si l'element est une Cons
+	 * @return
+	 */
+	public boolean isCons() {
+		return this.value.getClass() == Cons.class;
+	}
 
 	@Override
 	public int hashCode() {
@@ -194,7 +238,14 @@ public class LispElement {
 		else if(elt.getClass() == Long.class) {
 			elt = BigInteger.valueOf((long) elt);
 		}
-		else if(elt.getClass() != BigInteger.class && elt.getClass() != Double.class && elt.getClass() != LispBoolean.class && elt.getClass() != String.class) {
+		else if(
+				elt.getClass() != BigInteger.class 
+				&& elt.getClass() != Double.class 
+				&& elt.getClass() != LispBoolean.class 
+				&& elt.getClass() != String.class
+				&& elt.getClass() != LispList.class
+				&& elt.getClass() != Cons.class
+				) {
 			throw new IllegalArgumentException("Object class is not a Lisp element");
 		}
 		if(cache.containsKey(elt)) {