Skip to content
This repository was archived by the owner on Jun 29, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ CL environment)."

(defvar this-in-lambda-wrapped-form? nil)

(defvar *ps-gensym-counter* 0)

(defun lambda-wrap (form)
(let ((this-in-lambda-wrapped-form? :query)
(*ps-gensym-counter* *ps-gensym-counter*))
Expand Down Expand Up @@ -298,8 +300,6 @@ form, FORM, returns the new value for *compilation-level*."
(let ((compile-expression? t))
(ps-compile form)))

(defvar *ps-gensym-counter* 0)

(defun ps-gensym (&optional (prefix-or-counter "_JS"))
(assert (or (stringp prefix-or-counter) (integerp prefix-or-counter)))
(let ((prefix (if (stringp prefix-or-counter) prefix-or-counter "_JS"))
Expand Down
3 changes: 3 additions & 0 deletions src/js-ir-package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@
#:funcall
#:escape
#:regex

#:es-class
#:=>
))
41 changes: 41 additions & 0 deletions src/non-cl.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,44 @@

(define-ps-symbol-macro false ps-js:false)
(defvar false nil)

(define-expression-operator => (lambda-list &rest body)
(multiple-value-bind (effective-args effective-body)
(parse-extended-function lambda-list body)
`(ps-js:=> ,effective-args
,(let ((*function-block-names* ()))
(compile-function-body effective-args effective-body)))))

(define-statement-operator es-class (name (&optional parent) &body body)
(let (defs)
(flet ((collect (form)
(push form defs))
(parse (sym form)
(destructuring-bind (name (&rest params) &body body)
form
(multiple-value-bind (effective-args body-block docstring)
(compile-named-function-body name params body)
(list* sym name effective-args docstring body-block)))))
(dolist (form body)
(ecase (first form)
(static
(dolist (form (rest form))
(ecase (first form)
(setf
(pop form)
(while form
(collect `(static-assign ,(pop form)
,(compile-expression (pop form))))))
((defun)
(collect (parse 'static-function
(rest form)))))))
(setf
(pop form)
(while form
(collect `(setf ,(pop form)
,(compile-expression (pop form))))))
((defun)
(collect (parse 'function
(rest form))))))
`(ps-js:es-class ,name ,parent
,(nreverse defs)))))
5 changes: 5 additions & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@
#:break
#:continue

;; es6+ stuff
#:es-class
#:static
#:=>

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Deprecated functionality

Expand Down
46 changes: 46 additions & 0 deletions src/printer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ vice-versa.")
(parenthesize-at-toplevel
(lambda () (print-fun-def nil args body-block))))

(defprinter ps-js:=> (args body-block)
(parenthesize-at-toplevel
(lambda ()
(psw "(")
(loop
:for (arg . rest) :on args
:do (ps-print arg)
(when rest
(psw ", ")))
(psw ") => ")
(ps-print body-block))))

(defprinter ps-js:defun (name args docstring body-block)
(when docstring (print-comment docstring))
(print-fun-def name args body-block))
Expand Down Expand Up @@ -388,3 +400,37 @@ vice-versa.")
;; literal-js should be a form that evaluates to a string containing
;; valid JavaScript
(psw literal-js))

(defprinter ps-js:es-class (name parent body)
"class "
(ps-print name)
(when parent
(psw " extends ")
(ps-print parent))
" {"
(incf *indent-level*)
(labels ((fun (form)
(destructuring-bind (name (&rest args) docstring &body body)
form
(declare (ignore docstring))
(print-fun-def (list name nil) args body))))
(dolist (form body)
(newline-and-indent)
(ecase (first form)
(static-assign
(psw "static ")
(ps-print (second form))
(psw " = ")
(ps-print (third form)))
(static-function
(psw "static ")
(fun (rest form)))
(setf
(ps-print (second form))
(psw " = ")
(ps-print (third form)))
(function
(fun (rest form))))))
(decf *indent-level*)
(newline-and-indent)
(psw "}"))