From 5c8ba725a9107d73ae873b0391de720f08dd1966 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 1 Jun 2023 17:27:55 -0600 Subject: [PATCH] Bugfixes. --- sandbox.ld | 48 +++++++++++++++++++++++++++++++++-- src/ludus/core.clj | 2 +- src/ludus/interpreter_new.clj | 6 ++--- src/ludus/prelude.clj | 40 ++++++++++++++++++++++++++--- src/ludus/scanner.clj | 2 +- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/sandbox.ld b/sandbox.ld index b168e9b..23bce84 100644 --- a/sandbox.ld +++ b/sandbox.ld @@ -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/src/ludus/core.clj b/src/ludus/core.clj index dc65914..e1296e3 100644 --- a/src/ludus/core.clj +++ b/src/ludus/core.clj @@ -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)) diff --git a/src/ludus/interpreter_new.clj b/src/ludus/interpreter_new.clj index 2a5815e..cba232c 100644 --- a/src/ludus/interpreter_new.clj +++ b/src/ludus/interpreter_new.clj @@ -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) %))) diff --git a/src/ludus/prelude.clj b/src/ludus/prelude.clj index d4f8337..673e3cf 100644 --- a/src/ludus/prelude.clj +++ b/src/ludus/prelude.clj @@ -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- }) \ No newline at end of file diff --git a/src/ludus/scanner.clj b/src/ludus/scanner.clj index 368da41..bc2ac78 100644 --- a/src/ludus/scanner.clj +++ b/src/ludus/scanner.clj @@ -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