|
@@ -10,6 +10,8 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
import migl.lisp.operator.ComparatorOperator;
|
|
|
import migl.lisp.operator.ConsOperator;
|
|
|
+import migl.lisp.operator.DefineOperator;
|
|
|
+import migl.lisp.operator.MinMaxOperator;
|
|
|
import migl.util.ConsList;
|
|
|
import migl.util.ConsListFactory;
|
|
|
|
|
@@ -23,12 +25,16 @@ public class LispImpl implements Lisp {
|
|
|
private static Map<String, LispOperator> operators = new HashMap<>();
|
|
|
static {
|
|
|
//Définition des opérateurs
|
|
|
+ operators.put("define", new DefineOperator());
|
|
|
+ operators.put("set!", new DefineOperator());
|
|
|
operators.put("cons", new ConsOperator());
|
|
|
operators.put(">", new ComparatorOperator());
|
|
|
operators.put(">=", new ComparatorOperator());
|
|
|
operators.put("<", new ComparatorOperator());
|
|
|
operators.put("<=", new ComparatorOperator());
|
|
|
operators.put("=", new ComparatorOperator());
|
|
|
+ operators.put("min", new MinMaxOperator());
|
|
|
+ operators.put("max", new MinMaxOperator());
|
|
|
operators.put("quote", (op, lisp) -> {
|
|
|
if(lisp.size() != 1) {
|
|
|
throw new LispError(LispError.ERR_NUM_ARG);
|
|
@@ -174,7 +180,8 @@ public class LispImpl implements Lisp {
|
|
|
}
|
|
|
//Si element seul on évalue directement sa valeur
|
|
|
try {
|
|
|
- return LispElement.generate(lisp);
|
|
|
+ //return LispElement.generate(lisp);
|
|
|
+ return DefineOperator.eval(lisp);
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
throw new LispError(ex.getMessage(), ex);
|
|
|
}
|
|
@@ -209,7 +216,7 @@ public class LispImpl implements Lisp {
|
|
|
* @return
|
|
|
*/
|
|
|
private boolean verifyList(String expr) {
|
|
|
- Pattern p = Pattern.compile("\\(([ |\t]*[A-Za-z0-9\\.\\+\\-\\/\\*<>=#]+[ |\t]*)*\\)");
|
|
|
+ Pattern p = Pattern.compile("\\(([ |\t]*[A-Za-z0-9\\.\\+\\-\\/\\*<>=#!]+[ |\t]*)*\\)");
|
|
|
Matcher m = p.matcher(expr);
|
|
|
//Si pas de correspondance
|
|
|
if(!m.find()) {
|
|
@@ -321,7 +328,7 @@ public class LispImpl implements Lisp {
|
|
|
String operator = LispElement.generate(lisp.car()).toStr();
|
|
|
LispOperator op = operators.get(operator);
|
|
|
if(op == null) {
|
|
|
- throw new LispError(new UnsupportedOperationException("Unknow expression"));
|
|
|
+ throw new LispError(new UnsupportedOperationException(operator + LispError.ERR_UNKNOW));
|
|
|
}
|
|
|
try {
|
|
|
return op.apply(operator, lisp.cdr());
|
|
@@ -331,5 +338,24 @@ public class LispImpl implements Lisp {
|
|
|
throw new LispError("List Lisp malformed: " + ex.getMessage(), ex);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Verifie que le nom pour une variable n'est pas interdit
|
|
|
+ *
|
|
|
+ * @param name Nom pour la variable
|
|
|
+ * @throws LispError Si le nom n'est pas valide
|
|
|
+ */
|
|
|
+ public static void verifyForbiddenName(String name) throws LispError {
|
|
|
+ //Verifie que ce n'est pas une valeur (double, bool, ...)
|
|
|
+ try {
|
|
|
+ LispElement.valueOf(name).toStr();
|
|
|
+ } catch (IllegalStateException ex) {
|
|
|
+ throw new LispError(name + " is not a valid identifier", ex);
|
|
|
+ }
|
|
|
+ //Verifie que ce n'est pas le nom d'un operateur
|
|
|
+ if(operators.containsKey(name)) {
|
|
|
+ throw new LispError(name + " is not a valid identifier");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|