diff --git a/src/ludus/core.clj b/src/ludus/core.clj index 825611e..f74dc43 100644 --- a/src/ludus/core.clj +++ b/src/ludus/core.clj @@ -5,10 +5,11 @@ [ludus.parser :as parser] [ludus.interpreter :as interpreter] [ludus.show :as show] - [clojure.pprint :as pp]) + [clojure.pprint :as pp] + [ludus.loader :as loader]) (:gen-class)) -(defn- run [source] +(defn- run [file source] (let [scanned (scanner/scan source)] (if (not-empty (:errors scanned)) (do @@ -21,13 +22,17 @@ (println "I found some parsing errors!") (pp/pprint (:errors parsed)) (System/exit 66)) - (let [interpreted (interpreter/interpret parsed)] + (let [interpreted (interpreter/interpret parsed file)] (println (show/show interpreted)) (System/exit 0))))))) (defn -main [& args] (cond - (= (count args) 1) (run (slurp (first args))) + (= (count args) 1) + (let [file (first args) + source (loader/load-import file)] + (run file source)) + :else (do (println "Usage: ludus [script]") (System/exit 64)))) \ No newline at end of file diff --git a/src/ludus/interpreter.clj b/src/ludus/interpreter.clj index 53558d4..3e750a7 100644 --- a/src/ludus/interpreter.clj +++ b/src/ludus/interpreter.clj @@ -6,6 +6,7 @@ [ludus.prelude :as prelude] [ludus.data :as data] [ludus.show :as show] + [ludus.loader :as loader] [clojure.pprint :as pp] [clojure.set])) @@ -340,17 +341,17 @@ (throw (ex-info (str "Name " name " is alrady bound") {:ast ast})) (let [result ;; TODO: add any error handling at all (-> path - (slurp) + (loader/load-import (resolve-word ::file ctx)) (scanner/scan) (parser/parse) - (interpret))] + (interpret path))] (vswap! ctx update-ctx {name result}) result ;; TODO: test this! )))) (defn- interpret-ref [ast ctx] (let [name (:name ast) expr (:expr ast)] - (if (contains? @ctx name) + (when (contains? @ctx name) (throw (ex-info (str "Name " name " is already bound") {:ast ast}))) (let [value (interpret-ast expr ctx) box (atom value) @@ -485,7 +486,7 @@ (let [exprs (:exprs ast) inner (pop exprs) last (peek exprs) - ctx (volatile! prelude/prelude)] + ctx (volatile! (merge prelude/prelude ctx))] (run! #(interpret-ast % ctx) inner) (interpret-ast last ctx)) @@ -511,11 +512,12 @@ (throw (ex-info "Unknown AST node type" {:ast ast})))) -(defn interpret [parsed] +(defn interpret [parsed file] (try - (interpret-ast (::parser/ast parsed) {}) + (interpret-ast (::parser/ast parsed) {::file file}) (catch clojure.lang.ExceptionInfo e - (println "Ludus panicked!") + (println "Ludus panicked in" file) + (println "On line" (get-in e [:ast :token :line])) (println (ex-message e)) (pp/pprint (ex-data e)) (System/exit 67))))