Skip to content
This repository was archived by the owner on Sep 16, 2022. It is now read-only.

Commit 564e492

Browse files
committed
Add new IR model to compiler.
Right now, this is just the root "Component" node. In follow-up CLs, I'll add the "ComponentView" and "HostView" classes and start using them in the actual compiler pipeline. PiperOrigin-RevId: 221682466
1 parent 828cfe8 commit 564e492

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// Base class for all intermediate representation (IR) data model classes.
2+
abstract class IRNode {
3+
R accept<R, C>(IRVisitor<R, C> visitor, [C context]);
4+
}
5+
6+
/// The core reusable UI building blocks for an application.
7+
class Component implements IRNode {
8+
final String name;
9+
10+
Component(this.name);
11+
12+
@override
13+
R accept<R, C>(IRVisitor<R, C> visitor, [C context]) =>
14+
visitor.visitComponent(this, context);
15+
}
16+
17+
abstract class IRVisitor<R, C> {
18+
R visitComponent(Component component, [C context]);
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import '../model.dart';
2+
3+
/// An [IRVisitor] which simply returns null for every node visited.
4+
///
5+
/// This is intended to be a base class for other [IRVisitors], allowing other
6+
/// visitors to implement only the methods needed.
7+
class DefaultIRVisitor<R, C> implements IRVisitor<R, C> {
8+
@override
9+
R visitComponent(Component component, [C context]) => null;
10+
11+
/// Visits all nodes provided, aggregating the result.
12+
///
13+
/// If the visitor returns [null], then the result is removed from the list.
14+
List<T> visitAll<T extends R>(Iterable<IRNode> nodes, [C context]) {
15+
final results = <T>[];
16+
for (var node in nodes) {
17+
var result = node.accept(this, context) as T;
18+
if (result != null) results.add(result);
19+
}
20+
return results;
21+
}
22+
}

0 commit comments

Comments
 (0)