]> git.lizzy.rs Git - nothing.git/commitdiff
(#326) error as struct Expr
authorrexim <reximkut@gmail.com>
Mon, 10 Sep 2018 22:29:10 +0000 (05:29 +0700)
committerrexim <reximkut@gmail.com>
Mon, 10 Sep 2018 22:29:10 +0000 (05:29 +0700)
src/script/interpreter.c
src/script/interpreter.h
src/script/repl.c

index 004610ad169117936daa265758fe2938a9817bb6..93316899c2fea2fa78a3881249359b374372d55f 100644 (file)
@@ -13,19 +13,17 @@ struct EvalResult eval_success(struct Expr expr, struct Expr scope)
         .is_error = false,
         .expr = expr,
         .scope = scope,
-        .error = NULL
     };
 
     return result;
 }
 
-struct EvalResult eval_failure(const char *error, struct Expr expr, struct Expr scope)
+struct EvalResult eval_failure(struct Expr error, struct Expr scope)
 {
     struct EvalResult result = {
         .is_error = true,
-        .error = error,
-        .scope = scope,
-        .expr = expr
+        .expr = error,
+        .scope = scope
     };
 
     return result;
@@ -44,7 +42,10 @@ static struct EvalResult eval_atom(Gc *gc, struct Expr scope, struct Atom *atom)
         return eval_success(atom_as_expr(atom), scope);
     }
 
-    return eval_failure("Unexpected expression", atom_as_expr(atom), scope);
+    return eval_failure(CONS(gc,
+                             SYMBOL(gc, "unexpected-expression"),
+                             atom_as_expr(atom)),
+                        scope);
 }
 
 static struct EvalResult eval_args(Gc *gc, struct Expr scope, struct Expr args)
@@ -73,7 +74,10 @@ static struct EvalResult eval_args(Gc *gc, struct Expr scope, struct Expr args)
     default: {}
     }
 
-    return eval_failure("Unexpected expression", args, scope);
+    return eval_failure(CONS(gc,
+                             SYMBOL(gc, "unexpected-expression"),
+                             args),
+                        scope);
 }
 
 static struct EvalResult plus_op(Gc *gc, struct Expr args, struct Expr scope)
@@ -82,12 +86,18 @@ static struct EvalResult plus_op(Gc *gc, struct Expr args, struct Expr scope)
 
     while (!nil_p(args)) {
         if (args.type != EXPR_CONS) {
-            return eval_failure("Expected cons", args, scope);
+            return eval_failure(CONS(gc,
+                                     SYMBOL(gc, "expected-cons"),
+                                     args),
+                                scope);
         }
 
         if (args.cons->car.type != EXPR_ATOM ||
             args.cons->car.atom->type != ATOM_NUMBER) {
-            return eval_failure("Expected number", args.cons->car, scope);
+            return eval_failure(CONS(gc,
+                                     SYMBOL(gc, "expected-number"),
+                                     args.cons->car),
+                                scope);
         }
 
         result += args.cons->car.atom->num;
@@ -103,7 +113,10 @@ static struct EvalResult eval_funcall(Gc *gc, struct Expr scope, struct Cons *co
     (void) scope;
 
     if (!symbol_p(cons->car)) {
-        return eval_failure("Expected symbol", cons->car, scope);
+        return eval_failure(CONS(gc,
+                                 SYMBOL(gc, "expected-symbol"),
+                                 cons->car),
+                            scope);
     }
 
     /* TODO(#323): set builtin function is not implemented */
@@ -115,7 +128,10 @@ static struct EvalResult eval_funcall(Gc *gc, struct Expr scope, struct Cons *co
         return plus_op(gc, args.expr, scope);
     }
 
-    return eval_failure("Unknown function", cons->car, scope);
+    return eval_failure(CONS(gc,
+                             SYMBOL(gc, "unknown-function"),
+                             cons->car),
+                        scope);
 }
 
 struct EvalResult eval(Gc *gc, struct Expr scope, struct Expr expr)
@@ -130,15 +146,8 @@ struct EvalResult eval(Gc *gc, struct Expr scope, struct Expr expr)
     default: {}
     }
 
-    return eval_failure("Unexpected expression", expr, scope);
-}
-
-void print_eval_error(FILE *stream, struct EvalResult result)
-{
-    if (!result.is_error) {
-        return;
-    }
-
-    fprintf(stream, "%s\n", result.error);
-    print_expr_as_sexpr(result.expr);
+    return eval_failure(CONS(gc,
+                             SYMBOL(gc, "unexpected-expression"),
+                             expr),
+                        scope);
 }
index 1b89b259c938c9f9cc220aa7ad9f853181ed90a2..d61ac7adce63173879a228dbe6c8f9b73ca81870 100644 (file)
@@ -11,15 +11,11 @@ struct EvalResult
     bool is_error;
     struct Expr expr;
     struct Expr scope;
-    // TODO(#326): make EvalResult.error a struct Expr
-    const char *error;
 };
 
 struct EvalResult eval_success(struct Expr expr, struct Expr scope);
-struct EvalResult eval_failure(const char *error, struct Expr expr, struct Expr scope);
+struct EvalResult eval_failure(struct Expr expr, struct Expr scope);
 
 struct EvalResult eval(Gc *gc, struct Expr scope, struct Expr expr);
 
-void print_eval_error(FILE *stream, struct EvalResult result);
-
 #endif  // INTERPRETER_H_
index 068fca839eedd40e5c18777e76b6ba886e92bfe3..28d7c981410fbf2dc23896c92637261335b358ca 100644 (file)
@@ -37,11 +37,6 @@ int main(int argc, char *argv[])
         gc_inspect(gc);
 
         struct EvalResult eval_result = eval(gc, scope, parse_result.expr);
-        scope = eval_result.scope;
-        if (eval_result.is_error) {
-            print_eval_error(stderr, eval_result);
-            continue;
-        }
         printf("After eval:\t");
         gc_inspect(gc);
 
@@ -53,6 +48,10 @@ int main(int argc, char *argv[])
         print_expr_as_sexpr(eval_result.scope);
         printf("\n");
 
+        if (eval_result.is_error) {
+            printf("Error:\t");
+        }
+
         print_expr_as_sexpr(eval_result.expr);
         printf("\n");
     }