Skip to content

Commit 5856157

Browse files
authored
Merge pull request #26 from mkantor/unparsing-bugs
Fix some unparser bugs
2 parents 830263a + 3c4e626 commit 5856157

File tree

8 files changed

+48
-13
lines changed

8 files changed

+48
-13
lines changed

src/language/compiling/unparsing.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ testCases(
2424
[{}, either.makeRight('{}')],
2525
['a', either.makeRight('a')],
2626
['Hello, world!', either.makeRight('"Hello, world!"')],
27+
['@test', either.makeRight('@test')],
2728
[{ 0: 'a' }, either.makeRight('{ a }')],
2829
[{ 1: 'a' }, either.makeRight('{ 1: a }')],
2930
[
@@ -58,6 +59,17 @@ testCases(
5859
},
5960
either.makeRight('(a => :a)("it works!")'),
6061
],
62+
[
63+
{
64+
0: '@runtime',
65+
1: {
66+
0: '@function',
67+
parameter: 'context',
68+
body: { 0: '@lookup', query: 'context.program.start_time' },
69+
},
70+
},
71+
either.makeRight('{ @runtime, context => :context.program.start_time }'),
72+
],
6173
])
6274

6375
testCases(
@@ -67,6 +79,7 @@ testCases(
6779
[{}, either.makeRight('{}')],
6880
['a', either.makeRight('a')],
6981
['Hello, world!', either.makeRight('"Hello, world!"')],
82+
['@test', either.makeRight('@test')],
7083
[{ 0: 'a' }, either.makeRight('{\n a\n}')],
7184
[{ 1: 'a' }, either.makeRight('{\n 1: a\n}')],
7285
[
@@ -106,6 +119,19 @@ testCases(
106119
},
107120
either.makeRight('(a => :a)("it works!")'),
108121
],
122+
[
123+
{
124+
0: '@runtime',
125+
1: {
126+
0: '@function',
127+
parameter: 'context',
128+
body: { 0: '@lookup', query: 'context.program.start_time' },
129+
},
130+
},
131+
either.makeRight(
132+
'{\n @runtime\n context => :context.program.start_time\n}',
133+
),
134+
],
109135
])
110136

111137
testCases(
@@ -115,6 +141,7 @@ testCases(
115141
[{}, either.makeRight('{}')],
116142
['a', either.makeRight('"a"')],
117143
['Hello, world!', either.makeRight('"Hello, world!"')],
144+
['@test', either.makeRight('"@test"')],
118145
[{ 0: 'a' }, either.makeRight('{\n "0": "a"\n}')],
119146
[{ 1: 'a' }, either.makeRight('{\n "1": "a"\n}')],
120147
[

src/language/semantics/expression.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ export const isExpression = (
99
node: SemanticGraph | Molecule,
1010
): node is Expression =>
1111
typeof node === 'object' && typeof node[0] === 'string' && node[0][0] === '@'
12+
13+
export const isSpecificExpression = <Keyword extends `@${string}`>(
14+
keyword: Keyword,
15+
node: SemanticGraph | Molecule,
16+
): node is {
17+
readonly 0: Keyword
18+
} =>
19+
typeof node === 'object' && typeof node[0] === 'string' && node[0] === keyword

src/language/semantics/expressions/apply-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError } from '../../errors.js'
33
import type { Molecule } from '../../parsing.js'
4-
import { isExpression } from '../expression.js'
4+
import { isSpecificExpression } from '../expression.js'
55
import { makeUnelaboratedObjectNode, type ObjectNode } from '../object-node.js'
66
import { type SemanticGraph, type unelaboratedKey } from '../semantic-graph.js'
77
import { readArgumentsFromExpression } from './expression-utilities.js'
@@ -15,7 +15,7 @@ export type ApplyExpression = ObjectNode & {
1515
export const readApplyExpression = (
1616
node: SemanticGraph | Molecule,
1717
): Either<ElaborationError, ApplyExpression> =>
18-
isExpression(node)
18+
isSpecificExpression('@apply', node)
1919
? either.map(
2020
readArgumentsFromExpression(node, [
2121
['function', '1'],

src/language/semantics/expressions/check-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError } from '../../errors.js'
33
import type { Molecule } from '../../parsing.js'
4-
import { isExpression } from '../expression.js'
4+
import { isSpecificExpression } from '../expression.js'
55
import { makeUnelaboratedObjectNode, type ObjectNode } from '../object-node.js'
66
import { type SemanticGraph, type unelaboratedKey } from '../semantic-graph.js'
77
import { readArgumentsFromExpression } from './expression-utilities.js'
@@ -15,7 +15,7 @@ export type CheckExpression = ObjectNode & {
1515
export const readCheckExpression = (
1616
node: SemanticGraph | Molecule,
1717
): Either<ElaborationError, CheckExpression> =>
18-
isExpression(node)
18+
isSpecificExpression('@check', node)
1919
? either.map(
2020
readArgumentsFromExpression(node, [
2121
['value', '1'],

src/language/semantics/expressions/function-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError } from '../../errors.js'
33
import type { Atom, Molecule } from '../../parsing.js'
4-
import { isExpression } from '../expression.js'
4+
import { isSpecificExpression } from '../expression.js'
55
import { makeUnelaboratedObjectNode, type ObjectNode } from '../object-node.js'
66
import {
77
serialize,
@@ -22,7 +22,7 @@ export type FunctionExpression = ObjectNode & {
2222
export const readFunctionExpression = (
2323
node: SemanticGraph | Molecule,
2424
): Either<ElaborationError, FunctionExpression> =>
25-
isExpression(node)
25+
isSpecificExpression('@function', node)
2626
? either.flatMap(
2727
readArgumentsFromExpression(node, [
2828
['parameter', '1'],

src/language/semantics/expressions/lookup-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError, InvalidExpressionError } from '../../errors.js'
33
import type { Molecule } from '../../parsing.js'
4-
import { isExpression } from '../expression.js'
4+
import { isSpecificExpression } from '../expression.js'
55
import { isFunctionNode } from '../function-node.js'
66
import { keyPathToMolecule, type KeyPath } from '../key-path.js'
77
import {
@@ -23,7 +23,7 @@ export type LookupExpression = ObjectNode & {
2323
export const readLookupExpression = (
2424
node: SemanticGraph | Molecule,
2525
): Either<ElaborationError, LookupExpression> =>
26-
isExpression(node)
26+
isSpecificExpression('@lookup', node)
2727
? either.flatMap(
2828
readArgumentsFromExpression(node, [['query', '1']]),
2929
([q]) => {

src/language/semantics/expressions/runtime-expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError } from '../../errors.js'
33
import type { Molecule } from '../../parsing.js'
4-
import { isExpression } from '../expression.js'
4+
import { isSpecificExpression } from '../expression.js'
55
import { isFunctionNode } from '../function-node.js'
66
import { makeUnelaboratedObjectNode, type ObjectNode } from '../object-node.js'
77
import {
@@ -22,7 +22,7 @@ export type RuntimeExpression = ObjectNode & {
2222
export const readRuntimeExpression = (
2323
node: SemanticGraph | Molecule,
2424
): Either<ElaborationError, RuntimeExpression> =>
25-
isExpression(node)
25+
isSpecificExpression('@runtime', node)
2626
? either.flatMap(
2727
readArgumentsFromExpression(node, [['function', '1']]),
2828
([f]) => {

src/language/unparsing/plz-utilities.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ export const sugarFreeMoleculeAsKeyValuePairStrings = (
113113

114114
export const unparseAtom = (atom: string): Right<string> =>
115115
either.makeRight(
116-
quoteIfNecessary(
117-
/^@[^@]/.test(atom) ? kleur.bold(kleur.underline(atom)) : atom,
118-
),
116+
/^@[^@]/.test(atom)
117+
? kleur.bold(kleur.underline(quoteIfNecessary(atom)))
118+
: quoteIfNecessary(atom),
119119
)
120120

121121
type UnparseAtomOrMolecule = (

0 commit comments

Comments
 (0)