Use the little file loader

This commit is contained in:
Scott Richmond 2022-05-24 18:52:45 -04:00
parent d8d1bf0858
commit 907d4e2b31
2 changed files with 18 additions and 11 deletions

View File

@ -5,10 +5,11 @@
[ludus.parser :as parser] [ludus.parser :as parser]
[ludus.interpreter :as interpreter] [ludus.interpreter :as interpreter]
[ludus.show :as show] [ludus.show :as show]
[clojure.pprint :as pp]) [clojure.pprint :as pp]
[ludus.loader :as loader])
(:gen-class)) (:gen-class))
(defn- run [source] (defn- run [file source]
(let [scanned (scanner/scan source)] (let [scanned (scanner/scan source)]
(if (not-empty (:errors scanned)) (if (not-empty (:errors scanned))
(do (do
@ -21,13 +22,17 @@
(println "I found some parsing errors!") (println "I found some parsing errors!")
(pp/pprint (:errors parsed)) (pp/pprint (:errors parsed))
(System/exit 66)) (System/exit 66))
(let [interpreted (interpreter/interpret parsed)] (let [interpreted (interpreter/interpret parsed file)]
(println (show/show interpreted)) (println (show/show interpreted))
(System/exit 0))))))) (System/exit 0)))))))
(defn -main [& args] (defn -main [& args]
(cond (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 :else (do
(println "Usage: ludus [script]") (println "Usage: ludus [script]")
(System/exit 64)))) (System/exit 64))))

View File

@ -6,6 +6,7 @@
[ludus.prelude :as prelude] [ludus.prelude :as prelude]
[ludus.data :as data] [ludus.data :as data]
[ludus.show :as show] [ludus.show :as show]
[ludus.loader :as loader]
[clojure.pprint :as pp] [clojure.pprint :as pp]
[clojure.set])) [clojure.set]))
@ -340,17 +341,17 @@
(throw (ex-info (str "Name " name " is alrady bound") {:ast ast})) (throw (ex-info (str "Name " name " is alrady bound") {:ast ast}))
(let [result ;; TODO: add any error handling at all (let [result ;; TODO: add any error handling at all
(-> path (-> path
(slurp) (loader/load-import (resolve-word ::file ctx))
(scanner/scan) (scanner/scan)
(parser/parse) (parser/parse)
(interpret))] (interpret path))]
(vswap! ctx update-ctx {name result}) (vswap! ctx update-ctx {name result})
result ;; TODO: test this! result ;; TODO: test this!
)))) ))))
(defn- interpret-ref [ast ctx] (defn- interpret-ref [ast ctx]
(let [name (:name ast) expr (:expr ast)] (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}))) (throw (ex-info (str "Name " name " is already bound") {:ast ast})))
(let [value (interpret-ast expr ctx) (let [value (interpret-ast expr ctx)
box (atom value) box (atom value)
@ -485,7 +486,7 @@
(let [exprs (:exprs ast) (let [exprs (:exprs ast)
inner (pop exprs) inner (pop exprs)
last (peek exprs) last (peek exprs)
ctx (volatile! prelude/prelude)] ctx (volatile! (merge prelude/prelude ctx))]
(run! #(interpret-ast % ctx) inner) (run! #(interpret-ast % ctx) inner)
(interpret-ast last ctx)) (interpret-ast last ctx))
@ -511,11 +512,12 @@
(throw (ex-info "Unknown AST node type" {:ast ast})))) (throw (ex-info "Unknown AST node type" {:ast ast}))))
(defn interpret [parsed] (defn interpret [parsed file]
(try (try
(interpret-ast (::parser/ast parsed) {}) (interpret-ast (::parser/ast parsed) {::file file})
(catch clojure.lang.ExceptionInfo e (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)) (println (ex-message e))
(pp/pprint (ex-data e)) (pp/pprint (ex-data e))
(System/exit 67)))) (System/exit 67))))