-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcreate-hug-function.ts
More file actions
47 lines (44 loc) · 1.56 KB
/
create-hug-function.ts
File metadata and controls
47 lines (44 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { TupleExpression } from '../slang-nodes/TupleExpression.js';
import { TupleValues } from '../slang-nodes/TupleValues.js';
import { TupleValue } from '../slang-nodes/TupleValue.js';
import { isBinaryOperation } from './is-binary-operation.js';
import type { Expression } from '../slang-nodes/Expression.ts';
export function createHugFunction(
huggableOperators: string[]
): (node: Expression['variant']) => Expression['variant'] {
const operators = new Set(huggableOperators);
return (node: Expression['variant']): Expression['variant'] => {
if (isBinaryOperation(node) && operators.has(node.operator)) {
const loc = node.loc;
return Object.assign(
Object.create(TupleExpression.prototype) as TupleExpression,
{
kind: NonterminalKind.TupleExpression,
loc: { ...loc },
comments: undefined,
items: Object.assign(
Object.create(TupleValues.prototype) as TupleValues,
{
kind: NonterminalKind.TupleValues,
loc: { ...loc },
comments: undefined,
items: [
Object.assign(
Object.create(TupleValue.prototype) as TupleValue,
{
kind: NonterminalKind.TupleValue,
loc: { ...loc },
comments: undefined,
expression: node
}
)
]
}
)
}
);
}
return node;
};
}