-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.slosh
More file actions
170 lines (140 loc) · 4.59 KB
/
init.slosh
File metadata and controls
170 lines (140 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
(def nil? (fn (v) (eq? (type v) :Nil)))
(def pair? (fn (v) (eq? (type v) :Pair)))
(def string? (fn (v) (eq? (type v) :String)))
(def symbol? (fn (v) (eq? (type v) :Symbol)))
(def vec? (fn (v) (eq? (type v) :Vector)))
(def list? (fn (v) (if (or (nil? v)(pair? v))(if (nil? (cdr v)) #t (recur (cdr v))) #f)))
(def callable? (fn (v) (let (t (type v))(or (eq? t :Lambda)
(eq? t :Continuation)
(eq? t :Special)
(eq? t :Builtin)
(eq? t :Map)
(eq? t :Vactor)
(eq? t :Pair)))))
#%
Usage: (defmacro name doc_string? argument_list body)
Create a macro and bind it to a symbol in the current scope.
Section: core
Example:
(defmacro test-mac (x) (let (y (+ (ref (ref x)) 1)) `(set! ,x ,y)))
(def test-mac-x 2)
(test-mac test-mac-x)
(test::assert-equal 3 test-mac-x)
(defmacro test-mac (x) `(set! ,x 15))
(test-mac test-mac-x)
(test::assert-equal 15 test-mac-x)
%#
(def defmacro
(macro (name args & body)
`(def ~name (macro ~args ~@body))))
(defmacro get-error (& body)
`(let (old-error (on-error nil))
(defer (on-error old-error))
(call/cc (fn (k)
(on-error (fn (key val) (k (cons key val))))
(cons :ok (do ~@body))))))
(defmacro block (& body)
`(call/cc (fn (return-from) ~@body)))
#%
Define a named function in the current namespace.
Section: core
Example:
(defn defn-test (x y) (+ x y))
(test::assert-equal 5 (defn-test 2 3))
(defn defn-test (x y) (set! x (* x 2))(+ x y))
(test::assert-equal 7 (defn-test 2 3))
(defn defn-test (x y))
(test::assert-false (defn-test 2 3))
(defn defn-test (x y) #t)
(test::assert-true (defn-test 2 3))
%#
(defmacro defn
(name args & body)
`(def ~name (fn ~args ~@body)))
#%
Binds bindings to parameters in body. Use recur with desired bindings for
subsequent iteration.
Within the loop the lambda 'break' will end the loop, break can take an option
argument that is what the loop produces (nil if no argument).
Section: core
Example:
(def tot 0)
(loop (idx) (3) (do
(set! tot (+ tot 1))
(if (> idx 1) (recur (- idx 1)))))
(assert-equal 3 tot)
(def tot 0)
(loop (idx) (0)
(set! tot (+ tot 1))
(if (= idx 2) (break))
(recur (+ idx 1)))
(assert-equal 3 tot)
(assert-equal 11 (loop (idx) (0)
(if (= idx 2) (break 11))
(recur (+ idx 1))))
(assert-false (loop (idx) (0)
(if (= idx 2) (break))
(recur (+ idx 1))))
(assert-error (loop (idx) (0)
(if (= idx 2) (break 1 3))
(recur (+ idx 1))))
%#
(defmacro loop
(params bindings & body)
`(call/cc (fn (break) ((fn ~params ~@body) ~@bindings))))
(defmacro doc (sym) `(prn (eval (get-prop '~sym :doc-string))))
#%
Evaluate body a number of times equal to times' numerical value.
Section: core
Example:
(def i 0)
(dotimes 11 (set! i (+ 1 i)))
(assert-equal 11 i)
%#
(defmacro dotimes
(times body)
((fn (idx-name)
`(if (> ~times 0)
(loop (~idx-name) (~times) (do
(~@body)
(if (> ~idx-name 1) (recur (- ~idx-name 1)))))))(gensym)))
#%
Evaluate body a number of times equal to times' numnrical value. Includes an
incrementing reference binding, idx-bind, accesible in body.
Section: core
Example:
(def i 0)
(def i-tot 0)
(dotimes-i idx 11 (do (set! i-tot (+ idx i-tot))(set! i (+ 1 i))))
(assert-equal 11 i)
(assert-equal 55 i-tot)
%#
(defmacro dotimes-i
(idx-bind times & body)
`(let (~idx-bind 0)
(while (< ~idx-bind ~times)
~@body
(inc! ~idx-bind))))
(defn parse-git-branch () (let (branch ($sh "git rev-parse --abbrev-ref HEAD 2>/dev/null"))
(if (equal? branch "")
(str "")
(str "(" branch ")"))))
(defn get-pwd ()
(str-replace (env 'PWD) (env 'HOME) "~"))
(defn set-prompt-tail (last-status)
(let (
debug (if (str-contains (version) "debug") "[DEBUG]" "")
status (if (= last-status 0) "" (str "\x1b[31m(" last-status ")\x1b[39m"))
)
(if (= *euid* 0)
(str "\x1b[31m" status "\n\x1b[31m" debug "λ #\x1b[39m ")
(str "\x1b[32m" status "\n\x1b[32m" debug "λ >\x1b[39m "))))
(def *ns* "SLOSH")
(defn __prompt ()
(str "\x1b[32m[" *ns* "]:" (env "HOST") ":\x1b[34m" (str-trim! (get-pwd)) "/\x1b[37m" (parse-git-branch) (set-prompt-tail *last-status*)))
(sh "alias ls='/bin/ls --color -F'")
(sh "alias ll='/bin/ls --color -Falh'")
(sh "alias vi=nvim")
(sh "alias vim=nvim")
(sh "export PATH=/bin:/usr/local/bin:~/bin:~/.cargo/bin")
(sh "export LC_ALL=en_US.UTF-8")