Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
InterpreterContext.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Trees;
2 using Mercury.Syntax;
3 using System;
4 using System.Collections.Concurrent;
5 using System.Collections.Generic;
6 using System.Runtime.Caching;
7 
8 namespace Mercury.Interpreting
9 {
10 //=============================================================================
11 // InterpreterContext
12 //=============================================================================
13 
18  public abstract class InterpreterContext<T>
19  where T : class
20  {
21  //--[ Internal ]-----------------------------------------------------
22 
23  internal TreeEntry<T> hroot;
24  internal RuleEntry<T> cnode;
25 
26  internal Dictionary<string, Argument> bindings;
27  internal MemoryCache cache = null; // results cache
28 
29  internal string action;
30  internal ArgumentType reqtp;
31 
32  internal InterpreterContext<T> InternalClone(RuleEntry<T> current)
33  {
34  var cx = DeepClone();
35  cx.hroot = hroot;
36  cx.cnode = current;
37  cx.cache = cache;
38  return cx;
39  }
40 
41  internal void InternalCopy(InterpreterContext<T> cxtx)
42  { CopyFrom(cxtx); }
43 
44  internal void LogEntryEx(string hdr, string msg, params object[] args)
45  {
46  if (cnode == null) throw new InvalidOperationException();
47  cnode.Entries.Add(new TextEntry(hdr, msg, args));
48  }
49 
50  //--[ Constructors ]-------------------------------------------------
51 
56  { bindings = new Dictionary<string, Argument>(); }
57 
58  //--[ Properties ]---------------------------------------------------
59 
66  public TreeEntry<T> RootEntry { get { return hroot; } }
67 
68  //--[ Methods ]------------------------------------------------------
69 
72  public abstract InterpreterContext<T> DeepClone();
73 
75  public abstract void CopyFrom(InterpreterContext<T> other);
76 
80  public void LogEntry(string text, params object[] args)
81  { LogEntryEx(action, text, args); }
82  }
83 
84 //=============================================================================
85 // IInterpreterContextFactory
86 //=============================================================================
87 
89  public interface IInterpreterContextFactory
90  {
94  InterpreterContext<T> CreateContext<T>()
95  where T : class;
96  }
97 
98 //=============================================================================
99 // InternalContext
100 //=============================================================================
101 
102 //==<< InternalContext >>====================================================
103 
104  internal class InternalContext<T> : InterpreterContext<T>
105  where T : class
106  {
107  public override InterpreterContext<T> DeepClone()
108  { return new InternalContext<T>(); }
109 
110  public override void CopyFrom(InterpreterContext<T> other)
111  { }
112  }
113 
114 //==<< InternalContextFactory >>=============================================
115 
118  {
119  //--[ Interface Implementation ]-------------------------------------
120 
124  public InterpreterContext<T> CreateContext<T>()
125  where T : class
126  { return new InternalContext<T>(); }
127 
128  }
129 
130 }
Describes a rule that has been used to interpret a tree.
Definition: Entries.cs:92
Represents an entry for tree interpretation.
Definition: Entries.cs:52
Stores informations logged by actions or internal Evaluator.
Definition: Entries.cs:25
ArgumentType
Defines argument types using in the Mercury library
Definition: Argument.cs:11
InterpreterContext()
Initializes a new instance of the InterpreterContext{T} class.
void LogEntry(string text, params object[] args)
Logs an event into the history.
Interpreter Context class. Some languages may use it to override it and remember data when parse tree...
Produces contexts with no custom data