Sfoglia il codice sorgente

:recycle: Refactoring LispOperator

Arthur Brandao 6 anni fa
parent
commit
d419fd840e

+ 17 - 16
src/migl/lisp/LispImpl.java

@@ -23,13 +23,13 @@ public class LispImpl implements Lisp {
 	static {
 		//Définition des opérateurs
 		operators.put("cons", new ConsOperator());
-		operators.put("quote", (lisp) -> {
+		operators.put("quote", (op, lisp) -> {
 			if(lisp.size() != 1) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
 			return LispElement.generate(lisp.car().toString());
 		});
-		operators.put("if", (lisp) -> {
+		operators.put("if", (op, lisp) -> {
 			if(lisp.size() != 3) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -39,14 +39,14 @@ public class LispImpl implements Lisp {
 				return LispElement.getElement(lisp.cdr().cdr().car());
 			}
 		});
-		operators.put("not", (lisp) -> {
+		operators.put("not", (op, lisp) -> {
 			if(lisp.size() != 1) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
 			boolean result = !LispElement.getElement(lisp.car()).toBoolean();
 			return LispElement.generate(result);
 		});
-		operators.put("and", (lisp) -> {
+		operators.put("and", (op, lisp) -> {
 			boolean result = true;
 			while(!lisp.isEmpty()) {
 				result = result && LispElement.getElement(lisp.car()).toBoolean();
@@ -54,7 +54,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put("or", (lisp) -> {
+		operators.put("or", (op, lisp) -> {
 			boolean result = false;
 			while(!lisp.isEmpty()) {
 				result = result || LispElement.getElement(lisp.car()).toBoolean();
@@ -62,7 +62,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put(">", (lisp) -> {
+		operators.put(">", (op, lisp) -> {
 			if(lisp.size() == 0) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -82,7 +82,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put(">=", (lisp) -> {
+		operators.put(">=", (op, lisp) -> {
 			if(lisp.size() == 0) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -102,7 +102,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put("<", (lisp) -> {
+		operators.put("<", (op, lisp) -> {
 			if(lisp.size() == 0) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -122,7 +122,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put("<=", (lisp) -> {
+		operators.put("<=", (op, lisp) -> {
 			if(lisp.size() == 0) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -142,7 +142,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put("=", (lisp) -> {
+		operators.put("=", (op, lisp) -> {
 			if(lisp.size() == 0) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -160,7 +160,7 @@ public class LispImpl implements Lisp {
 			}
 			return LispElement.generate(result);
 		});
-		operators.put("+", (lisp) -> {
+		operators.put("+", (op, lisp) -> {
 			BigInteger resultInt = new BigInteger("0");
             while(!lisp.isEmpty()){
                 LispElement eltInt = LispElement.getElement(lisp.car());
@@ -179,7 +179,7 @@ public class LispImpl implements Lisp {
             }
             return LispElement.generate(result);
 		});
-		operators.put("*", (lisp) -> {
+		operators.put("*", (op, lisp) -> {
 			BigInteger resultInt = new BigInteger("1");
             while(!lisp.isEmpty()){
                 LispElement eltInt = LispElement.getElement(lisp.car());
@@ -198,7 +198,7 @@ public class LispImpl implements Lisp {
             }
             return LispElement.generate(result);
 		});
-		operators.put("-", (lisp) -> {
+		operators.put("-", (op, lisp) -> {
 			switch(lisp.size()) {				
 				case 1:
 					LispElement elt = LispElement.getElement(lisp.car());
@@ -219,7 +219,7 @@ public class LispImpl implements Lisp {
 					throw new LispError(LispError.ERR_NUM_ARG);
 			}
 		});
-		operators.put("/", (lisp) -> {
+		operators.put("/", (op, lisp) -> {
 			if(lisp.size() != 2) {
 				throw new LispError(LispError.ERR_NUM_ARG);
 			}
@@ -408,12 +408,13 @@ public class LispImpl implements Lisp {
 		if(lisp.isEmpty()) {
 			return LispElement.generate("()");
 		}
-		LispOperator op = operators.get(LispElement.generate(lisp.car()).toStr());
+		String operator = LispElement.generate(lisp.car()).toStr();
+		LispOperator op = operators.get(operator);
 		if(op == null) {
 			throw new LispError(new UnsupportedOperationException("Unknow expression"));
 		}
 		try {
-			return op.apply(lisp.cdr());
+			return op.apply(operator, lisp.cdr());
 		} catch (IllegalStateException ex) {
 			throw new LispError(ex.getMessage(), ex);
 		} catch (IllegalArgumentException ex) {

+ 1 - 1
src/migl/lisp/LispOperator.java

@@ -5,6 +5,6 @@ import migl.util.ConsList;
 @FunctionalInterface
 public interface LispOperator {
 
-	LispElement apply(ConsList<Object> lisp) throws LispError;
+	LispElement apply(String operator, ConsList<Object> lisp) throws LispError;
 	
 }

+ 1 - 1
src/migl/lisp/operator/ConsOperator.java

@@ -9,7 +9,7 @@ import migl.util.ConsListFactory;
 public class ConsOperator implements LispOperator {
 
 	@Override
-	public LispElement apply(ConsList<Object> lisp) throws LispError {
+	public LispElement apply(String operator, ConsList<Object> lisp) throws LispError {
 		if(lisp.size() != 2) {
 			throw new LispError(LispError.ERR_NUM_ARG);
 		}