Verified Commit b513da9d authored by Vladimir Sedach's avatar Vladimir Sedach
Browse files

Fix: (lambda () "str") to return "str", not treat "str" as docstring

parent 116019cf
......@@ -71,19 +71,14 @@ Syntax of key spec:
"Parses a function or block body, which may or may not include a
docstring. Returns 2 or 3 values: a docstring (if allowed for), a list
of (declare ...) forms, and the remaining body."
(flet ((values* (docstring declarations body)
(if allow-docstring
(values docstring declarations body)
(values declarations body))))
(loop for forms on body for (form) = forms
with docstring = (not allow-docstring)
if (and (stringp form) (not docstring))
do (setf docstring form)
else if (and (consp form) (eq (first form) 'declare))
collect form into declarations
return (values* docstring declarations forms)
finally (return (values* docstring declarations nil)))))
(let (docstring declarations)
(loop while
(cond ((and (consp (car body)) (eq (caar body) 'declare))
(push (pop body) declarations))
((and allow-docstring (not docstring)
(stringp (car body)) (cdr body))
(setf docstring (pop body)))))
(values body declarations docstring)))
(defun parse-extended-function (lambda-list body)
"The lambda list is transformed as follows:
......@@ -141,12 +136,12 @@ of (declare ...) forms, and the remaining body."
`(var ,rest
((@ Array prototype slice call)
arguments ,(length effective-args))))))
(multiple-value-bind (docstring declarations executable-body)
(multiple-value-bind (fun-body declarations docstring)
(parse-body body :allow-docstring t)
(values effective-args
(append declarations
opt-forms key-forms (awhen rest-form (list it))
......@@ -384,12 +384,12 @@ lambda-list::=
,@(when result? (list result))))
(defpsmacro do (decls (end-test &optional (result nil result?)) &body body)
(multiple-value-bind (declarations executable-body) (parse-body body)
(multiple-value-bind (do-body declarations) (parse-body body)
`(block nil
(let ,(do-make-iteration-bindings decls)
(for () ((not ,end-test)) ()
,(do-make-iter-psteps decls))
,@(when result? (list result))))))
......@@ -475,10 +475,10 @@ lambda-list::=
(defpsmacro destructuring-bind (bindings expr &body body)
(setf bindings (dot->rest bindings))
(multiple-value-bind (declarations executable-body) (parse-body body)
(multiple-value-bind (body1 declarations) (parse-body body)
(let* ((arr (if (hoist-expr? bindings expr) (ps-gensym '_db) expr))
(bound (destructuring-wrap arr 0 bindings declarations
(cons 'progn executable-body))))
(cons 'progn body1))))
(cond ((eq arr expr) bound)
(t `(let ((,arr ,expr)) ,bound))))))
......@@ -522,10 +522,10 @@ lambda-list::=
;;; misc
(defpsmacro let* (bindings &body body)
(multiple-value-bind (declarations executive-body) (parse-body body)
(multiple-value-bind (let-body declarations) (parse-body body)
(loop for binding in (cons nil (reverse bindings))
for var = (if (symbolp binding) binding (car binding))
for body = executive-body
for body = let-body
then `((let (,binding)
(declare ,@(pop-declarations-for-var var declarations))
......@@ -436,7 +436,7 @@ Parenscript now implements implicit return, update your code! Things like (lambd
(defmacro with-declaration-effects ((var block) &body body)
(with-ps-gensyms (decls)
`(multiple-value-bind (,decls ,var) (parse-body ,block)
`(multiple-value-bind (,var ,decls) (parse-body ,block)
(let ((*special-variables*
(loop for decl in ,decls nconc
......@@ -800,3 +800,13 @@
'(1 2 3))
'(1 (2 3)))
(test-js-eval defun-not-a-docstring
(defun foo ()
(test-js-eval lambda-not-a-docstring
((lambda () "bar"))
......@@ -4054,6 +4054,24 @@ for (var i = 0; i < 5; i += 1) {
(or (not foo) (not (not foo)) (not (not (not foo))))
"!foo || foo || !foo;")
(test-ps-js empty-defun-docstring-declare
(defun foo (x)
(declare (ignore x)))
"/** docstring */
function foo(x) {
return null;
(test-ps-js defun-docstring-string
(defun foo (x)
"/** docstring */
function foo(x) {
return 'abc';
;;; broken
;; (test-ps-js let-defun-toplevel
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment