Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Evaluator.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace Mercury.Interpreting
4 {
5 //=============================================================================
6 // Evaluator
7 //=============================================================================
8 
15  internal sealed class Evaluator<T> : Argument
16  where T : class
17  {
18  //--[ Private fields ]-----------------------------------------------
19  private bool evaluated;
20  private Argument[] args;
21  private RecursiveEval<T> eval;
22 
23  private InterpreterAction<T> action;
24  private InterpreterContext<T> context;
25 
26  //--[ Constructors ]-------------------------------------------------
27 
28  public Evaluator(InterpreterAction<T> action, Argument[] args,
29  RecursiveEval<T> eval, InterpreterContext<T> context)
30  : base(action.ReturnType, null)
31  {
32  if (action == null) throw new ArgumentNullException("action");
33  if (args == null) throw new ArgumentNullException("args");
34  if (eval == null) throw new ArgumentNullException("eval");
35 
36  this.evaluated = false;
37  this.action = action;
38  this.args = args;
39  this.eval = eval;
40  this.context = context;
41  }
42 
43  //--[ Properties ]---------------------------------------------------
44 
45  internal override object Data
46  {
47  get
48  {
49  if (evaluated) return base.Data;
50  evaluated = true;
51 
52  var type = context.reqtp;
53  int tpix = 0;
54  for(int argix = 0; argix < args.Length; ++argix)
55  {
56  ArgumentType argtp = action.ArgumentTypes[tpix];
57 
58  if (argtp.HasFlag(ArgumentType.Lazy)) continue;
59 
60  context.reqtp = argtp;
61  if ((args[argix].Data == null) && (!argtp.HasFlag(ArgumentType.Null)))
62  return null;
63 
64  tpix = Math.Min(action.ArgumentTypes.Length - 1, ++tpix);
65  }
66 
67  context.reqtp = type;
68  context.action = action.Name;
69  context.LogEntryEx("(Evaluator)", "invoking {0}", action.Name);
70 
71  base.Data = action.Invoke(args, eval, context);
72 
73  context.LogEntryEx("(Evaluator)", "action {0} returned [{1}]",
74  action.Name, base.Data == null ? "(null)" : base.Data.ToString());
75  return base.Data;
76  }
77  }
78 
79  }
80 }
delegate T RecursiveEval< T >(Tree< Symbol > tree, InterpreterContext< T > context)
Delegate type for recursive evaluation (interpretation)
ArgumentType
Defines argument types using in the Mercury library
Definition: Argument.cs:11