-
Notifications
You must be signed in to change notification settings - Fork 0
/
postfix_expression.dart
47 lines (40 loc) · 1.09 KB
/
postfix_expression.dart
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
part of '../../expression.dart';
class PostfixExpression<I, O1, O> extends ParserBuilder<I, O> {
static const _template = '''
{{var1}}
{{p1}}
if (state.ok) {
{{var2}}
{{p2}}
if (state.ok) {
final v1 = {{val1}};
final v2 = {{val2}};
{{res0}} = {{calculate}};
} else {
state.ok = true;
{{res0}} = {{val1}};
}
}''';
static const _templateFast = '''
{{p1}}
if (state.ok) {
{{p2}}
state.ok = true;
}''';
final SemanticAction<O> calculate;
final ParserBuilder<I, O> expression;
final ParserBuilder<I, O1> operator;
const PostfixExpression(this.expression, this.operator, this.calculate);
@override
String build(Context context, ParserResult? result) {
final fast = result == null;
final r1 = context.getResult(expression, !fast);
final r2 = context.getResult(operator, !fast);
final values = {
'calculate': calculate.build(context, 'calculate', ['v1', 'v2']),
'p1': expression.build(context, r1),
'p2': Silent(operator).build(context, r2),
};
return render2(fast, _templateFast, _template, values, [result, r1, r2]);
}
}