Keep working, cljs repl :(((((

This commit is contained in:
Scott Richmond 2023-11-24 13:17:20 -05:00
parent 7ec258ee24
commit a23e3bfdc5
11 changed files with 1287 additions and 38 deletions

8
assets/index.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>

View File

@ -1,11 +1,13 @@
{ {:deps
:deps {org.clojure/clojurescript {:mvn/version "1.11.121"}} {org.clojure/clojurescript {:mvn/version "1.11.121"}
thheller/shadow-cljs {:mvn/version "2.26.0"}}
:aliases :aliases
{:repl {:main
{:exec-fn ludus.core/main!}
:repl
{:exec-fn clojure.core.server/start-server {:exec-fn clojure.core.server/start-server
:exec-args {:name "repl" :exec-args {:name "repl"
:port 5555 :port 5555
:accept clojure.core.server/repl :accept clojure.core.server/repl
:server-daemon false}}} :server-daemon false}}}}
}

1138
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "ludus",
"version": "1.0.0",
"description": "![Ludus logo](logo.png) ## Ludus: A friendly, dynamic, functional language",
"main": "index.js",
"directories": {
"doc": "doc",
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"shadow-cljs": "^2.26.0"
}
}

View File

@ -1,11 +1,12 @@
(defproject ludus "0.1.0-SNAPSHOT" (defproject ludus "0.1.0-SNAPSHOT"
:description "FIXME: write description" :description "FIXME: write description"
:url "http://example.com/FIXME" :url "http://ludus.dev"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"} :url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.11.1"] :dependencies [[org.clojure/clojure "1.11.1"]
[babashka/fs "0.1.6"] ;[babashka/fs "0.1.6"]
[quil "4.0.0-SNAPSHOT"]] ;[quil "4.0.0-SNAPSHOT"]
]
:plugins [[lein-cljfmt "0.8.0"]] :plugins [[lein-cljfmt "0.8.0"]]
:repl-options {:init-ns ludus.core} :repl-options {:init-ns ludus.core}
:main ludus.core :main ludus.core

13
shadow-cljs.edn Normal file
View File

@ -0,0 +1,13 @@
;; shadow-cljs configuration
{:deps true
:builds
{:node {:target :node-library
:output-to "target/js/ludus.js"
:exports-fn ludus.core/run
:asset-path "/js"
:modules {:main {:entries [ludus.core]}}}
:browser {:target :browser
:output-dir "target/js"
:asset-path "/assets"
:modules {:main {:entries [ludus.core]}}}}}

View File

@ -5,21 +5,19 @@
[ludus.grammar :as grammar] [ludus.grammar :as grammar]
[ludus.interpreter :as interpreter] [ludus.interpreter :as interpreter]
[ludus.show :as show] [ludus.show :as show]
[clojure.pprint :as pp]
)) ))
(println "Hi, there.")
(defn run [source] (defn run [source]
(println (str "Running some ludus source: " source)) (println (str "Running some ludus source: " source))
(let [scanned (scanner/scan source)] (let [scanned (scanner/scan source)]
(if (not-empty (:errors scanned)) (if (not-empty (:errors scanned))
(do (do
(println "I found some scanning errors!") (println "I found some scanning errors!")
(pp/pprint (:errors scanned)) (println (:errors scanned))
nil
) )
(let [parsed (parser/apply-parser grammar/script (:tokens scanned))] (let [parsed (parser/apply-parser grammar/script (:tokens scanned))]
(println "Scanned: ")
(println scanned)
(if (parser/fail? parsed) (if (parser/fail? parsed)
(do (do
(println "I found some parsing errors!") (println "I found some parsing errors!")
@ -28,12 +26,21 @@
) )
(let [interpreted (interpreter/interpret source parsed)] (let [interpreted (interpreter/interpret source parsed)]
(println (show/show interpreted)) (println (show/show interpreted))
interpreted interpreted))))))
))))))
(run " (defn main! []
(println "Ludus says, hi there...")
#?(:clj (println "...from Clojure.")
:cljs (println "...from ClojureScript."))
(run ":foo")
(run "add (1, 2)")
(run "nil")
(run "if true then :foo else :bar")
)
fn foo () -> :bar (run ":foo")
(run "add (1, 2)")
foo () (run "nil")
") (run "if true then :foo else :bar")

View File

@ -11,7 +11,9 @@
(declare expression pattern) (declare expression pattern)
(defp separator choice [:comma :newline :break]) ; (defp separator choice [:comma :newline :break])
(defn separator [] (choice :separator [:comma :newline :break]))
(defp separators quiet one+ separator) (defp separators quiet one+ separator)
@ -24,6 +26,7 @@
(defp splat group order-1 [(quiet :splat) :word]) (defp splat group order-1 [(quiet :splat) :word])
(defp patt-splat-able flat choice [:word :ignored :placeholder]) (defp patt-splat-able flat choice [:word :ignored :placeholder])
(defp splattern group order-1 [(quiet :splat) (maybe patt-splat-able)]) (defp splattern group order-1 [(quiet :splat) (maybe patt-splat-able)])
(defp literal flat choice [:nil :true :false :number :string]) (defp literal flat choice [:nil :true :false :number :string])

View File

@ -840,12 +840,18 @@
; (println (ex-message e)) ; (println (ex-message e))
; (System/exit 67)))) ; (System/exit 67))))
(def runtime-error
#?(
:clj clojure.lang.ExceptionInfo
:cljs js/Object
))
;; TODO: update this to use new parser pipeline & new AST representation ;; TODO: update this to use new parser pipeline & new AST representation
(defn interpret [source parsed] (defn interpret [source parsed]
(try (try
(let [base-ctx (volatile! {::parent (volatile! prelude/prelude)})] (let [base-ctx (volatile! {::parent (volatile! prelude/prelude)})]
(interpret-ast parsed base-ctx)) (interpret-ast parsed base-ctx))
(catch clojure.lang.ExceptionInfo e (catch #?(:cljs :default :clj Throwable) e
(println "Ludus panicked!") (println "Ludus panicked!")
(println "On line" (get-in (ex-data e) [:ast :token :line])) (println "On line" (get-in (ex-data e) [:ast :token :line]))
(println ">>> " (get-line source (get-in (ex-data e) [:ast :token :line]))) (println ">>> " (get-line source (get-in (ex-data e) [:ast :token :line])))

View File

@ -3,6 +3,7 @@
[ludus.data :as data] [ludus.data :as data]
[ludus.show :as show] [ludus.show :as show]
;[ludus.draw :as d] ;[ludus.draw :as d]
#?(:cljs [cljs.reader])
)) ))
;; TODO: make eq, and, or special forms that short-circuit ;; TODO: make eq, and, or special forms that short-circuit
@ -96,9 +97,6 @@
::data/type ::data/clj ::data/type ::data/clj
:body ludus.show/show}) :body ludus.show/show})
(def sleep- {:name "sleep"
::data/type ::data/clj
:body (fn [ms] (Thread/sleep ms))})
(def conj- {:name "conj" (def conj- {:name "conj"
::data/type ::data/clj ::data/type ::data/clj
:body conj}) :body conj})
@ -142,36 +140,85 @@
(contains? xs (inc i)) (nth xs (inc i)) (contains? xs (inc i)) (nth xs (inc i))
:else default)))}) :else default)))})
(def types {
:keyword
#?(
:clj clojure.lang.Keyword
:cljs cljs.core/Keyword
)
:long
#?(
:clj java.lang.Long
:cljs js/Number
)
:double
#?(
:clj java.lang.Double
:cljs js/Number
)
:string
#?(
:clj java.lang.String
:cljs js/String
)
:boolean
#?(
:clj java.lang.Boolean
:cljs js/Boolean
)
:set
#?(
:clj clojure.lang.PersistentHashSet
:cljs cljs.core/PersistentHashSet
)
:vector
#?(
:clj clojure.lang.PersistentVector
:cljs cljs.core/PersistentVector
)
:map
#?(
:clj clojure.lang.PersistentArrayMap
:cljs cljs.core/PersistentArrayMap
)
})
(defn get-type [value] (defn get-type [value]
(let [t (type value)] (let [t (type value)]
(cond (cond
(nil? value) :nil (nil? value) :nil
(= clojure.lang.Keyword t) :keyword (= (:keyword types) t) :keyword
(= java.lang.Long t) :number (= (:long types) t) :number
(= java.lang.Double t) :number (= (:double types) t) :number
(= java.lang.String t) :string (= (:string types) t) :string
(= java.lang.Boolean t) :boolean (= (:boolean types) t) :boolean
(= clojure.lang.PersistentHashSet t) :set (= (:set types) t) :set
;; tuples and lists ;; tuples and lists
(= clojure.lang.PersistentVector t) (= (:vector types) t)
(if (= ::data/tuple (first value)) :tuple :list) (if (= ::data/tuple (first value)) :tuple :list)
;; structs dicts namespaces refs ;; structs dicts namespaces refs
(= clojure.lang.PersistentArrayMap t) (= (:map types) t)
(cond (cond
(::data/type value) (case (::data/type value) (::data/type value) (case (::data/type value)
(::data/fn ::data/clj) :fn (::data/fn ::data/clj) :fn
::data/ns :ns) ::data/ns :ns)
(::data/dict value) :dict (::data/dict value) :dict
(::data/struct value) :struct (::data/struct value) :struct
:else :none :else :none
)))) ))))
@ -181,11 +228,17 @@
(defn strpart [kw] (->> kw str rest (apply str))) (defn strpart [kw] (->> kw str rest (apply str)))
(def readstr
#?(
:clj read-string
:cljs cljs.reader/read-string
))
(def clj {:name "clj" (def clj {:name "clj"
::data/type ::data/clj ::data/type ::data/clj
:body (fn [& args] :body (fn [& args]
(println "Args passed: " args) (println "Args passed: " args)
(let [called (-> args first strpart read-string eval) (let [called (-> args first strpart readstr eval)
fn-args (rest args)] fn-args (rest args)]
(println "Fn: " called) (println "Fn: " called)
(println "Args: " fn-args) (println "Args: " fn-args)
@ -215,7 +268,6 @@
"set!" set!- "set!" set!-
"and" and- "and" and-
"or" or- "or" or-
"sleep" sleep-
"assoc" assoc- "assoc" assoc-
"conj" conj- "conj" conj-
"get" get- "get" get-

View File

@ -1,4 +1,4 @@
(ns cludus.core-test (ns ludus.core-test
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[cludus.core :refer :all])) [cludus.core :refer :all]))