-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLanguage.cs
357 lines (298 loc) · 12.2 KB
/
Language.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
This program and the accompanying materials are made available under the
terms of the MIT license (X11 license) which accompanies this distribution.
Author: D. Langner, C. Bürger
*/
using System;
using System.Linq;
using System.Collections.Generic;
using Ast = Racr.AstNode;
using AgRule = Racr.AgRuleAttribute;
static class QuestionnairesLanguage {
public static Racr.Specification QL;
static QuestionnairesLanguage() {
QL = new Racr.Specification();
QL.AstRule("Form->Element*<Body");
QL.AstRule("Element->");
QL.AstRule("Group:Element->Expression-Element*<Body");
QL.AstRule("Question:Element->name-label");
QL.AstRule("OrdinaryQuestion:Question->type-value");
QL.AstRule("ComputedQuestion:Question->Expression");
QL.AstRule("Expression->");
QL.AstRule("Use:Expression->name");
QL.AstRule("Constant:Expression->value");
QL.AstRule("Computation:Expression->operator-Expression*<Operands");
QL.CompileAstSpecifications("Form");
QL.RegisterAgRules(typeof(QuestionnairesLanguage));
QL.RegisterAgRules(typeof(QuestionnairesGui));
QL.CompileAgSpecifications();
}
// AST Accessors:
public static Ast GetBody(this Ast n) { return n.Child("Body"); }
public static Ast GetExpression(this Ast n) { return n.Child("Expression"); }
public static string GetName(this Ast n) { return n.Child<string>("name"); }
public static string GetLabel(this Ast n) { return n.Child<string>("label"); }
public static Types GetValueType(this Ast n) { return n.Child<Types>("type"); }
public static object GetValue(this Ast n) { return n.Child<object>("value"); }
public static Ast GetOperands(this Ast n) { return n.Child("Operands"); }
public static string GetOperator(this Ast n) { return n.Child<string>("operator"); }
// Attribute Accessors:
public static Ast Root(this Ast n) { return n.AttValue<Ast>("Root"); }
public static Ast ErrorQuestion(this Ast n) { return n.AttValue<Ast>("ErrorQuestion"); }
public static bool IsErrorQuestion(this Ast n) { return n.AttValue<bool>("IsErrorQuestion"); }
public static Ast GLookup(this Ast n, string name) { return n.AttValue<Ast>("GLookup", name); }
public static Ast LLookup(this Ast n, string name) { return n.AttValue<Ast>("LLookup", name); }
public static Types Type(this Ast n) { return n.AttValue<Types>("Type"); }
public static bool IsValid(this Ast n) { return n.AttValue<bool>("IsValid"); }
public static bool IsLValid(this Ast n) { return n.AttValue<bool>("IsLValid"); }
public static string SExpr(this Ast n) { return n.AttValue<string>("SExpr"); }
public static bool IsActive(this Ast n) { return n.AttValue<bool>("IsActive"); }
public static bool IsShown(this Ast n) { return n.AttValue<bool>("IsShown"); }
public static Ast FindActive(this Ast n, string name) { return n.AttValue<Ast>("FindActive", name); }
public static object Value(this Ast n) { return n.AttValue<object>("Value"); }
// Rewriting:
public static void SetValue(this Ast n, object value) { n.RewriteTerminal("value", value); }
// Support Attributes:
[AgRule("Root", "Form")] static Ast FormRoot(Ast n) {
return n;
}
[AgRule("ErrorQuestion", "Form")] static Ast FormErrorQuestion(Ast n) {
return n.GetBody().Child(1);
}
[AgRule("IsErrorQuestion", "Element")] static bool ElementIsErrorQuestion(Ast n) {
return n == n.ErrorQuestion();
}
// Name Analysis:
static Ast findL(string name, Ast l, int i) {
for (int j = 1; j <= i; j++) {
var r = l.Child(j).LLookup(name);
if (r != null) return r;
}
return null;
}
[AgRule("GLookup", "Form", Context = "Body")] static Ast FormGLookup(Ast n, string name) {
var ret = findL(name, n.Parent(), n.ChildIndex() - 1);
return (ret != null) ? ret : n.ErrorQuestion();
}
[AgRule("GLookup", "Group", Context = "Body")] static Ast GroupGLookup(Ast n, string name) {
var ret = findL(name, n.Parent(), n.ChildIndex() - 1);
return (ret != null) ? ret : n.Parent().GLookup(name);
}
[AgRule("LLookup", "Question")] static Ast QuestionLLookup(Ast n, string name) {
return n.GetName() == name ? n : null;
}
[AgRule("LLookup", "Group")] static Ast GroupLLookup(Ast n, string name) {
return findL(name, n.GetBody(), n.GetBody().NumChildren());
}
// Type Analysis:
[AgRule("Type", "OrdinaryQuestion")] static Types OrdinaryQuestionType(Ast n) {
var type = n.GetValueType();
return TypeToAcceptor(type) != null ? type : Types.ErrorType;
}
[AgRule("Type", "ComputedQuestion")] static Types ComputedQuestionType(Ast n) {
return n.GetExpression().Type();
}
[AgRule("Type", "Use")] static Types UseType(Ast n) {
return n.GLookup(n.GetName()).Type();
}
[AgRule("Type", "Constant")] static Types ConstantType(Ast n) {
return ValueToType(n.GetValue());
}
[AgRule("Type", "Computation")] static Types ComputationType(Ast n) {
var op = n.GetOperator();
var inType = Types.ErrorType;
var outType = Types.ErrorType;
var operands = n.GetOperands().Children();
if (op == "&&" || op == "//" || op == "not") {
inType = outType = Types.Boolean;
} else if (op == "=" || op == "<" || op == ">" || op == "<=" ||
op == ">=" || op == "!=") {
inType = Types.Number;
outType = Types.Boolean;
} else if (op == "+" || op == "-" || op == "*" || op == "/") {
inType = outType = Types.Number;
} else if (op == "string-append") {
inType = outType = Types.String;
} else if (op == "string=?" || op == "string<?" || op == "string>?" ||
op == "string<=?" || op == "string>=?") {
inType = Types.String;
outType = Types.Boolean;
}
if (operands.Any(x => ((Ast) x).Type() != inType)) return Types.ErrorType;
return outType;
}
// Well-formedness:
[AgRule("IsValid", "Form")] static bool FormIsValid(Ast n) {
return n.GetBody().Children(new Racr.Range(2)).All(x => ((Ast)x).IsValid());
}
[AgRule("IsValid", "Group")] static bool GroupIsValid(Ast n) {
return n.IsLValid() && n.GetBody().Children().All(x => ((Ast)x).IsValid());
}
[AgRule("IsValid", "Question")] static bool QuestionIsValid(Ast n) {
return n.IsLValid();
}
[AgRule("IsLValid", "Group")] static bool GroupIsLValid(Ast n) {
return n.GetExpression().Type() == Types.Boolean;
}
[AgRule("IsLValid", "Question")] static bool QuestionIsLValid(Ast n) {
if (n.Type() == Types.ErrorType) return false;
var prev = n.GLookup(n.GetName());
return prev.IsErrorQuestion() || n.Type() == prev.Type();
}
// Persistency:
[AgRule("SExpr", "Form")] static string FormSExpr(Ast n) {
return "(Form " + String.Join(" ",
n.GetBody().Children().Skip(1).Select(x => ((Ast)x).SExpr())) + ")";
}
[AgRule("SExpr", "Group")] static string GroupSExpr(Ast n) {
return "(If " + n.GetExpression().SExpr() + " " +
String.Join(" ", n.GetBody().Children().Select(x => ((Ast)x).SExpr())) + ")";
}
[AgRule("SExpr", "OrdinaryQuestion")] static string OrdinaryQuestionSExpr(Ast n) {
return "(?? '" + n.GetName() + " " + Lexer.EscapeString(n.GetLabel()) + " " + n.Type() + " " +
(n.GetValue() == ErrorValue ? "" : Lexer.EscapeValue(n.GetValue())) + ")";
}
[AgRule("SExpr", "ComputedQuestion")] static string ComputedQuestionSExpr(Ast n) {
return "(~? '" + n.GetName() + " " + Lexer.EscapeString(n.GetLabel()) +
" " + n.GetExpression().SExpr() + ")";
}
[AgRule("SExpr", "Use")] static string UseSExpr(Ast n) {
return "(~> '" + n.GetName() + ")";
}
[AgRule("SExpr", "Constant")] static string ConstantSExpr(Ast n) {
return "(~! " + Lexer.EscapeValue(n.GetValue()) + ")";
}
[AgRule("SExpr", "Computation")] static string ComputationSExpr(Ast n) {
return "(~~ " + n.GetOperator() + " " +
String.Join(" ", n.GetOperands().Children().Select(x => ((Ast)x).SExpr())) + ")";
}
// Interpretation:
[AgRule("FindActive", "Element")] static Ast ElementFindActive(Ast n, string name) {
var current = n.GLookup(name);
while (!current.IsActive()) current = current.GLookup(name);
return current;
}
[AgRule("IsActive", "Form")] static bool FormIsActive(Ast n) {
return true;
}
[AgRule("IsActive", "Group")] static bool GroupIsActive(Ast n) {
var v = n.GetExpression().Value();
return (v is bool) ? (bool) v : false;
}
[AgRule("IsActive", "Question")] static bool QuestionIsActive(Ast n) {
return n.IsErrorQuestion() || (
n.Parent().IsActive() &&
n.FindActive(n.GetName()).IsErrorQuestion());
}
[AgRule("IsShown", "Element")] static bool ElementIsShown(Ast n) {
return !n.IsErrorQuestion() && n.IsActive();
}
[AgRule("Value", "ComputedQuestion")] static object ComputedQuestionValue(Ast n) {
return n.GetExpression().Value();
}
[AgRule("Value", "Constant")] static object ConstantValue(Ast n) {
return n.Type() == Types.ErrorType ? ErrorValue : n.GetValue();
}
[AgRule("Value", "Use")] static object UseValue(Ast n) {
if (n.Type() == Types.ErrorType) return ErrorValue;
var active = n.FindActive(n.GetName());
return active.Type() == n.Type() ? active.Value() : ErrorValue;
}
[AgRule("Value", "OrdinaryQuestion")] static object OrdinaryQuestionValue(Ast n) {
if (n.Type() == Types.ErrorType) return ErrorValue;
var acceptor = TypeToAcceptor(n.Type()); // acceptor is never null
return acceptor(n.GetValue()) ? n.GetValue() : ErrorValue;
}
[AgRule("Value", "Computation")] static object ComputationValue(Ast n) {
if (n.Type() == Types.ErrorType) return ErrorValue;
var args = n.GetOperands().Children().Select(p => ((Ast) p).Value()).ToArray();
foreach (var arg in args) if (arg == ErrorValue) return ErrorValue;
try { return opTable[n.GetOperator()](args); }
catch { return ErrorValue; }
}
public static readonly object ErrorValue = new Object();
public enum Types { Boolean, Number, String, ErrorType }
public static Func<object,bool> TypeToAcceptor(Types t) {
switch (t) {
case Types.Boolean: return v => v is bool;
case Types.Number: return v => v is double;
case Types.String: return v => v is string;
default: return null;
}
}
public static Types ValueToType(object v) {
if (v is bool) return Types.Boolean;
if (v is double) return Types.Number;
if (v is string) return Types.String;
return Types.ErrorType;
}
private static readonly Dictionary<string, Func<object[], object>> opTable =
new Dictionary<string, Func<object[], object>>() {
{ "&&", (object[] l) => {
return l.All(x => (bool) x); } },
{ "//", (object[] l) => {
return l.Any(x => (bool) x); } },
{ "not", (object[] l) => {
return !l.All(x => (bool) x); } },
{ "+", (object[] l) => {
return l.Aggregate(0.0, (s, x) => s + (double) x); } },
{ "-", (object[] l) => {
return l.Skip(1).Aggregate((double)l[0], (s, x) => s - (double) x); } },
{ "*", (object[] l) => {
return l.Aggregate(1.0, (s, x) => s * (double) x); } },
{ "/", (object[] l) => {
return l.Skip(1).Aggregate((double)l[0], (s, x) => s / (double) x); } },
{ "!=", (object[] l) => {
var s = new HashSet<double>();
foreach (var x in l)
if (!s.Add((double) x)) return false;
return true;
} },
{ "=", (object[] l) => {
return l.All(x => (double) x == (double) l[0]); } },
{ "<", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if ((double)l[i-1] >= (double)l[i]) return false;
return true;
} },
{ "<=", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if ((double)l[i-1] > (double)l[i]) return false;
return true;
} },
{ ">", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if ((double)l[i-1] <= (double)l[i]) return false;
return true;
} },
{ ">=", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if ((double)l[i-1] < (double)l[i]) return false;
return true;
} },
{ "string=?", (object[] l) => {
return l.All(x => (string) x == (string) l[0]); } },
{ "string<?", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if (StringComparer.Ordinal.Compare(l[i-1], l[i]) >= 0) return false;
return true;
} },
{ "string<=?", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if (StringComparer.Ordinal.Compare(l[i-1], l[i]) > 0) return false;
return true;
} },
{ "string>?", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if (StringComparer.Ordinal.Compare(l[i-1], l[i]) <= 0) return false;
return true;
} },
{ "string>=?", (object[] l) => {
for (int i = 1; i < l.Length; i++)
if (StringComparer.Ordinal.Compare(l[i-1], l[i]) < 0) return false;
return true;
} },
{ "string-append", (object[] l) => {
return l.Aggregate("", (s, x) => s + (string) x); } },
};
}