-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathBitwiseOrExpression.ts
More file actions
72 lines (60 loc) · 2 KB
/
BitwiseOrExpression.ts
File metadata and controls
72 lines (60 loc) · 2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
import { createHugFunction } from '../slang-utils/create-hug-function.js';
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
import { extractVariant } from '../slang-utils/extract-variant.js';
import { SlangNode } from './SlangNode.js';
import { Expression } from './Expression.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { PrintableNode } from './types.d.ts';
const tryToHug = createHugFunction([
'+',
'-',
'*',
'/',
'**',
'<<',
'>>',
'&',
'^'
]);
const printBitwiseOrExpression = printBinaryOperation(
createKindCheckFunction([
NonterminalKind.InequalityExpression,
NonterminalKind.EqualityExpression,
NonterminalKind.AndExpression,
NonterminalKind.OrExpression
])
);
export class BitwiseOrExpression extends SlangNode {
readonly kind = NonterminalKind.BitwiseOrExpression;
leftOperand: Expression['variant'];
operator: string;
rightOperand: Expression['variant'];
constructor(
ast: ast.BitwiseOrExpression,
collected: CollectedMetadata,
options: ParserOptions<PrintableNode>
) {
super(ast, collected);
this.leftOperand = extractVariant(
new Expression(ast.leftOperand, collected, options)
);
this.operator = ast.operator.unparse();
this.rightOperand = extractVariant(
new Expression(ast.rightOperand, collected, options)
);
this.updateMetadata(this.leftOperand, this.rightOperand);
this.leftOperand = tryToHug(this.leftOperand);
this.rightOperand = tryToHug(this.rightOperand);
}
print(
path: AstPath<BitwiseOrExpression>,
print: PrintFunction,
options: ParserOptions<PrintableNode>
): Doc {
return printBitwiseOrExpression(this, path, print, options);
}
}