Skip to content

Commit

Permalink
add new experimental hash syntax (backwards compatible)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Mar 29, 2016
1 parent a43d239 commit a68249b
Show file tree
Hide file tree
Showing 24 changed files with 843 additions and 165 deletions.
2 changes: 1 addition & 1 deletion docs/docs.json

Large diffs are not rendered by default.

31 changes: 26 additions & 5 deletions docs/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ examples:
// 0.5.0

(quote (meta ("compiled at " (|> Date new .to-string))))
"compiled at Mon Mar 21 2016 00:28:10 GMT-0700 (PDT)"
"compiled at Tue Mar 29 2016 13:09:39 GMT-0700 (PDT)"

name: macro core/lambda
description: Defines a lambda/function/closure in Sibilant. Equivalent to
Expand Down Expand Up @@ -1083,7 +1083,8 @@ document.body.addEventListener((function(event) {

name: macro core/hash
description: this is the macro that is called by braces (`{}`). Produces a
javascript object out of alternating key value pairs.
javascript object out of alternating key value pairs. To repeat an
entry as both key and value, use the & character, as shown in examples. To use the value of a variable as a key, use the backtick character before the key. These can be combined
tags: collections, objects
arguments: (...pairs)
examples:
Expand All @@ -1099,6 +1100,26 @@ examples:
{ 'key { 'nested 'value } }
{ "key": { "nested": "value" } }

{ kv1& kv2& }
{
kv1: kv1,
kv2: kv2
}

{ `variable 1 }
(function() {
var hash$1 = { };
hash$1[variable] = 1;
return hash$1;
}).call(this)

{ `variable & }
(function() {
var hash$2 = { };
hash$2[variable] = variable;
return hash$2;
}).call(this)

name: function core/simple-list
description: This is the macro that is called when brackets (`[]`) are
used. Emits a javascript array literal. Splats (`...`) can be used to
Expand Down Expand Up @@ -1157,18 +1178,18 @@ arguments: (thing ...body)
examples:
(|> 2 (tap (+ 5) console.log) (* 10))
((function() {
/* include/macros.sibilant:1454:9 */
/* include/macros.sibilant:1485:9 */

console.log((arguments[0] + 5));
return arguments[0];
})(2) * 10)

(#-> .to-upper-case (tap console.log) (.split " "))
(function() {
/* include/macros.sibilant:1452:17 */
/* include/macros.sibilant:1483:17 */

return (function() {
/* include/macros.sibilant:1454:9 */
/* include/macros.sibilant:1485:9 */

console.log(arguments[0]);
return arguments[0];
Expand Down
67 changes: 49 additions & 18 deletions include/macros.sibilant
Original file line number Diff line number Diff line change
Expand Up @@ -1295,27 +1295,58 @@ returned. Aliased as `#`, as shown in examples."
value)

(docs "this is the macro that is called by braces (`{}`). Produces a
javascript object out of alternating key value pairs."
javascript object out of alternating key value pairs. To repeat an
entry as both key and value, use the & character, as shown in examples. To use the value of a variable as a key, use the backtick character before the key. These can be combined"
tags [ collections objects ]
examples [ (hash k1 v1 k2 v2) (hash 'key 'value) { 'key { 'nested 'value } } ])
examples [ (hash k1 v1 k2 v2)
(hash 'key 'value)
{ 'key { 'nested 'value } }
{ kv1& kv2& } { `variable 1 } { `variable & } ])


(macro hash (...pairs)
(when (odd? pairs.length)
(error ("odd number of key-value pairs in hash: "
(call inspect pairs))))

(var quote-keys sibilant.quote-hash-keys
pair-strings (bulk-map pairs (#(key value)
[ (if (and quote-keys (not (node? key 'string)))
["\"" (transpile key) "\""]
(transpile key))
": "
(transpile value)])))
(if (>= 1 pair-strings.length)
["{ " (interleave ", " pair-strings) " }"]
["{" (indent (interleave ",\n" pair-strings)) "}"]))

(assign pairs (pairs.map (#(p i)
(if (and (= p.token "&") (node? p 'special))
(do
(var double (get pairs (if (even? i) (+ 1 i) (- i 1))))
(if (and (node? double 'tick) (= double.token "`"))
(first double.contents)
double))
p))))

(when (odd? pairs.length)
(error ("odd number of key-value pairs in hash: "
(call inspect pairs))))

(var {dynamic-keys static-keys}
(pairs.reduce (#(o item i)
(if (and (even? i) (node? item 'tick) (= item.token "`"))
(Object.assign {} o { dynamic-keys: [ ...o.dynamic-keys (first item.contents) ] })

(and (odd? o.dynamic-keys.length) (odd? i))
(Object.assign {} o { dynamic-keys: [ ...o.dynamic-keys item ] })

(Object.assign {} o { static-keys: [ ...o.static-keys item ] })))
{ dynamic-keys: [], static-keys: [] }))

(var quote-keys sibilant.quote-hash-keys
pair-strings (bulk-map static-keys (#(key value)
[ (if (and quote-keys (not (node? key 'string)))
["\"" (transpile key) "\""]
(transpile key))
": "
(transpile value)])))

(if dynamic-keys.length
(do
(var symbol (generate-symbol 'hash))
`(*scoped-without-source
(var @symbol (hash ...@static-keys))
(set @symbol ...@dynamic-keys)
@symbol))

(>= 1 pair-strings.length)
["{ " (interleave ", " pair-strings) " }"]
["{" (indent (interleave ",\n" pair-strings)) "}"]))

(macro quote (content)
(var unquotes (find-unquotes content))
Expand Down

0 comments on commit a68249b

Please sign in to comment.