From a5b176332457a6a9465fc8242af5736041a54323 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 26 May 2022 18:40:26 -0400 Subject: [PATCH] Add import error handling --- src/ludus/interpreter.clj | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/ludus/interpreter.clj b/src/ludus/interpreter.clj index 1c9be13..4b59b5a 100644 --- a/src/ludus/interpreter.clj +++ b/src/ludus/interpreter.clj @@ -340,12 +340,13 @@ name (:name ast)] (if (contains? @ctx name) (throw (ex-info (str "Name " name " is alrady bound") {:ast ast})) - (let [result ;; TODO: add any error handling at all - (-> path - (loader/load-import (resolve-word ::file ctx)) - (scanner/scan) - (parser/parse) - (interpret path))] + (let [source (try + (loader/load-import path (resolve-word ::file ctx)) + (catch Exception e + (if (::loader/error (ex-data e)) + (throw (ex-info (ex-message e) {:ast ast})) + (throw e)))) + result (-> source (scanner/scan) (parser/parse) (interpret path))] (vswap! ctx update-ctx {name result}) result ;; TODO: test this! )))) @@ -518,9 +519,9 @@ (interpret-ast (::parser/ast parsed) {::file file}) (catch clojure.lang.ExceptionInfo e (println "Ludus panicked in" file) - (println "On line" (get-in e [:ast :token ::token/line])) + (println "On line" (get-in (ex-data e) [:ast :token ::token/line])) (println (ex-message e)) - (pp/pprint (ex-data e)) + ;;(pp/pprint (ex-data e)) (System/exit 67)))) (defn interpret-safe [parsed] @@ -532,7 +533,7 @@ (println (ex-message e)) (pp/pprint (ex-data e))))) -(do +(comment (def source "panic! :oops