-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_map.ts
More file actions
38 lines (36 loc) · 1.18 KB
/
filter_map.ts
File metadata and controls
38 lines (36 loc) · 1.18 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
// Port of expr-lang/expr optimizer/filter_map.go
import { Node, BuiltinNode, PredicateNode, PointerNode } from "../ast/node.js";
import { Find } from "../ast/find.js";
import { Visitor, NodeRef } from "../ast/visitor.js";
import { patchCopyType } from "./optimizer.js";
export class filterMap implements Visitor {
Visit(node: NodeRef): void {
const mapBuiltin = node.node;
if (
mapBuiltin instanceof BuiltinNode &&
mapBuiltin.Name === "map" &&
mapBuiltin.Arguments.length === 2 &&
Find(mapBuiltin.Arguments[1]!, isIndexPointer) === null
) {
const predicate = mapBuiltin.Arguments[1]!;
if (predicate instanceof PredicateNode) {
const filter = mapBuiltin.Arguments[0]!;
if (
filter instanceof BuiltinNode &&
filter.Name === "filter" &&
filter.Map === null /* not already optimized */
) {
const b = new BuiltinNode("filter", filter.Arguments);
b.Map = predicate.Node;
patchCopyType(node, b);
}
}
}
}
}
function isIndexPointer(node: Node): boolean {
if (node instanceof PointerNode && node.Name === "index") {
return true;
}
return false;
}