Compare commits

...

8 Commits

Author SHA1 Message Date
Scott Richmond
d416511b48 remove repl cruft 2024-07-14 14:42:31 -04:00
Scott Richmond
a6c899a85f Pretty-patterns shows words at ends of splatterns 2024-07-14 14:41:53 -04:00
Scott Richmond
9ddb43a30f called keywords work in pipelines 2024-07-14 14:28:09 -04:00
Scott Richmond
9e50f0cbdf Fix typo 2024-07-14 14:18:10 -04:00
Scott Richmond
2f03bbb12f mismatched arity -> wrong number of arguments 2024-07-14 13:53:32 -04:00
Scott Richmond
8cf84e63d3 add char functions to doc 2024-07-14 13:49:49 -04:00
Scott Richmond
5c32d32f24 add chars to prelude 2024-07-14 13:48:47 -04:00
Scott Richmond
32b42e0242 ignore .zig-cache 2024-07-13 18:30:45 -04:00
8 changed files with 44 additions and 15 deletions

1
.gitignore vendored
View File

@ -33,3 +33,4 @@ target/repl-port
.repl-buffer.janet .repl-buffer.janet
.env .env
src/jpm_tree src/jpm_tree
.zig-cache

View File

@ -386,6 +386,19 @@ fn downcase {
(str as :string) -> base :downcase (str) (str as :string) -> base :downcase (str)
} }
fn chars {
"Takes a string and returns its characters as a list. Works only for strings with only ascii characters. Panics on any non-ascii characters."
(str as :string) -> match base :chars (str) with {
(:ok, chrs) -> chrs
(:err, msg) -> panic! msg
}
}
fn chars/safe {
"Takes a string and returns its characters as a list, wrapped in a result tuple. Works only for strings with only ascii characters. Returns an error tuple on any non-ascii characters."
(str as :string) -> base :chars (str)
}
fn ws? { fn ws? {
"Tells if a string is a whitespace character." "Tells if a string is a whitespace character."
(" ") -> true (" ") -> true
@ -1386,6 +1399,7 @@ pkg Prelude {
box? & boxes box? & boxes
butlast & lists strings tuples butlast & lists strings tuples
ceil & math ceil & math
chars & strings
clear! & turtles clear! & turtles
coll? & dicts lists sets tuples coll? & dicts lists sets tuples
colors & turtles colors & turtles

View File

@ -129,7 +129,7 @@
:typed (string (show-patt (get-in x [:data 1])) " as " (show-patt (get-in x [:data 0]))) :typed (string (show-patt (get-in x [:data 1])) " as " (show-patt (get-in x [:data 0])))
:interpolated (get-in x [:token :lexeme]) :interpolated (get-in x [:token :lexeme])
:string (get-in x [:token :lexeme]) :string (get-in x [:token :lexeme])
:splat (string "..." (when (x :splatted) (show-patt (x :splatted)))) :splat (string "..." (when (x :data) (show-patt (x :data))))
(error (string "cannot show pattern of unknown type " (x :type))))) (error (string "cannot show pattern of unknown type " (x :type)))))
(defn pretty-patterns [fnn] (defn pretty-patterns [fnn]
@ -235,6 +235,19 @@
(defn mod [x y] (defn mod [x y]
(% x y)) (% x y))
(defn- byte->ascii [c i]
(if (< c 128)
(string/from-bytes c)
(error (string "non-ASCII character at index" i))))
(defn chars [str]
(def out @[])
(try
(for i 0 (length str)
(array/push out (byte->ascii (str i) i)))
([e] (break [:err e])))
[:ok out])
(def ctx { (def ctx {
"add" + "add" +
"and" ludus/and "and" ludus/and
@ -243,6 +256,7 @@
"atan_2" math/atan2 "atan_2" math/atan2
"bool" bool "bool" bool
"ceil" math/ceil "ceil" math/ceil
"chars" chars
"concat" concat "concat" concat
"conj!" conj! "conj!" conj!
"conj" conj "conj" conj
@ -298,10 +312,9 @@
"upcase" string/ascii-upper "upcase" string/ascii-upper
}) })
(def base (let [b @{}] (def base (let [b @{:^type :dict}]
(each [k v] (pairs ctx) (each [k v] (pairs ctx)
(set (b (keyword k)) v)) (set (b (keyword k)) v))
b)) b))
(set (base :^type) :dict)

View File

@ -28,7 +28,7 @@
"lists" ["any?" "append" "at" "butlast" "coll?" "concat" "count" "each!" "empty?" "filter" "first" "fold" "join" "keep" "last" "list" "list?" "map" "ordered?" "random" "range" "rest" "second" "sentence" "slice"] "lists" ["any?" "append" "at" "butlast" "coll?" "concat" "count" "each!" "empty?" "filter" "first" "fold" "join" "keep" "last" "list" "list?" "map" "ordered?" "random" "range" "rest" "second" "sentence" "slice"]
"sets" ["any?" "append" "coll?" "concat" "contains?" "count" "empty?" "omit" "random" "set" "set?"] "sets" ["any?" "append" "coll?" "concat" "contains?" "count" "empty?" "omit" "random" "set" "set?"]
"tuples" ["any?" "at" "coll?" "count" "empty?" "first" "last" "ordered?" "rest" "second" "tuple?"] "tuples" ["any?" "at" "coll?" "count" "empty?" "first" "last" "ordered?" "rest" "second" "tuple?"]
"strings" ["any?" "concat" "count" "downcase" "empty?" "join" "sentence" "show" "slice" "split" "string" "string?" "strip" "trim" "upcase" "words"] "strings" ["any?" "chars" "chars/safe" "concat" "count" "downcase" "empty?" "join" "sentence" "show" "slice" "split" "string" "string?" "strip" "trim" "upcase" "words"]
"types and values" ["assoc?" "bool?" "box?" "coll?" "dict?" "eq?" "fn?" "keyword?" "list?" "neq?" "nil?" "number?" "ordered?" "set?" "show" "some" "some?" "string?" "tuple?" "type"] "types and values" ["assoc?" "bool?" "box?" "coll?" "dict?" "eq?" "fn?" "keyword?" "list?" "neq?" "nil?" "number?" "ordered?" "set?" "show" "some" "some?" "string?" "tuple?" "type"]
"boxes and state" ["box?" "unbox" "store!" "update!"] "boxes and state" ["box?" "unbox" "store!" "update!"]
"results" ["err" "err?" "ok" "ok?" "unwrap!" "unwrap_or"] "results" ["err" "err?" "ok" "ok?" "unwrap!" "unwrap_or"]

View File

@ -460,6 +460,7 @@
[:function :tuple] (call-fn root-ast prev curr) [:function :tuple] (call-fn root-ast prev curr)
# [:applied :tuple] (call-partial root-ast prev curr) # [:applied :tuple] (call-partial root-ast prev curr)
[:keyword :args] (get (first curr) prev :^nil) [:keyword :args] (get (first curr) prev :^nil)
[:keyword :tuple] (get (first curr) prev :^nil)
[:dict :keyword] (get prev curr :^nil) [:dict :keyword] (get prev curr :^nil)
[:nil :keyword] :^nil [:nil :keyword] :^nil
[:pkg :keyword] (get prev curr :^nil) [:pkg :keyword] (get prev curr :^nil)
@ -489,9 +490,9 @@
(def last-term (last terms)) (def last-term (last terms))
(for i 1 (-> terms length dec) (for i 1 (-> terms length dec)
(def curr (interpret (terms i) ctx)) (def curr (interpret (terms i) ctx))
(set prev (call-fn (first terms) curr [prev]))) (set prev (apply-synth-term (first terms) curr [prev])))
(def last-fn (interpret last-term ctx)) (def last-fn (interpret last-term ctx))
(call-fn (first terms) last-fn [prev])) (apply-synth-term (first terms) last-fn [prev]))
(defn- pkg [ast ctx] (defn- pkg [ast ctx]
(def members (ast :data)) (def members (ast :data))

View File

@ -53,21 +53,21 @@
(comment (comment
# (do # (do
(def start (os/clock)) # (def start (os/clock))
(def source ` (def source `
at ("aéc", 3) doc! (add)
`) `)
(def out (-> source (def out (-> source
ludus ludus
j/decode j/decode
)) ))
(def end (os/clock)) # (def end (os/clock))
(setdyn :out stdout) (setdyn :out stdout)
(pp out) (pp out)
(def console (out "console")) (def console (out "console"))
(print console) (print console)
(def result (out "result")) (def result (out "result"))
(print result) (print result)
(print (- end start)) # (print (- end start))
) )

View File

@ -1117,10 +1117,10 @@
# (do # (do
(comment (comment
(def source ` (def source `
let foo = :bar [...x]
`) `)
(def scanned (s/scan source)) (def scanned (s/scan source))
# (print "\n***NEW PARSE***\n") # (print "\n***NEW PARSE***\n")
(def a-parser (new-parser scanned)) (def a-parser (new-parser scanned))
(def parsed (lett a-parser)) (try (def parsed (pattern a-parser)) ([e] (pp e)))
) )

View File

@ -435,12 +435,12 @@ Deferred until a later iteration of Ludus:
(def rest-arities (keys (arities :rest))) (def rest-arities (keys (arities :rest)))
(when (empty? rest-arities) (when (empty? rest-arities)
(array/push (validator :errors) (array/push (validator :errors)
{:node ast :msg "mismatched arity"}) {:node ast :msg "wrong number of arguments"})
(break validator)) (break validator))
(def rest-min (min ;rest-arities)) (def rest-min (min ;rest-arities))
(when (< num-args rest-min) (when (< num-args rest-min)
(array/push (validator :errors) (array/push (validator :errors)
{:node ast :msg "mismatched arity"})) {:node ast :msg "wrong number of arguments"}))
validator) validator)
(defn- kw-root [validator] (defn- kw-root [validator]