Skip to content

Commit f9a4150

Browse files
committed
Check for specific keyword while reading expressions
This fixes some more unparsing bugs.
1 parent ba194b5 commit f9a4150

File tree

7 files changed

+42
-10
lines changed

7 files changed

+42
-10
lines changed

src/language/compiling/unparsing.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ testCases(
5959
},
6060
either.makeRight('(a => :a)("it works!")'),
6161
],
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+
],
6273
])
6374

6475
testCases(
@@ -108,6 +119,19 @@ testCases(
108119
},
109120
either.makeRight('(a => :a)("it works!")'),
110121
],
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+
],
111135
])
112136

113137
testCases(

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]) => {

0 commit comments

Comments
 (0)