Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
InterpreterAction.cs
Go to the documentation of this file.
1 using Mercury.Exceptions;
2 using Mercury.Nucleus.Trees;
3 using Mercury.Syntax;
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Linq;
8 
9 namespace Mercury.Interpreting
10 {
11 //=============================================================================
12 // Utilities
13 //=============================================================================
14 
22  public delegate T RecursiveEval<T>(Tree<Symbol> tree, InterpreterContext<T> context)
23  where T : class;
24 
30  //public delegate T ConvertFunc<T>(Argument[] args, RecursiveEval<T> eval,
31  // InterpreterContext<T> context)
32  // where T : class;
33 
38  //public delegate T ConcatFunc<T>(T[] args)
39  // where T : class;
40 
41 //=============================================================================
42 // IInterpreterAction
43 //=============================================================================
44 
46  public abstract class InterpreterAction<T>
47  where T : class
48  {
49  //--[ Protected methods ]--------------------------------------------
50 
51  protected void CheckDefinition()
52  {
53  if (string.IsNullOrWhiteSpace(Name))
54  throw new InvalidActionException(this.ToString(), "invalid action name");
55 
56  if ((Arity.Item1 < 0) || (Arity.Item2 < 0) || (Arity.Item1 > Arity.Item2))
57  throw new InvalidActionException(this.ToString(), "invalid arity ({0}, {1})", Arity.Item1, Arity.Item2);
58 
59  if ((ArgumentTypes.Length == 0) && ((Arity.Item1 != 0) || (Arity.Item2 != 0)))
60  throw new InvalidActionException(this.ToString(), "argument type list is empty");
61 
62  for (int ix = 0; ix < ArgumentTypes.Length; ++ix)
63  if ((ArgumentTypes[ix] & ArgumentType.Any) == ArgumentType.None)
64  throw new InvalidActionException(this.ToString(), "argument type {0}, {1} is not a valid type", ix, ArgumentTypes[ix].ToString());
65  }
66 
67  //--[ Constructors ]-------------------------------------------------
68 
94  public InterpreterAction(string name, Tuple<int, int> arity, ArgumentType[] argtypes,
95  bool allowswild = true)
96  {
97  if (name == null) throw new ArgumentNullException("name");
98  if (arity == null) throw new ArgumentNullException("arity");
99  if (argtypes == null) throw new ArgumentNullException("argtypes");
100 
101  Name = name;
102  Arity = arity;
103  ArgumentTypes = argtypes;
104  ReturnType = ArgumentType.TValue;
105  AllowsWildcard = allowswild;
106 
107  CheckDefinition();
108  }
109 
110  //--[ Properties ]---------------------------------------------------
111 
113  public string Name { get; internal set; }
114 
116  public Tuple<int, int> Arity { get; internal set; }
117 
119  public ArgumentType ReturnType { get; internal set; }
120 
126  public ArgumentType[] ArgumentTypes { get; internal set; }
127 
132  public bool AllowsWildcard { get; internal set; }
133 
144  public abstract object Invoke(Argument[] arguments, RecursiveEval<T> eval,
145  InterpreterContext<T> context);
146  }
147 
148 //=============================================================================
149 // InterpreterAction
150 //=============================================================================
151 
157  public abstract class InterpreterAction<T, U> : InterpreterAction<T>
158  where T : class
159  {
160  //--[ Constructor ]--------------------------------------------------
161 
170  public InterpreterAction(string name, Tuple<int, int> arity,
171  ArgumentType[] argtypes, bool allowswild = true)
172  : base(name, arity, argtypes, allowswild)
173  {
174  ReturnType = Defaults.TypeToArgType<T, U>();
175  var rtp = Defaults.TypeToArgType<T, T>();
176 
177  if ((ReturnType & rtp) != ArgumentType.None)
178  ReturnType |= ArgumentType.TValue;
179 
180  CheckDefinition();
181  }
182 
183  //--[ Methods ]------------------------------------------------------
184 
190  public abstract U Evaluate(Argument[] arguments, RecursiveEval<T> eval,
191  InterpreterContext<T> context);
192 
193  //--[ Interface implementation ]-------------------------------------
194 
195  #region [ IInterpreterAction ]
196 
197  public override object Invoke(Argument[] arguments, RecursiveEval<T> eval,
198  InterpreterContext<T> context)
199  { return Evaluate(arguments, eval, context); }
200 
201  #endregion
202 
203  }
204 
205 //=============================================================================
206 // GenericAction
207 //=============================================================================
208 
214  public abstract class GenericAction<T> : InterpreterAction<T>
215  where T : class
216  {
217  //--[ Constructors ]-------------------------------------------------
218 
226  public GenericAction(string name, Tuple<int, int> arity,
227  ArgumentType[] argtypes, bool allowswild = true)
228  : base(name, arity, argtypes, allowswild)
229  { ReturnType = ArgumentType.None; }
230 
231  //--[ Methods ]------------------------------------------------------
232 
237  public abstract ArgumentType InferReturnType(ArgumentType expected, ArgumentType[] actual);
238  }
239 
240 }
InterpreterAction(string name, Tuple< int, int > arity, ArgumentType[] argtypes, bool allowswild=true)
Initializes a new instance of the InterpreterAction{T,U} class. See Mercury.Interpreting.InterpreterAction{T} for better explanation.
delegate T RecursiveEval< T >(Tree< Symbol > tree, InterpreterContext< T > context)
Delegate type for recursive evaluation (interpretation)
An exception representing a problem with Mercury.Interpreting.InterpreterAction{T} class...
override object Invoke(Argument[] arguments, RecursiveEval< T > eval, InterpreterContext< T > context)
Evaluates the arguments. It is guaranteed to have a valid number of arguments (specified by Arity) a...
ArgumentType
Defines argument types using in the Mercury library
Definition: Argument.cs:11
InterpreterAction(string name, Tuple< int, int > arity, ArgumentType[] argtypes, bool allowswild=true)
Initializes a new instance of the InterpreterAction{T} class. The target application can inherit from...
GenericAction(string name, Tuple< int, int > arity, ArgumentType[] argtypes, bool allowswild=true)
Initializes a GenericAction{T} instance.
Represents an argument passed to the Mercury.Interpreting.InterpreterAction{T}, the actual parameter ...
Definition: Argument.cs:58
Interpreter Context class. Some languages may use it to override it and remember data when parse tree...