Skip to content

Commit d3b3a67

Browse files
committed
allow object literals without values jcubic#185
1 parent a83cdef commit d3b3a67

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

dist/std.min.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(define (alist->object alist) "(alist->object alist)\u000A\u000AFunction convert alist pairs to JavaScript object." (if (pair? alist) (alist.to_object) (alist->object (new lips.Pair #<undefined> ()))))
1919
(define (parent.frames) "(parent.frames)\u000A\u000AFuncion return list of environments from parent frames (lambda function calls)" (let iter ((result (quote ())) (frame (parent.frame 1))) (if (eq? frame (interaction-environment)) (cons frame result) (if (null? frame) result (let ((parent.frame (--> frame (get (quote parent.frame) (object :throwError #f))))) (if (function? parent.frame) (iter (cons frame result) (parent.frame 0)) result))))))
2020
(define (pair-map fn seq-list) "(pair-map fn list)\u000A\u000AFunction call fn argument for pairs in a list and return combined list with\u000Avalues returned from function fn. It work like the map but take two items from list" (let iter ((seq-list seq-list) (result (quote ()))) (if (null? seq-list) result (if (and (pair? seq-list) (pair? (cdr seq-list))) (let* ((first (car seq-list)) (second (cadr seq-list)) (value (fn first second))) (if (null? value) (iter (cddr seq-list) result) (iter (cddr seq-list) (cons value result))))))))
21-
(define (object-expander readonly expr . rest) "(object-expander reaonly '(:foo (:bar 10) (:baz (1 2 3))))\u000A\u000ARecursive function helper for defining LIPS code for create objects\u000Ausing key like syntax." (let ((name (gensym "name")) (quot (if (null? rest) #f (car rest)))) (if (null? expr) (quasiquote (alist->object ())) (quasiquote (let (((unquote name) (alist->object (quote ())))) (unquote-splicing (pair-map (lambda (key value) (if (not (key? key)) (let ((msg (string-append (type key) " " (repr key) " is not a symbol!"))) (throw msg)) (let ((prop (key->string key))) (if (and (pair? value) (key? (car value))) (quasiquote (set-obj! (unquote name) (unquote prop) (unquote (object-expander readonly value)))) (if quot (quasiquote (set-obj! (unquote name) (unquote prop) (quote (unquote value)))) (quasiquote (set-obj! (unquote name) (unquote prop) (unquote value)))))))) expr)) (unquote (if readonly (quasiquote (Object.freeze (unquote name))))) (unquote name))))))
21+
(define (object-expander readonly expr . rest) "(object-expander reaonly '(:foo (:bar 10) (:baz (1 2 3))))\u000A(object-expander reaonly '(:foo :bar))\u000A\u000A\u000ARecursive function helper for defining LIPS code for create objects\u000Ausing key like syntax. if no values are used it will create JavaScript\u000Ashorthand objects where keys are used for keys and values" (let ((name (gensym "name")) (quot (if (null? rest) #f (car rest)))) (if (null? expr) (quasiquote (alist->object ())) (quasiquote (let (((unquote name) (alist->object (quote ())))) (unquote-splicing (let loop ((lst expr) (result ())) (if (null? lst) (reverse result) (let ((first (car lst)) (second (if (null? (cdr lst)) () (cadr lst)))) (if (not (key? first)) (let ((msg (string-append (type first) " " (repr first) " is not a symbol!"))) (throw msg)) (let ((prop (key->string first))) (if (or (key? second) (null? second)) (let ((code (quasiquote (set-obj! (unquote name) (unquote prop) #<undefined>)))) (loop (cdr lst) (cons code result))) (let ((code (if (and (pair? second) (key? (car second))) (quasiquote (set-obj! (unquote name) (unquote prop) (unquote (object-expander readonly second)))) (if quot (quasiquote (set-obj! (unquote name) (unquote prop) (quote (unquote second)))) (quasiquote (set-obj! (unquote name) (unquote prop) (unquote second))))))) (loop (cddr lst) (cons code result)))))))))) (unquote (if readonly (quasiquote (Object.freeze (unquote name))))) (unquote name))))))
2222
(define-macro (object . expr) "(object :name value)\u000A\u000AMacro that create JavaScript object using key like syntax." (try (object-expander #f expr) (catch (e) (error e.message))))
2323
(define-macro (object-literal . expr) "(object-literal :name value)\u000A\u000AMacro that create JavaScript object using key like syntax. This is similar,\u000Ato object but all values are quoted. This macro is used with & object literal." (try (object-expander #t expr #t) (catch (e) (error e.message))))
2424
(define (alist->assign desc . sources) "(alist->assign alist . list-of-alists)\u000A\u000AFunction that work like Object.assign but for LIPS alist." (for-each (lambda (source) (for-each (lambda (pair) (let* ((key (car pair)) (value (cdr pair)) (d-pair (assoc key desc))) (if (pair? d-pair) (set-cdr! d-pair value) (append! desc (list pair))))) source)) sources) desc)

dist/std.scm

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -265,31 +265,41 @@
265265
;; -----------------------------------------------------------------------------
266266
(define (object-expander readonly expr . rest)
267267
"(object-expander reaonly '(:foo (:bar 10) (:baz (1 2 3))))
268+
(object-expander reaonly '(:foo :bar))
269+
268270

269271
Recursive function helper for defining LIPS code for create objects
270-
using key like syntax."
272+
using key like syntax. if no values are used it will create JavaScript
273+
shorthand objects where keys are used for keys and values"
271274
(let ((name (gensym "name")) (quot (if (null? rest) false (car rest))))
272275
(if (null? expr)
273276
`(alist->object ())
274277
`(let ((,name (alist->object '())))
275-
,@(pair-map (lambda (key value)
276-
(if (not (key? key))
277-
(let ((msg (string-append (type key)
278-
" "
279-
(repr key)
280-
" is not a symbol!")))
281-
(throw msg))
282-
(let ((prop (key->string key)))
283-
(if (and (pair? value) (key? (car value)))
284-
`(set-obj! ,name
285-
,prop
286-
,(object-expander readonly value))
287-
(if quot
288-
`(set-obj! ,name ,prop ',value)
289-
`(set-obj! ,name ,prop ,value))))))
290-
expr)
278+
,@(let loop ((lst expr) (result nil))
279+
(if (null? lst)
280+
(reverse result)
281+
(let ((first (car lst))
282+
(second (if (null? (cdr lst)) nil (cadr lst))))
283+
(if (not (key? first))
284+
(let ((msg (string-append (type first)
285+
" "
286+
(repr first)
287+
" is not a symbol!")))
288+
(throw msg))
289+
(let ((prop (key->string first)))
290+
(if (or (key? second) (null? second))
291+
(let ((code `(set-obj! ,name ,prop undefined)))
292+
(loop (cdr lst) (cons code result)))
293+
(let ((code (if (and (pair? second) (key? (car second)))
294+
`(set-obj! ,name
295+
,prop
296+
,(object-expander readonly second))
297+
(if quot
298+
`(set-obj! ,name ,prop ',second)
299+
`(set-obj! ,name ,prop ,second)))))
300+
(loop (cddr lst) (cons code result)))))))))
291301
,(if readonly
292-
`(Object.freeze ,name))
302+
`(Object.freeze ,name))
293303
,name))))
294304

295305
;; -----------------------------------------------------------------------------

dist/std.xcb

982 Bytes
Binary file not shown.

lib/bootstrap.scm

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -265,31 +265,41 @@
265265
;; -----------------------------------------------------------------------------
266266
(define (object-expander readonly expr . rest)
267267
"(object-expander reaonly '(:foo (:bar 10) (:baz (1 2 3))))
268+
(object-expander reaonly '(:foo :bar))
269+
268270

269271
Recursive function helper for defining LIPS code for create objects
270-
using key like syntax."
272+
using key like syntax. if no values are used it will create JavaScript
273+
shorthand objects where keys are used for keys and values"
271274
(let ((name (gensym "name")) (quot (if (null? rest) false (car rest))))
272275
(if (null? expr)
273276
`(alist->object ())
274277
`(let ((,name (alist->object '())))
275-
,@(pair-map (lambda (key value)
276-
(if (not (key? key))
277-
(let ((msg (string-append (type key)
278-
" "
279-
(repr key)
280-
" is not a symbol!")))
281-
(throw msg))
282-
(let ((prop (key->string key)))
283-
(if (and (pair? value) (key? (car value)))
284-
`(set-obj! ,name
285-
,prop
286-
,(object-expander readonly value))
287-
(if quot
288-
`(set-obj! ,name ,prop ',value)
289-
`(set-obj! ,name ,prop ,value))))))
290-
expr)
278+
,@(let loop ((lst expr) (result nil))
279+
(if (null? lst)
280+
(reverse result)
281+
(let ((first (car lst))
282+
(second (if (null? (cdr lst)) nil (cadr lst))))
283+
(if (not (key? first))
284+
(let ((msg (string-append (type first)
285+
" "
286+
(repr first)
287+
" is not a symbol!")))
288+
(throw msg))
289+
(let ((prop (key->string first)))
290+
(if (or (key? second) (null? second))
291+
(let ((code `(set-obj! ,name ,prop undefined)))
292+
(loop (cdr lst) (cons code result)))
293+
(let ((code (if (and (pair? second) (key? (car second)))
294+
`(set-obj! ,name
295+
,prop
296+
,(object-expander readonly second))
297+
(if quot
298+
`(set-obj! ,name ,prop ',second)
299+
`(set-obj! ,name ,prop ,second)))))
300+
(loop (cddr lst) (cons code result)))))))))
291301
,(if readonly
292-
`(Object.freeze ,name))
302+
`(Object.freeze ,name))
293303
,name))))
294304

295305
;; -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)