ludus/src/base.janet

308 lines
6.7 KiB
Plaintext
Raw Normal View History

# A base library for Ludus
# Only loaded in the prelude
2024-05-19 22:04:08 +00:00
(defn bool [x] (if (= :^nil x) nil x))
(defn ludus/and [& args] (every? (map bool args)))
(defn ludus/or [& args] (some bool args))
(defn ludus/type [value]
(when (= :^nil value) (break :nil))
2024-05-19 22:04:08 +00:00
(def typed? (when (dictionary? value) (value :^type)))
(def the-type (if typed? typed? (type value)))
(case the-type
:buffer :string
:boolean :bool
:array :list
:table :dict
:cfunction :function
the-type))
(var stringify nil)
(defn- dict-str [dict]
(string/join
(map
(fn [[k v]] (string (stringify k) " " (stringify v)))
(pairs dict))
", "))
(defn- stringish? [x] (or (string? x) (buffer? x)))
(defn- stringify* [value]
(when (stringish? value) (break value))
(def type (ludus/type value))
(case type
:nil ""
:number (string value)
2024-05-19 22:04:08 +00:00
:bool (string value)
:keyword (string ":" value)
:tuple
(string/join (map stringify value) ", ")
:list
(string/join (map stringify value) ", ")
:dict (dict-str value)
:set
(string/join (map stringify (keys value)) ", ")
2024-06-05 19:52:03 +00:00
:box (stringify (value :^value))
2024-05-19 22:04:08 +00:00
:fn (string "fn " (value :name))
:function (string "builtin " (string value))
2024-06-04 18:50:48 +00:00
:pkg (dict-str value)
2024-05-19 22:04:08 +00:00
))
(set stringify stringify*)
2024-06-07 21:16:29 +00:00
(var show nil)
2024-06-04 18:50:48 +00:00
(defn- show-pkg [x]
(def tab (struct/to-table x))
(set (tab :^name) nil)
(set (tab :^type) nil)
(string "pkg " (x :^name) " {" (stringify tab) "}")
)
2024-06-07 21:16:29 +00:00
(defn- dict-show [dict]
(string/join
(map
2024-06-07 21:16:29 +00:00
(fn [[k v]] (string (show k) " " (show v)))
(pairs dict))
", "))
2024-06-16 01:52:50 +00:00
(defn- set-show [sett]
(def prepped (merge sett))
(set (prepped :^type) nil)
(def shown (map show (keys prepped)))
(string/join shown ", ")
)
2024-06-07 21:16:29 +00:00
(defn- show* [x]
(case (ludus/type x)
2024-06-07 21:16:29 +00:00
:nil "nil"
:string (string "\"" x "\"")
2024-06-07 21:16:29 +00:00
:tuple (string "(" (string/join (map show x) ", ") ")")
:list (string "[" (string/join (map show x) ", ") "]")
:dict (string "#{" (dict-show x) "}")
2024-06-16 01:52:50 +00:00
:set (string "${" (set-show x) "}")
2024-06-14 19:55:57 +00:00
:box (string "box " (x :name) " [ " (show (x :^value)) " ]")
2024-06-07 21:16:29 +00:00
:pkg (show-pkg x)
(stringify x)))
(set show show*)
# (var json nil)
# (defn- dict-json [dict]
# (string/join
# (map
# (fn [[k v]] (string (json k) ": " (json v)))
# (pairs dict))
# ", "))
# (defn- json* [x]
# (case (ludus/type x)
# :nil "\"null\""
# :number (string x)
# :bool (if true "\"true\"" "\"false\"")
# :string (string "\"" x "\"")
# :keyword (string "\"" x "\"")
# :tuple (string "[" (string/join (map json x) ", ") "]")
# :list (string "[" (string/join (map json x) ", ")"]")
# :dict (string "{" (dict-json x) "}")
# :set (string "[" (string/join (map json (keys x)) ", ") "]")
# (show x)))
# (set json json*)
2024-06-06 00:16:29 +00:00
(defn show-patt [x]
2024-06-04 17:00:34 +00:00
(case (x :type)
:nil "nil"
:bool (string (x :data))
:number (string (x :data))
:keyword (string ":" (x :data))
:word (x :data)
:placeholder (get-in x [:token :lexeme])
:tuple (string "(" (string/join (map show-patt (x :data)) ", ") ")")
:list (string "[" (string/join (map show-patt (x :data)) ", ")"]")
:dict (string "#{" (string/join (map show-patt (x :data)) ", ") "}")
:pair (string (show-patt (get-in x [:data 0])) " " (show-patt (get-in x [:data 1])))
:typed (string (show-patt (get-in x [:data 1])) " as " (show-patt (get-in x [:data 0])))
:interpolated (get-in x [:token :lexeme])
:string (get-in x [:token :lexeme])
2024-06-06 00:16:29 +00:00
:splat (string "..." (when (x :splatted) (show-patt (x :splatted))))
2024-06-04 17:02:15 +00:00
(error (string "cannot show pattern of unknown type " (x :type)))))
2024-06-04 17:00:34 +00:00
(defn pretty-patterns [fnn]
(def {:body clauses} fnn)
2024-06-14 18:53:23 +00:00
(string/join (map (fn [x] (-> x first show-patt)) clauses) "\n"))
2024-06-06 20:14:04 +00:00
(defn doc [fnn]
2024-06-15 21:03:37 +00:00
(when (not= :fn (ludus/type fnn)) (break "No documentation available."))
(def {:name name :doc docstring} fnn)
(string/join [name
(pretty-patterns fnn)
(if docstring docstring "No docstring available.")]
"\n"))
2024-06-04 17:00:34 +00:00
(defn- conj!-set [sett value]
(set (sett value) true)
sett)
2024-05-19 22:04:08 +00:00
(defn- conj-set [sett value]
(def new (merge sett))
(conj!-set new value))
(defn- conj!-list [list value]
(array/push list value))
2024-05-19 22:04:08 +00:00
(defn- conj-list [list value]
(def new (array/slice list))
(conj!-list new value))
(defn conj! [x value]
(case (ludus/type x)
:list (conj!-list x value)
:set (conj!-set x value)))
2024-05-19 22:04:08 +00:00
(defn conj [x value]
(case (ludus/type x)
:list (conj-list x value)
:set (conj-set x value)
(error (string "cannot conj onto " (show x)))))
(defn disj! [sett value]
(set (sett value) nil)
sett)
2024-05-19 22:04:08 +00:00
(defn disj [sett value]
(def new (merge sett))
2024-05-19 22:13:08 +00:00
(set (new value) nil)
2024-05-19 22:04:08 +00:00
new)
(defn assoc! [dict key value]
(set (dict key) value)
dict)
2024-05-19 22:04:08 +00:00
(defn assoc [dict key value]
(merge dict {key value}))
(defn dissoc! [dict key]
(set (dict key) nil)
dict)
2024-05-19 22:04:08 +00:00
(defn dissoc [dict key]
(def new (merge dict))
(set (new key) nil)
new)
(defn ludus/get [key dict &opt def]
(default def :^nil)
(get dict key def))
(defn rest [indexed]
(array/slice indexed 1))
(defn to_list [x]
(case (ludus/type x)
:list x
:tuple @[;x]
:dict (pairs x)
:set (-> x (dissoc :^type) keys)
@[x]))
(defn showprint [x]
(if (= :string (ludus/type x))
x
(show x)))
2024-06-06 00:16:29 +00:00
(defn print! [args]
(print ;(map showprint args)))
2024-06-04 17:28:20 +00:00
2024-05-19 22:04:08 +00:00
(defn prn [x]
(pp x)
x)
(defn concat [x y & zs]
2024-05-19 22:04:08 +00:00
(case (ludus/type x)
:string (string x y ;zs)
:list (array/concat @[] x y ;zs)
:set (merge x y ;zs)))
2024-05-19 22:04:08 +00:00
2024-06-06 00:16:29 +00:00
(defn unbox [b] (get b :^value))
2024-05-19 22:04:08 +00:00
2024-06-06 00:16:29 +00:00
(defn store! [b x] (set (b :^value) x))
2024-05-19 22:04:08 +00:00
2024-06-07 21:25:46 +00:00
(defn mod [x y]
2024-06-11 21:24:22 +00:00
(% x y))
2024-06-07 21:25:46 +00:00
2024-05-19 22:04:08 +00:00
(def ctx {
"add" +
2024-06-14 18:53:23 +00:00
"and" ludus/and
"assoc!" assoc!
"assoc" assoc
"atan_2" math/atan2
"bool" bool
"ceil" math/ceil
"concat" concat
"conj!" conj!
"conj" conj
"cos" math/cos
"count" length
"dec" dec
"disj!" disj!
"disj" disj
"dissoc!" dissoc!
"dissoc" dissoc
2024-05-19 22:04:08 +00:00
"div" /
2024-06-14 18:53:23 +00:00
"doc" doc
"downcase" string/ascii-lower
"eq?" deep=
"first" first
"floor" math/floor
"get" ludus/get
2024-05-19 22:04:08 +00:00
"gt" >
"gte" >=
2024-06-14 18:53:23 +00:00
"inc" inc
"last" last
2024-05-19 22:04:08 +00:00
"lt" <
"lte" <=
2024-06-14 18:53:23 +00:00
"mod" mod
"mult" *
2024-05-19 22:04:08 +00:00
"not" not
"nth" ludus/get
2024-06-14 18:53:23 +00:00
"or" ludus/or
2024-05-19 22:04:08 +00:00
"pi" math/pi
2024-06-14 18:53:23 +00:00
"print!" print!
"prn" prn
"push" array/push
2024-05-19 22:04:08 +00:00
"random" math/random
"range" range
2024-06-14 18:53:23 +00:00
"rest" rest
"round" math/round
"show" show
"sin" math/sin
2024-06-19 22:23:54 +00:00
"slice" array/slice
2024-06-10 22:26:48 +00:00
"split" string/split
2024-06-14 18:53:23 +00:00
"sqrt" math/sqrt
"store!" store!
"str_slice" string/slice
"stringify" stringify
"sub" -
"tan" math/tan
"to_list" to_list
2024-06-10 22:26:48 +00:00
"trim" string/trim
"triml" string/triml
2024-06-14 18:53:23 +00:00
"trimr" string/trimr
"type" ludus/type
"unbox" unbox
"upcase" string/ascii-upper
})
2024-06-04 20:24:54 +00:00
(def base (let [b @{}]
(each [k v] (pairs ctx)
(set (b (keyword k)) v))
b))
(set (base :^type) :dict)
2024-05-19 22:04:08 +00:00