Skip to content

Commit 6969f52

Browse files
authored
Merge pull request #53 from mkantor/miscellanea
Miscellaneous minor improvements
2 parents 926440b + d40ae43 commit 6969f52

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

src/language/cli/input.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const handleInput = async <Result>(
1818
'input-format': { type: 'string' },
1919
},
2020
})
21+
22+
// Only JSON is supported currently. `--input-format` isn't really necessary
23+
// right now, but requiring it ensures forwards-compatibility of scripts when
24+
// other formats are added.
2125
if (args.values['input-format'] === undefined) {
2226
throw new Error('Missing required option: --input-format')
2327
} else if (args.values['input-format'] !== 'json') {

src/language/runtime/evaluator.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ testCases(evaluate, input => `evaluating \`${JSON.stringify(input)}\``)(
5959
],
6060
],
6161
output => {
62-
assert(!either.isLeft(output))
62+
if (either.isLeft(output)) {
63+
assert.fail(output.value.message)
64+
}
6365
assert(typeof output.value === 'object')
6466
assert(output.value['tag'] === 'some')
6567
assert(typeof output.value['value'] === 'string')

src/language/semantics/prelude.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const keyPathToLookupExpression = (keyPath: NonEmptyKeyPath) => {
6363
}
6464
}
6565

66-
const serializePartiallyAppliedFunction =
66+
const serializeOnceAppliedFunction =
6767
(keyPath: NonEmptyKeyPath, argument: SemanticGraph) => () =>
6868
either.makeRight(
6969
makeApplyExpression({
@@ -108,7 +108,7 @@ export const prelude: ObjectNode = makeObjectNode({
108108
parameter: types.functionType,
109109
return: types.something,
110110
},
111-
serializePartiallyAppliedFunction(['apply'], argument),
111+
serializeOnceAppliedFunction(['apply'], argument),
112112
option.none,
113113
functionToApply => {
114114
if (!isFunctionNode(functionToApply)) {
@@ -175,7 +175,7 @@ export const prelude: ObjectNode = makeObjectNode({
175175
parameter: function0.signature.parameter,
176176
return: function1.signature.parameter,
177177
},
178-
serializePartiallyAppliedFunction(
178+
serializeOnceAppliedFunction(
179179
['flow'],
180180
makeObjectNode({ 0: function0, 1: function1 }),
181181
),
@@ -205,7 +205,7 @@ export const prelude: ObjectNode = makeObjectNode({
205205
parameter: types.integer,
206206
return: types.integer,
207207
},
208-
serializePartiallyAppliedFunction(['integer', 'add'], number2),
208+
serializeOnceAppliedFunction(['integer', 'add'], number2),
209209
option.none,
210210
number1 => {
211211
if (
@@ -262,10 +262,7 @@ export const prelude: ObjectNode = makeObjectNode({
262262
parameter: types.integer,
263263
return: types.boolean,
264264
},
265-
serializePartiallyAppliedFunction(
266-
['integer', 'less_than'],
267-
number2,
268-
),
265+
serializeOnceAppliedFunction(['integer', 'less_than'], number2),
269266
option.none,
270267
number1 => {
271268
if (
@@ -304,7 +301,7 @@ export const prelude: ObjectNode = makeObjectNode({
304301
parameter: types.integer,
305302
return: types.integer,
306303
},
307-
serializePartiallyAppliedFunction(['integer', 'subtract'], number2),
304+
serializeOnceAppliedFunction(['integer', 'subtract'], number2),
308305
option.none,
309306
number1 => {
310307
if (
@@ -384,7 +381,7 @@ export const prelude: ObjectNode = makeObjectNode({
384381
parameter: types.something,
385382
return: types.something,
386383
},
387-
serializePartiallyAppliedFunction(['match'], cases),
384+
serializeOnceAppliedFunction(['match'], cases),
388385
option.none,
389386
argument => {
390387
if (!nodeIsTagged(argument)) {
@@ -432,10 +429,7 @@ export const prelude: ObjectNode = makeObjectNode({
432429
parameter: types.naturalNumber,
433430
return: types.naturalNumber,
434431
},
435-
serializePartiallyAppliedFunction(
436-
['natural_number', 'add'],
437-
number2,
438-
),
432+
serializeOnceAppliedFunction(['natural_number', 'add'], number2),
439433
option.none,
440434
number1 => {
441435
if (
@@ -509,7 +503,7 @@ export const prelude: ObjectNode = makeObjectNode({
509503
parameter: types.something,
510504
return: types.something,
511505
},
512-
serializePartiallyAppliedFunction(['object', 'lookup'], key),
506+
serializeOnceAppliedFunction(['object', 'lookup'], key),
513507
option.none,
514508
argument => {
515509
if (!isObjectNode(argument)) {
@@ -560,7 +554,7 @@ export const prelude: ObjectNode = makeObjectNode({
560554
parameter: types.atom,
561555
return: types.atom,
562556
},
563-
serializePartiallyAppliedFunction(['atom', 'append'], atomToAppend),
557+
serializeOnceAppliedFunction(['atom', 'append'], atomToAppend),
564558
option.none,
565559
atomToAppendTo => {
566560
if (
@@ -594,10 +588,7 @@ export const prelude: ObjectNode = makeObjectNode({
594588
parameter: types.atom,
595589
return: types.atom,
596590
},
597-
serializePartiallyAppliedFunction(
598-
['atom', 'prepend'],
599-
atomToPrepend,
600-
),
591+
serializeOnceAppliedFunction(['atom', 'prepend'], atomToPrepend),
601592
option.none,
602593
atomToPrependTo => {
603594
if (

src/language/unparsing/plz-utilities.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,40 +195,50 @@ const unparseSugaredIndex = (
195195
)
196196
if (either.isLeft(objectUnparseResult)) {
197197
return objectUnparseResult
198-
}
198+
} else {
199+
if (typeof expression.query !== 'object') {
200+
// TODO: It would be nice if this were provably impossible.
201+
return either.makeLeft({
202+
kind: 'unserializableValue',
203+
message: 'Invalid index expression',
204+
})
205+
} else {
206+
const keyPath = Object.entries(expression.query).reduce(
207+
(accumulator: KeyPath | 'invalid', [key, value]) => {
208+
if (accumulator === 'invalid') {
209+
return accumulator
210+
} else {
211+
if (
212+
key === String(accumulator.length) &&
213+
typeof value === 'string'
214+
) {
215+
return [...accumulator, value]
216+
} else {
217+
return 'invalid'
218+
}
219+
}
220+
},
221+
[],
222+
)
199223

200-
const keyPath = Object.entries(expression.query).reduce(
201-
(accumulator: KeyPath | 'invalid', [key, value]) => {
202-
if (accumulator === 'invalid') {
203-
return accumulator
224+
if (
225+
keyPath === 'invalid' ||
226+
Object.keys(expression.query).length !== keyPath.length
227+
) {
228+
return either.makeLeft({
229+
kind: 'unserializableValue',
230+
message: 'invalid key path',
231+
})
204232
} else {
205-
if (key === String(accumulator.length) && typeof value === 'string') {
206-
return [...accumulator, value]
207-
} else {
208-
return 'invalid'
209-
}
233+
const { dot } = punctuation(kleur)
234+
return either.makeRight(
235+
objectUnparseResult.value
236+
.concat(dot)
237+
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
238+
)
210239
}
211-
},
212-
[],
213-
)
214-
215-
if (
216-
keyPath === 'invalid' ||
217-
Object.keys(expression.query).length !== keyPath.length
218-
) {
219-
return either.makeLeft({
220-
kind: 'unserializableValue',
221-
message: 'invalid key path',
222-
})
240+
}
223241
}
224-
225-
const { dot } = punctuation(kleur)
226-
227-
return either.makeRight(
228-
objectUnparseResult.value
229-
.concat(dot)
230-
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
231-
)
232242
}
233243

234244
const unparseSugaredLookup = (

0 commit comments

Comments
 (0)