Skip to content

Commit

Permalink
fix(filters): pass filters to all evals
Browse files Browse the repository at this point in the history
closes dart-archive#755

Ad support for '"s" + ("m"|filter) + "e"'
  • Loading branch information
vicb committed Mar 22, 2014
1 parent d1c6eb1 commit 5b29121
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions bin/parser_generator_for_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ main(arguments) {
'add(a,b)',
'notAProperty',
"'Foo'|uppercase",
"'f' + ('o'|uppercase) + 'o'",
"1|increment:2",
"'abcd'|substring:1:offset",
"'abcd'|substring:1:3|uppercase",
Expand Down
16 changes: 8 additions & 8 deletions lib/core/parser/eval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ class Conditional extends syntax.Conditional {
Conditional(syntax.Expression condition,
syntax.Expression yes, syntax.Expression no)
: super(condition, yes, no);
eval(scope, [FilterMap filters]) => toBool(condition.eval(scope))
? yes.eval(scope)
: no.eval(scope);
eval(scope, [FilterMap filters]) => toBool(condition.eval(scope, filters))
? yes.eval(scope, filters)
: no.eval(scope, filters);
}

class PrefixNot extends syntax.Prefix {
PrefixNot(syntax.Expression expression) : super('!', expression);
eval(scope, [FilterMap filters]) => !toBool(expression.eval(scope));
eval(scope, [FilterMap filters]) => !toBool(expression.eval(scope, filters));
}

class Binary extends syntax.Binary {
Binary(String operation, syntax.Expression left, syntax.Expression right):
super(operation, left, right);
eval(scope, [FilterMap filters]) {
var left = this.left.eval(scope);
var left = this.left.eval(scope, filters);
switch (operation) {
case '&&': return toBool(left) && toBool(this.right.eval(scope));
case '||': return toBool(left) || toBool(this.right.eval(scope));
case '&&': return toBool(left) && toBool(this.right.eval(scope, filters));
case '||': return toBool(left) || toBool(this.right.eval(scope, filters));
}
var right = this.right.eval(scope);
var right = this.right.eval(scope, filters);

// Null check for the operations.
if (left == null || right == null) {
Expand Down
12 changes: 6 additions & 6 deletions lib/core/parser/eval_calls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class CallScope extends syntax.CallScope with CallReflective {
CallScope(name, arguments)
: super(name, arguments),
symbol = newSymbol(name);
eval(scope, [FilterMap filters]) => _eval(scope, scope);
eval(scope, [FilterMap filters]) => _eval(scope, scope, filters);
}

class CallMember extends syntax.CallMember with CallReflective {
final Symbol symbol;
CallMember(object, name, arguments)
: super(object, name, arguments),
symbol = newSymbol(name);
eval(scope, [FilterMap filters]) => _eval(scope, object.eval(scope, filters));
eval(scope, [FilterMap filters]) => _eval(scope, object.eval(scope, filters),
filters);
}

class CallScopeFast0 extends syntax.CallScope with CallFast {
Expand Down Expand Up @@ -106,13 +107,12 @@ abstract class CallReflective {
Symbol get symbol;
syntax.CallArguments get arguments;

// TODO(kasperl): This seems broken -- it needs filters.
_eval(scope, holder) {
List positionals = evalList(scope, arguments.positionals);
_eval(scope, holder, FilterMap filters) {
List positionals = evalList(scope, arguments.positionals, filters);
if (arguments.named.isNotEmpty) {
var named = new Map<Symbol, dynamic>();
arguments.named.forEach((String name, value) {
named[new Symbol(name)] = value.eval(scope);
named[new Symbol(name)] = value.eval(scope, filters);
});
if (holder is Map) {
var fn = ensureFunctionFromMap(holder, name);
Expand Down
5 changes: 3 additions & 2 deletions lib/core/parser/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class EvalError {

/// Evaluate the [list] in context of the [scope].
List evalList(scope, List<Expression> list, [FilterMap filters]) {
int length = list.length;
for (int cacheLength = _evalListCache.length; cacheLength <= length; cacheLength++) {
final length = list.length;
int cacheLength = _evalListCache.length;
for (; cacheLength <= length; cacheLength++) {
_evalListCache.add(new List(cacheLength));
}
List result = _evalListCache[length];
Expand Down
1 change: 1 addition & 0 deletions test/core/parser/parser_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ main() {
describe('filters', () {
it('should call a filter', () {
expect(eval("'Foo'|uppercase", filters)).toEqual("FOO");
expect(eval("'f' + ('o'|uppercase) + 'o'", filters)).toEqual("fOo");
expect(eval("'fOo'|uppercase|lowercase", filters)).toEqual("foo");
});

Expand Down

0 comments on commit 5b29121

Please sign in to comment.