Merge pull request #6 from Limvot/md5_hash_func
Add 32 bit int to hex string and leftrotate impls. Also some string functions
This commit is contained in:
111
find.kp
Normal file
111
find.kp
Normal file
@@ -0,0 +1,111 @@
|
||||
|
||||
((wrap (vau root_env (quote)
|
||||
((wrap (vau (let1)
|
||||
(let1 lambda (vau se (p b1) (wrap (eval (array vau p b1) se)))
|
||||
(let1 current-env (vau de () de)
|
||||
(let1 cons (lambda (h t) (concat (array h) t))
|
||||
(let1 Y (lambda (f3)
|
||||
((lambda (x1) (x1 x1))
|
||||
(lambda (x2) (f3 (lambda (& y) (lapply (x2 x2) y))))))
|
||||
(let1 vY (lambda (f)
|
||||
((lambda (x3) (x3 x3))
|
||||
(lambda (x4) (f (vau de1 (& y) (vapply (x4 x4) y de1))))))
|
||||
(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2)
|
||||
true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2)))))
|
||||
(let (
|
||||
rec-lambda (vau se (n p b) (eval (array Y (array lambda (array n) (array lambda p b))) se))
|
||||
cdr (lambda (x) (slice x 1 -1))
|
||||
if (vau de (con than & else) (eval (array cond con than
|
||||
true (cond (> (len else) 0) (idx else 0)
|
||||
true false)) de))
|
||||
and (let (macro_helper (rec-lambda recurse (bs i) (cond (= i (len bs)) true
|
||||
(= (+ 1 i) (len bs)) (idx bs i)
|
||||
true (array let (array 'tmp (idx bs i)) (array if 'tmp (recurse bs (+ i 1)) 'tmp)))))
|
||||
(vau se (& bs) (eval (macro_helper bs 0) se)))
|
||||
|
||||
; if len(s1) > 0 && len(sub) > 0:
|
||||
; char1 = s1[0]
|
||||
; rest1 = s1[1:]
|
||||
; char2 = sub[0]
|
||||
; rest2 = sub[1:]
|
||||
; if char1 == char2:
|
||||
; return recurse(rest1, rest2)
|
||||
; else
|
||||
; return false
|
||||
; else if len(s1) > 0 && len(sub) == 0:
|
||||
; return true
|
||||
; else if len(s1) == 0:
|
||||
; return false
|
||||
; else:
|
||||
; return false
|
||||
;
|
||||
;
|
||||
compare_substr (rec-lambda cmp (str1 str2) (let (
|
||||
str1_len (len str1)
|
||||
str2_len (len str2))
|
||||
; len(s1) > 0 && len(sub) > 0
|
||||
(cond (and (> str1_len 0) (> str2_len 0))
|
||||
(let (
|
||||
char1 (idx str1 0)
|
||||
rest1 (cdr str1)
|
||||
char2 (idx str2 0)
|
||||
rest2 (cdr str2)
|
||||
same_char (= char1 char2)
|
||||
; if char1 == char2: return recurse(str, sub)
|
||||
; else: return false
|
||||
) (cond same_char (cmp rest1 rest2)
|
||||
true false
|
||||
))
|
||||
; len(s1) > 0 && len(sub) == 0
|
||||
(and (> str1_len 0) (= str2_len 0)) true
|
||||
; else
|
||||
true false
|
||||
)
|
||||
))
|
||||
; i = index of current match start, passed in
|
||||
; len1 = len(str)
|
||||
; len2 = len(sub)
|
||||
;
|
||||
; if len2 > len1:
|
||||
; return -1
|
||||
; else if len1 > 0 && len2 == 0:
|
||||
; // No more substr, we matched?
|
||||
; return i
|
||||
; else if len1 > 0 && len2 > 0:
|
||||
; if compare(str, sub):
|
||||
; return i
|
||||
; else:
|
||||
; rest1 = str[1:]
|
||||
; return recurse(rest1, sub, i+1)
|
||||
; else
|
||||
; return -1
|
||||
;
|
||||
_find (rec-lambda _find (str sub i) (let (
|
||||
len1 (len str)
|
||||
len2 (len sub)
|
||||
) (cond (> len2 len1) -1
|
||||
(= len2 0) i
|
||||
(and (> len1 0) (> len2 0))
|
||||
(cond (compare_substr str sub) i
|
||||
true (_find (cdr str) sub (+ i 1)))
|
||||
true -1
|
||||
)))
|
||||
; find the index of a substr in a str
|
||||
; check if a substr is in a str
|
||||
find (lambda (str sub) (_find str sub 0))
|
||||
contains(lambda (str sub) (!= (find str sub) -1))
|
||||
|
||||
monad (array 'write 1 "testing find funcs: \n" (vau (written code)
|
||||
(array 'write 1 "find in \"foobar\" the string \"oba\"\n" (vau (written code)
|
||||
(array 'write 1 (concat (str (contains "foobar" "oba")) "\n") (vau (written code)
|
||||
(array 'exit 0)))
|
||||
))
|
||||
))
|
||||
|
||||
) monad)
|
||||
; end of all lets
|
||||
))))))
|
||||
; impl of let1
|
||||
)) (vau de (s v b) (eval (array (array wrap (array vau (array s) b)) v) de)))
|
||||
; impl of quote
|
||||
)) (vau (x5) x5))
|
||||
66
int2hex.kp
Normal file
66
int2hex.kp
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
((wrap (vau root_env (quote)
|
||||
((wrap (vau (let1)
|
||||
(let1 lambda (vau se (p b1) (wrap (eval (array vau p b1) se)))
|
||||
(let1 current-env (vau de () de)
|
||||
(let1 cons (lambda (h t) (concat (array h) t))
|
||||
(let1 Y (lambda (f3)
|
||||
((lambda (x1) (x1 x1))
|
||||
(lambda (x2) (f3 (lambda (& y) (lapply (x2 x2) y))))))
|
||||
(let1 vY (lambda (f)
|
||||
((lambda (x3) (x3 x3))
|
||||
(lambda (x4) (f (vau de1 (& y) (vapply (x4 x4) y de1))))))
|
||||
(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2)
|
||||
true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2)))))
|
||||
(let (
|
||||
; Some helper hex literals
|
||||
xF 15
|
||||
xF0 240
|
||||
xFF 255
|
||||
xFF00 65280
|
||||
xFFFF 65535
|
||||
xFFFF0000 4294901760
|
||||
|
||||
; nibble to hex digit
|
||||
nibble_to_hexstr(lambda (n)
|
||||
(cond (= n 15) "f"
|
||||
(= n 14) "e"
|
||||
(= n 13) "d"
|
||||
(= n 12) "c"
|
||||
(= n 11) "b"
|
||||
(= n 10) "a"
|
||||
true (str n))
|
||||
)
|
||||
; char to hex str
|
||||
char_to_hexstr (lambda (c)
|
||||
(concat
|
||||
(nibble_to_hexstr (>> (band c xF0) 4))
|
||||
(nibble_to_hexstr (band c xF)))
|
||||
)
|
||||
; short to hex str
|
||||
short_to_hexstr (lambda (s)
|
||||
(concat
|
||||
(char_to_hexstr (>> (band s xFF00) 8))
|
||||
(char_to_hexstr (band s xFF)))
|
||||
)
|
||||
; 32 bit int to hex string helper
|
||||
int_to_hexstr (lambda (i)
|
||||
(str
|
||||
(concat
|
||||
(short_to_hexstr (>> (band i xFFFF0000) 16))
|
||||
(short_to_hexstr (band i xFFFF)))
|
||||
)
|
||||
)
|
||||
|
||||
monad (array 'write 1 "enter int to get hex string: " (vau (written code)
|
||||
(array 'read 0 60 (vau (num code)
|
||||
(array 'write 1 (concat (int_to_hexstr (read-string num)) "\n") (vau (written code)
|
||||
(array 'exit 0)
|
||||
))))))
|
||||
) monad)
|
||||
; end of all lets
|
||||
))))))
|
||||
; impl of let1
|
||||
)) (vau de (s v b) (eval (array (array wrap (array vau (array s) b)) v) de)))
|
||||
; impl of quote
|
||||
)) (vau (x5) x5))
|
||||
36
leftrotate32bit.kp
Normal file
36
leftrotate32bit.kp
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
((wrap (vau root_env (quote)
|
||||
((wrap (vau (let1)
|
||||
(let1 lambda (vau se (p b1) (wrap (eval (array vau p b1) se)))
|
||||
(let1 current-env (vau de () de)
|
||||
(let1 cons (lambda (h t) (concat (array h) t))
|
||||
(let1 Y (lambda (f3)
|
||||
((lambda (x1) (x1 x1))
|
||||
(lambda (x2) (f3 (lambda (& y) (lapply (x2 x2) y))))))
|
||||
(let1 vY (lambda (f)
|
||||
((lambda (x3) (x3 x3))
|
||||
(lambda (x4) (f (vau de1 (& y) (vapply (x4 x4) y de1))))))
|
||||
(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2)
|
||||
true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2)))))
|
||||
(let (
|
||||
|
||||
; bitwise left rotate
|
||||
leftrotate (lambda (i shift_amt)
|
||||
; Rotate i left by shift amt
|
||||
(bor (<< i shift_amt) (>> i (- 32 shift_amt)))
|
||||
)
|
||||
|
||||
monad (array 'write 1 "enter int to left rotate: " (vau (written code)
|
||||
(array 'read 0 60 (vau (num code)
|
||||
(array 'write 1 "enter rotate amount: " (vau (written code)
|
||||
(array 'read 0 60 (vau (amount code)
|
||||
(array 'write 1 (concat (str (leftrotate (read-string num) (read-string amount))) "\n") (vau (written code)
|
||||
(array 'exit 0)
|
||||
))))))))))
|
||||
) monad)
|
||||
; end of all lets
|
||||
))))))
|
||||
; impl of let1
|
||||
)) (vau de (s v b) (eval (array (array wrap (array vau (array s) b)) v) de)))
|
||||
; impl of quote
|
||||
)) (vau (x5) x5))
|
||||
Reference in New Issue
Block a user