Skip to content

Commit 3eb00f9

Browse files
committed
ad hoc defs of string-named words
1 parent 0cb3de7 commit 3eb00f9

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

src/purr/client.cljs

+16-13
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@
1717
(let [{:keys [expr error]} (rum/react state)]
1818
[:div
1919
[:h3 "Type some code!"]
20-
[:input {:on-change (fn [evt]
21-
(let [val (get-val evt)
22-
result (try
23-
{:expr (run val)
24-
:error nil}
25-
(catch js/Error e
26-
{:error (.-message e)
27-
:expr nil}))]
28-
(swap! state
29-
(fn [old]
30-
(-> old
31-
(assoc :code val)
32-
(merge result))))))}]
20+
[:textarea
21+
{:auto-focus true
22+
:on-change (fn [evt]
23+
(let [val (get-val evt)
24+
result (try
25+
{:expr (run val)
26+
:error nil}
27+
(catch js/Error e
28+
{:error (.-message e)
29+
:expr nil}))]
30+
(swap! state
31+
(fn [old]
32+
(-> old
33+
(assoc :code val)
34+
(merge result))))))
35+
:rows 5}]
3336
(when expr
3437
[:p "Result: " expr])
3538
(when error

src/purr/parse.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
BLOCK = '[' TERM* ']' ;
1212
NUM = #'\\-?\\d+(\\.\\d+)?' ;
1313
OP = '+' | '/' | '-' | '*' | '>' | '<' | '<=' | '>=' ;
14-
TERM = (WORD | BLOCK | STR | NUM | OP) #' ?' ; "))
14+
TERM = (WORD | BLOCK | STR | NUM | OP) #'\\s?' ; "))
1515

1616
(defn to-num [string]
1717
#?(:clj (read-string string)

src/purr/run.cljc

+22-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
stack])
1414

1515
(def words
16+
"Map of word names to functions that return:
17+
[new-expressions
18+
new-stack
19+
new-library]"
1620
{"+" (partial binary +)
1721
"-" (partial binary -)
1822
"*" (partial binary *)
@@ -22,6 +26,13 @@
2226
"<=" (partial binary <=)
2327
">=" (partial binary >=)
2428
"!=" (partial binary not=)
29+
"def" (fn [[fn-name body & rest]]
30+
[[]
31+
rest
32+
{fn-name
33+
(if (coll? body)
34+
body
35+
[body])}])
2536
"dup" (fn [stack] [[(first stack)] stack])
2637
"dup2" (fn [stack] [(reverse (take 2 stack)) stack])
2738
"drop" (fn [stack] [[] (rest stack)])
@@ -50,21 +61,26 @@
5061
(defn run-word [word lib stack]
5162
(let [task (get lib word)]
5263
(if task
53-
(task stack)
64+
(if (fn? task)
65+
(task stack)
66+
[task stack])
5467
(let [error (str "word not found: " word)]
5568
#?(:clj (throw (Exception. error))
5669
:cljs (throw (js/Error. error)))))))
5770

58-
(defn exec [stack-0 lib exprs-0]
59-
(loop [stack stack-0 exprs exprs-0]
71+
(defn exec [stack-0 lib-0 exprs-0]
72+
(loop [stack stack-0 exprs exprs-0 lib lib-0]
6073
(let [expr (first exprs)]
6174
(cond
6275
(nil? expr) stack
6376
(symbol? expr)
64-
(let [[newexprs newstack] (run-word (str expr) lib stack)]
65-
(recur newstack (concat newexprs (rest exprs))))
77+
(let [[newexprs newstack new-lib] (run-word (str expr) lib stack)]
78+
(recur
79+
newstack
80+
(concat newexprs (rest exprs))
81+
(merge lib new-lib)))
6682
(or (= expr true) (= expr false) (number? expr) (string? expr) (char? expr) (coll? expr))
67-
(recur (cons expr stack) (rest exprs))
83+
(recur (cons expr stack) (rest exprs) lib)
6884
true
6985
(let [error (str "unrecognized: "
7086
expr " of " (type expr))]

test/purr/core_test.clj

+6
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@
5757
(deftest fibonacci
5858
(is (= (run "1 1 [dup2 +] 5 repeat") "1 1 2 3 5 8 13")))
5959

60+
(deftest def-word-alias
61+
(is (= (run "1 \"one\" def one") "1")))
62+
63+
(deftest def-words
64+
(is (= (run "[1 +] \"inc\" def 2 inc") "3")))
65+
6066
(run-tests 'purr.core-test)

test/purr/parse_test.clj

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
(deftest block
1919
(is (= (parse "1 [2 3]") [1 [2 3]])))
2020

21+
(deftest newlines
22+
(is (= (parse "1\n2\n+") [1 2 '+])))
23+
2124
(run-tests 'purr.parse-test)

0 commit comments

Comments
 (0)