Bugfixes.

This commit is contained in:
Scott Richmond 2023-06-01 17:27:55 -06:00
parent 7c30b6259b
commit 5c8ba725a9
5 changed files with 87 additions and 11 deletions

View File

@ -1,3 +1,47 @@
import "foo.ld" as foo
fn map {
(f) -> fn mapper (xs) -> map (f, xs)
(f, xs) -> {
let n = count (xs)
loop (0, []) with (i, ys) -> if eq (i, n)
then ys
else recur (inc (i), conj (ys, f (nth (i, xs))))
}
}
print ("Hello, world!", foo)
fn reduce {
(f) -> fn reducer {
(xs) -> reduce (f, xs)
(xs, init) -> reduce (f, xs, init)
}
(f, xs) -> {
let first_x = first (xs)
let more_xs = rest (xs)
reduce (f, more_xs, first_x)
}
(f, xs, init) -> {
let n = count (xs)
loop (0, init) with (i, acc) -> if eq (i, n)
then acc
else {
let curr = nth (i, xs)
let next = f (acc, curr)
recur (inc (i), next)
}
}
}
fn filter {
(f) -> fn filterer (xs) -> filter (f, xs)
(f, xs) -> {
let n = count (xs)
loop (0, []) with (i, ys) -> when {
eq (i, n) -> ys
f (nth (i, xs)) -> recur (inc (i), conj (ys, nth (i, xs)))
else -> recur (inc (i), ys)
}
}
}
let xs = [1, 2, 3]
filter (gte (_, 2) ,xs)

View File

@ -23,7 +23,7 @@
(if (p/fail? parsed)
(do
(println "I found some parsing errors!")
(println p/err-msg parsed)
(println (p/err-msg parsed))
(System/exit 66))
(let [interpreted (interpreter/interpret source file parsed)]
(println (show/show interpreted))

View File

@ -5,13 +5,13 @@
[ludus.scanner :as s]))
(def source
"when x is { true -> true }
"(1 2)
"
)
(def tokens (-> source s/scan :tokens))
(def result (p/apply-parser g/when-expr tokens))
(def result (p/apply-parser g/script tokens))
(-> result :data)
@ -24,7 +24,7 @@
(-> node
(report)
(dissoc
:status
;:status
:remaining
:token)
(update :data #(into [] (map clean) %)))

View File

@ -37,6 +37,22 @@
::data/type ::data/clj
:body /})
(def gt {:name "gt"
::data/type ::data/clj
:body >})
(def gte {:name "gte"
::data/type ::data/clj
:body >=})
(def lt {:name "lt"
::data/type ::data/clj
:body <})
(def lte {:name "lte"
::data/type ::data/clj
:body <=})
(def inc- {:name "inc"
::data/type ::data/clj
:body inc})
@ -114,10 +130,17 @@
(def nth- {:name "nth"
::data/type ::data/clj
:body (fn [i, xs]
(if (contains? xs (inc i))
(nth xs (inc i))
nil))})
:body (fn
([i, xs]
(cond
(> 0 i) nil
(contains? xs (inc i)) (nth xs (inc i))
:else nil))
([i, xs, default]
(cond
(> 0 i) default
(contains? xs (inc i)) (nth xs (inc i))
:else default)))})
(defn get-type [value]
(let [t (type value)]
@ -168,6 +191,10 @@
(println "Args: " fn-args)
(apply called fn-args)))})
(def count- {:name "count"
::data/type ::data/clj
:body (fn [xs] (dec (count xs)))})
(def prelude {
"id" id
"eq" eq
@ -176,6 +203,10 @@
"sub" sub
"mult" mult
"div" div
"gt" gt
"gte" gte
"lt" lt
"lte" lte
"inc" inc-
"dec" dec-
"not" not
@ -193,4 +224,5 @@
"first" first-
"rest" rest-
"nth" nth-
"count" count-
})

View File

@ -25,7 +25,7 @@
"ref" :ref ;; impl
"then" :then ;; impl
"true" :true ;; impl -> literal word
;"with" :with ;; impl
"with" :with ;; impl
;; actor model/concurrency
"receive" :receive