X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=lispstrack.lisp;h=562f004228962bef1da46536f6d20b03534e825f;hb=7a3b48bc54c5540a963c6aca4c974ca90c41bfca;hp=6150d86a7638c3214e617ef7c873c769303204d0;hpb=d46de145191d4d4bc1b4759a6d49c0daf6e57c33;p=jscl.git diff --git a/lispstrack.lisp b/lispstrack.lisp index 6150d86..562f004 100644 --- a/lispstrack.lisp +++ b/lispstrack.lisp @@ -273,14 +273,13 @@ (t (nth (1- n) (cdr list))))) (defun last (x) - (if (null (cdr x)) - x - (last (cdr x)))) + (if (consp (cdr x)) + (last (cdr x)) + x)) (defun butlast (x) - (if (null (cdr x)) - nil - (cons (car x) (butlast (cdr x))))) + (and (consp (cdr x)) + (cons (car x) (butlast (cdr x))))) (defun member (x list) (cond @@ -323,6 +322,15 @@ (- x #\0) nil)) + (defun subseq (seq a &optional b) + (cond + ((stringp seq) + (if b + (slice seq a b) + (slice seq a))) + (t + (error "Unsupported argument.")))) + (defun parse-integer (string) (let ((value 0) (index 0) @@ -982,6 +990,17 @@ (define-compilation string-length (x) (concat "(" (ls-compile x env fenv) ").length")) +(define-compilation slice (string a &optional b) + (concat "(function(){" *newline* + "var str = " (ls-compile string env fenv) ";" *newline* + "var a = " (ls-compile a env fenv) ";" *newline* + "var b;" *newline* + (if b + (concat "b = " (ls-compile b env fenv) ";" *newline*) + "") + "return str.slice(a,b);" *newline* + "})()")) + (define-compilation char (string index) (concat "(" (ls-compile string env fenv)