Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object comprehensions via loops in braces #1563

Merged
merged 11 commits into from
Nov 7, 2024
25 changes: 25 additions & 0 deletions civet.dev/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,31 @@ min := for min item of array
max := for max item of array
</Playground>

### Object Comprehensions

Loops can also accumulate their body values into an object.
When any loop is found is found within an object expression,
peey marked this conversation as resolved.
Show resolved Hide resolved
its body value is spread into the containing object.

<Playground>
object := {a: 1, b: 2, c: 3}
doubled := {
for key in object
[key]: 2 * object[key]
}
</Playground>

The loop can exist anywhere a property is expected.
It can be freely mixed with other object properties.

<Playground>
rateLimits := {
admin: Infinity,
for user of users:
peey marked this conversation as resolved.
Show resolved Hide resolved
[user.name]: getRemainingLimit(user)
}
</Playground>

### Infinite Loop

<Playground>
Expand Down
31 changes: 31 additions & 0 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,33 @@ PropertyDefinition
# NOTE: basic identifiers are now part of the rule above
#_?:ws IdentifierReference:id ->
# return prepend(ws, id)
_?:ws InsertDotDotDot:dots IterationExpression:exp ->
let { statement } = exp

// immutably set exp.statement.object = true
statement = { ...statement, object: true }
exp = {
...exp,
statement,
children: exp.children.map(($) => $ === exp.statement ? statement : $),
}

const children = [ws, dots, exp]
if (statement.reduction) {
children.unshift({
type: "Error",
message: "Reduction loops are forbidden in object literals",
})
}

return {
type: "SpreadProperty",
children,
names: exp.names,
dots,
value: exp,
}


NamedProperty
# NOTE: CoverInitializedName early error doesn't seem necessary with this parser
Expand Down Expand Up @@ -6453,6 +6480,10 @@ DotDotDot
"…" ->
return { $loc, token: "..." }

InsertDotDotDot
"" ->
return { $loc, token: "..." }

DoubleColon
"::" ->
return { $loc, token: $1 }
Expand Down
3 changes: 3 additions & 0 deletions source/parser/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ function iterationDeclaration(statement: IterationStatement | ForStatement)
when "max" then "-Infinity"
when "product" then "1"
else "0"
else if statement.object
declaration.children.push "={}"
peey marked this conversation as resolved.
Show resolved Hide resolved
else
// Assign [] directly only in const case, so TypeScript can better infer
if decl is "const"
Expand All @@ -684,6 +686,7 @@ function iterationDeclaration(statement: IterationStatement | ForStatement)
return declaration
unless block.empty
assignResults block, (node) =>
return [ "Object.assign(", resultsRef, ",", node, ")" ] if statement.object
return [ resultsRef, ".push(", node, ")" ] unless reduction
switch reduction.subtype
when "some"
Expand Down
266 changes: 266 additions & 0 deletions test/object-comprehensions.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
{ testCase, throws } from ./helper.civet

describe "object comprehensions", ->
testCase """
basic
---
{
for x of [1, 2, 3]
[x]: 2 * x
[x * 2]: 4 * x
}
---
({
...(()=>{const results={};for (const x of [1, 2, 3]) {
Object.assign(results,({[x]: 2 * x,
[x * 2]: 4 * x}))
}return results})()
})
"""
peey marked this conversation as resolved.
Show resolved Hide resolved

testCase """
with other props
---
{
a: 'a prop'
for x of [1, 2, 3]
[x]: 2 * x
[x * 2]: 4 * x
b: 'b prop'
}
---
({
a: 'a prop',
...(()=>{const results={};for (const x of [1, 2, 3]) {
Object.assign(results,({[x]: 2 * x,
[x * 2]: 4 * x}))
}return results})(),
b: 'b prop'
})
"""

testCase """
mixed trailing commas
---
{
a1: 'a1 prop'
a2: 'a2 prop',
for x of [1, 2, 3]
[x]: 2 * x
[x * 2]: 4 * x,
b1: 'b1 prop'
b2: 'b2 prop'
}
---
({
a1: 'a1 prop',
a2: 'a2 prop',
...(()=>{const results={};for (const x of [1, 2, 3]) {
Object.assign(results,({[x]: 2 * x,
[x * 2]: 4 * x,}))
}return results})(),
b1: 'b1 prop',
b2: 'b2 prop'
})
"""

testCase """
indentation separates comprehension props from object props
---
{
[a]: 'a computed prop'
for x of [1, 2, 3]
[x]: 2 * x
[x * 2]: 4 * x
[b]: 'b computed prop'
}
---
({
[a]: 'a computed prop',
...(()=>{const results={};for (const x of [1, 2, 3]) {
Object.assign(results,({[x]: 2 * x,
[x * 2]: 4 * x}))
}return results})(),
[b]: 'b computed prop'
})
"""

testCase """
scoping
---
{
for results of [1]
[results]: 2 * x
}
---
({
...(()=>{const results1={};for (const results of [1]) {
Object.assign(results1,({[results]: 2 * x}))
}return results1})()
})
"""

testCase """
loop body with additional statements: single prop
---
{
for x of [1]
foo bar
[x]: 2 * x
}
---
({
...(()=>{const results={};for (const x of [1]) {
foo(bar)
Object.assign(results,({[x]: 2 * x}))
}return results})()
})
"""

testCase """
loop body with additional statements: multi prop style 1
peey marked this conversation as resolved.
Show resolved Hide resolved
---
{
for x of [1]
foo bar
{
[x]: 2 * x
[2*x]: 4 * x
}
}
---
({
...(()=>{const results={};for (const x of [1]) {
foo(bar)
Object.assign(results,({
[x]: 2 * x,
[2*x]: 4 * x
}))
}return results})()
})
"""

testCase """
loop body with additional statements: multi prop style 2
---
{
for x of [1]
foo bar
[x]: 2 * x
[2*x]: 4 * x
}
---
({
...(()=>{const results={};for (const x of [1]) {
foo(bar)
Object.assign(results,({[x]: 2 * x,
[2*x]: 4 * x}))
}return results})()
})
"""

testCase """
on first line
---
{for x of [1]
[x]: 2 * x
[2*x]: 4 * x }
---
({...(()=>{const results={};for (const x of [1]) {
Object.assign(results,({[x]: 2 * x,
[2*x]: 4 * x}))
}return results})() })
"""

testCase """
on first line with other props
---
{ a: "a prop", for x of [1]
[x]: 2 * x
[2*x]: 4 * x }
---
({ a: "a prop", ...(()=>{const results={};for (const x of [1]) {
Object.assign(results,({[x]: 2 * x,
[2*x]: 4 * x}))
}return results})() })
"""

testCase """
single line
---
o := {for x of [1] [x]: 2 * x }
---
const o = {...(()=>{const results={};for (const x of [1]({[x]: 2 * x})) {Object.assign(results,x)}return results})() }
"""

testCase """

non-comprehension loop
---
o := {...for x of [1] [x]: 2 * x }
---
const o = {...(()=>{const results=[];for (const x of [1]({[x]: 2 * x})) {results.push(x)}return results})() }
"""

testCase """
while object comprehension
---
o := { while(predicate(x)) [x]: f(x)}
---
const o = { ...(()=>{const results={};while(predicate(x)) Object.assign(results,({[x]: f(x)}));return results})()}
"""

testCase """
non-comprehension while loop
---
o := {...while(predicate(x)) [x]: f(x)}
---
const o = {...(()=>{const results=[];while(predicate(x)) results.push(({[x]: f(x)}));return results})()}
"""

testCase """
do while object comprehension
peey marked this conversation as resolved.
Show resolved Hide resolved
---
o := {do [x]: f(x) while(predicate(x)) }
---
const o = {...(()=>{const results={};do { Object.assign(results,({[x]: f(x)})) } while(predicate(x))return results})() }
"""

testCase """
empty loop is allowed
---
{
for x of [{a: b}, {c:d}]
}
---
({
...(()=>{const results={};for (const x of [{a: b}, {c:d}]) {Object.assign(results,x)}return results})()
})
"""

testCase """
simple expressionization is allowed
---
{
for x of [1, 2, 3]
f(x)
}
---
({
...(()=>{const results={};for (const x of [1, 2, 3]) {
Object.assign(results,f(x))
}return results})()
})
"""

throws """
for reductions are disallowed
---
{
a: b,
for count x of [1, 2, 3]
x % 2 == 0
}
---
ParseErrors: unknown:3:3 Reduction loops are forbidden in object literals
"""
Loading