Line data Source code
1 : // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'package:collection/collection.dart';
6 :
7 : import 'ast.dart';
8 : import 'visitor.dart';
9 :
10 : typedef bool _Semantics(String variable);
11 :
12 : /// A visitor for evaluating boolean selectors against a specific set of
13 : /// semantics.
14 : class Evaluator implements Visitor<bool> {
15 : /// The semantics to evaluate against.
16 : final _Semantics _semantics;
17 :
18 : Evaluator(semantics)
19 0 : : _semantics = semantics is Iterable
20 0 : ? DelegatingIterable.typed(semantics.toSet()).contains
21 0 : : semantics as _Semantics;
22 :
23 0 : bool visitVariable(VariableNode node) => _semantics(node.name);
24 :
25 0 : bool visitNot(NotNode node) => !node.child.accept(this);
26 :
27 : bool visitOr(OrNode node) =>
28 0 : node.left.accept(this) || node.right.accept(this);
29 :
30 : bool visitAnd(AndNode node) =>
31 0 : node.left.accept(this) && node.right.accept(this);
32 :
33 0 : bool visitConditional(ConditionalNode node) => node.condition.accept(this)
34 0 : ? node.whenTrue.accept(this)
35 0 : : node.whenFalse.accept(this);
36 : }
|