Adding string processing

- 'character' type, only contructible by decomposing strings
- 'boom' builtin function ;) to create a list of characters from a string
- string library with basic manipulation functionalities
- slightly improved Makefile
This commit is contained in:
teo3300
2024-06-22 23:03:25 +09:00
parent 5996af1aea
commit 287b96ea7d
7 changed files with 73 additions and 7 deletions

40
libs/string.mal Normal file
View File

@ -0,0 +1,40 @@
(def! char (fn* [l]
(car (boom l))))
(def! char? (fn* [a]
(= (type a) :char)))
(def! string? (fn* [a]
(= (type a) :string)))
(def! strc (fn* [l]
(def! strc-r (fn* [l s]
(if (empty? l)
s
(strc-r (cdr l) (str s (car l))))))
(strc-r l "")))
(def! split (fn* [s c]
"Split the string at every occurrence of character sc"
(if (and (string? s)
(char? c))
(def! split-r (fn* [l t r]
(if (empty? l)
(cons t r)
(do (def! cc (car l))
(if (= cc c)
(split-r (cdr l) "" (cons t r))
(split-r (cdr l) (str t cc) r))))))
(raise "split: accepts a string and a char as arguments"))
(reverse (split-r (boom s) "" '()))))
(def! join (fn* [l s]
"Join element of list l to a stiring, using s as separator"
(def! join-r (fn* [l t]
(if (empty? l)
t
(join-r (cdr l) (str t s (car l))))))
(join-r (cdr l) (car l))))
(def! chsub (fn* [s c1 c2]
(strc (map-if (fn* [x] (= x c1)) (fn* [x] c2) (boom s)))))