This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreateeval.dart
174 lines (140 loc) · 4.75 KB
/
createeval.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../../elements.dart';
import 'createdata.dart';
Construct parseTemplate(String template) {
final List<Construct> result = [];
final int length = template.length;
int startIndex = 0;
int index = 0;
while (index + 1 < length) {
if (template[index] == '\$' && isLetter(template.codeUnitAt(index + 1))) {
if (startIndex < index) {
result.add(ConstantConstruct(template.substring(startIndex, index)));
}
int startTemplate = index + 1;
index = startTemplate + 1;
while (index < length && isLetterOrDigit(template.codeUnitAt(index))) {
++index;
}
result.add(IdentifierConstruct(template.substring(startTemplate, index)));
startIndex = index;
} else {
++index;
}
}
if (startIndex < length) {
result.add(ConstantConstruct(template.substring(startIndex)));
}
return ConcatenateConstruct(result);
}
Construct parseCode(String code) {
int index = code.indexOf('+=');
if (index >= 0) {
return _parseAssignment(
code.substring(0, index), code.substring(index + 2), AssignmentType.PLUS);
}
index = code.indexOf('=');
if (index >= 0) {
return _parseAssignment(
code.substring(0, index), code.substring(index + 1), AssignmentType.SET);
}
String term = code.trim();
if (term.length > 0 && isLetter(term.codeUnitAt(0))) {
return IdentifierConstruct(term);
} else {
return ConstantConstruct(term);
}
}
Construct _parseAssignment(String lhs, String rhs, AssignmentType type) {
Construct lhsConstruct = parseCode(lhs);
Construct rhsConstruct = parseCode(rhs);
if (!(lhsConstruct is IdentifierConstruct)) {
return ConstantConstruct(renderError(lhs));
}
return AssignmentConstruct(lhsConstruct as IdentifierConstruct, rhsConstruct, type);
}
abstract class Construct {
void observe(EvaluationContext context, Operation operation, Lifespan lifespan);
String evaluate(EvaluationContext datastore);
}
class ConstantConstruct implements Construct {
final String value;
ConstantConstruct(this.value);
void observe(EvaluationContext context, Operation operation, Lifespan lifespan) => null;
String evaluate(EvaluationContext context) => value;
}
String renderError(String symbol) => symbol + '???';
class IdentifierConstruct implements Construct {
final String identifier;
IdentifierConstruct(this.identifier);
void observe(EvaluationContext context, Operation operation, Lifespan lifespan) {
CompositeData record = context.resolve(identifier);
// TODO: handle non-DataRecord records
if (record != null && record is DataRecord) {
record.state.observeDeep(operation, lifespan);
}
}
String evaluate(EvaluationContext context) {
CompositeData record = context.resolve(identifier);
// TODO: handle non-DataRecord records
if (record != null && record is DataRecord) {
return record.state.value;
} else {
return error;
}
}
Ref<String> getRef(EvaluationContext context) {
CompositeData record = context.resolve(identifier);
if (record != null && record is DataRecord) {
return record.state;
} else {
return null;
}
}
String get error => renderError(identifier);
}
class ConcatenateConstruct implements Construct {
final List<Construct> parameters;
ConcatenateConstruct(this.parameters);
void observe(EvaluationContext context, Operation operation, Lifespan lifespan) {
parameters.forEach((c) => c.observe(context, operation, lifespan));
}
String evaluate(EvaluationContext context) {
StringBuffer result = StringBuffer();
parameters.forEach((c) => result.write(c.evaluate(context)));
return result.toString();
}
}
enum AssignmentType { SET, PLUS }
class AssignmentConstruct implements Construct {
final IdentifierConstruct lhs;
final Construct rhs;
final AssignmentType type;
AssignmentConstruct(this.lhs, this.rhs, this.type);
void observe(EvaluationContext context, Operation operation, Lifespan lifespan) {
lhs.observe(context, operation, lifespan);
rhs.observe(context, operation, lifespan);
}
String evaluate(EvaluationContext context) {
Ref<String> lhsRef = lhs.getRef(context);
if (lhsRef == null) {
return lhs.error;
}
String rhsValue = rhs.evaluate(context);
switch (type) {
case AssignmentType.SET:
lhsRef.value = rhsValue;
break;
case AssignmentType.PLUS:
lhsRef.value = (parseInt(lhsRef.value) + parseInt(rhsValue)).toString();
break;
}
return lhsRef.value;
}
static int parseInt(String s) {
int result = int.tryParse(s);
return result != null ? result : 0;
}
}