1 using Mercury.Nucleus.Trees;
3 using System.Collections.Generic;
8 namespace Mercury.Interpreting
30 internal object[] args;
31 internal string mem = null;
33 internal TextEntry(
string actname,
string msg, params
object[] args)
43 public string ActionName {
get;
internal set; }
46 public string Text {
get {
return mem == null ? mem = string.Format(msg, args) : mem; } }
57 internal List<DataEntry> Rules {
get; set; }
59 internal TreeEntry(Tree<Symbol> t)
63 Rules =
new List<DataEntry>();
69 public Tree<Symbol>
Tree {
get;
private set; }
72 public bool Succeeded {
get;
internal set; }
75 public IReadOnlyList<DataEntry> RuleEntries {
get {
return Rules; } }
85 public T MemoizedValue {
get;
internal set; }
97 internal List<DataEntry> Entries {
get; set; }
106 Entries =
new List<DataEntry>();
115 public bool Match {
get;
internal set; }
118 public T Value {
get;
internal set; }
124 public bool Error {
get;
internal set; }
133 public IReadOnlyList<DataEntry> SubEntries {
get {
return Entries; } }
146 public static class EntryWriter<T>
153 private static void WriteDEntry(
int lvl,
DataEntry dentry, TextWriter o)
156 if (tentry != null) WriteDEntry(lvl, tentry, o);
157 else WriteDEntry(lvl, dentry as
TextEntry, o);
160 private static void WriteDEntry(
int lvl,
TreeEntry<T> tentry, TextWriter o)
162 var pad =
new string(
'\t', lvl);
163 o.WriteLine(
"{0}>> {1}", pad, tentry.Tree);
165 WriteREntry(lvl + 1, rentry, o);
167 o.WriteLine(
"{0}<< {{{1}}}", pad, tentry.Succeeded ?
"SUCC" :
"FAIL");
171 private static void WriteDEntry(
int lvl,
TextEntry tentry, TextWriter o)
173 var pad =
new string(
'\t', lvl);
174 o.WriteLine(
"{0}:: {1}: {2}", pad, tentry.ActionName, tentry.Text);
181 private static void WriteREntry(
int lvl,
DataEntry dentry, TextWriter o)
184 if (rentry != null) WriteREntry(lvl, rentry, o);
188 private static void WriteREntry(
int lvl,
MemoEntry<T> mentry, TextWriter o)
190 var pad =
new string(
'\t', lvl);
191 o.WriteLine(
"{0}~~ {{MEMOIZED}} <{1}>", pad,
192 mentry.MemoizedValue == null ?
"(null)" : mentry.MemoizedValue.ToString());
195 private static void WriteREntry(
int lvl,
RuleEntry<T> rentry, TextWriter o)
197 var pad =
new string(
'\t', lvl);
198 o.WriteLine(
"{0}-> {1}", pad, rentry.RewriteRule);
200 o.WriteLine(
"{0} {{<EXCEPTION>}}", pad);
202 o.Write(
"{0} {{{1}}} ", pad, rentry.Match ?
"MATCH" :
"no match");
206 int max = rentry.Instances.Select(x => x.Var.Name.Length).Max();
207 var str =
"{0,-" + max +
"}";
210 foreach (var instance
in rentry.
Instances)
212 o.Write(ix++ == 0 ?
"{1} " :
"{0} ~~> ", pad,
"~~>");
213 var obj = (instance.Var is
Wildcard)
214 ?
"{" +
string.Join(
"; ", (instance.Value as Tree<Symbol>[]).Select(x => x.ToString(2))) +
"}"
215 : (instance.Value as Tree<Symbol>).ToString(2);
216 o.WriteLine(str +
" = {1}", instance.Var.Name, obj);
223 WriteDEntry(lvl + 1, tentry, o);
226 o.WriteLine(
"{0}<- ret <{1}>", pad, rentry.Value == null ?
"(null)" : rentry.Value.ToString());
237 { WriteDEntry(0, root, o); }
Entry base (dummy) interface.
Describes a rule that has been used to interpret a tree.
Represents an entry for tree interpretation.
Represents memoized value
Stores informations logged by actions or internal Evaluator.
bool Match
Indicates whether the LHS of rule matched the tree.
InstanceList Instances
List of instances or null if rule did not match.
IReadOnlyList< DataEntry > SubEntries
List of TextEntries or TreeEntries that occured while evaluating action of this rule.
IReadOnlyList< DataEntry > RuleEntries
List of rule entries that were tried.
static void WriteEntries(TreeEntry< T > root, TextWriter o)
Writes the entries into the stream.
int Count
Number of elements in the list
An element that matches zero or more values. Can be used as a variable, but the actioncall must be of...
Represents the list of instances
bool Error
Indicates whether an exception has been thrown when evaluating the rule action.