Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EvaluationContext.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using System.Threading;
17 using Irony.Parsing;
18 using Irony.Interpreter.Ast;
19 
20 namespace Irony.Interpreter {
21 
23  Ready,
24  Evaluating,
26  Aborted,
27  }
28 
29  public enum JumpType {
30  None = 0,
31  Break,
32  Continue,
33  Return,
34  Goto,
35  Exception,
36  }
37 
38  public partial class EvaluationContext {
39  public readonly int ThreadId;
41  public readonly bool LanguageCaseSensitive;
42  public readonly ValuesTable Globals;
43  public DataStack Data;
45  public JumpType Jump = JumpType.None;
47  //public Closure Tail;
48  public StackFrame TopFrame, CurrentFrame;
49  public StringBuilder OutputBuffer = new StringBuilder();
50  public int EvaluationTime;
51 
53  Runtime = runtime;
54  LanguageCaseSensitive = Runtime.Language.Grammar.CaseSensitive;
55  //Globals = new GlobalValuesTable(100, Symbols, LanguageCaseSensitive);
56  Globals = new ValuesTable(100);
57  CallDispatcher = new DynamicCallDispatcher(this);
58  ThreadId = Thread.CurrentThread.ManagedThreadId;
59  TopFrame = new StackFrame(this, Globals);
60  CurrentFrame = TopFrame;
61  Data = new DataStack();
62  Data.Init(runtime.Unassigned); //set LastPushedItem to unassigned
63  }
64 
65  public object LastResult {
66  get { return Data.LastPushedItem; }
67  }
68  public void ClearLastResult() {
69  Data.Init(Runtime.Unassigned);
70  }
71  public bool HasLastResult {
72  get { return LastResult != Runtime.Unassigned; }
73  }
74 
75  public void PushFrame(string methodName, AstNode node, StackFrame parent) {
76  CurrentFrame = new StackFrame(this, methodName, CurrentFrame, parent);
77  }
78  public void PopFrame() {
79  CurrentFrame = CurrentFrame.Caller;
80  }
81 
82  public bool TryGetValue(string name, out object value) {
83  if (CurrentFrame.Values.TryGetValue(name, out value)) return true;
84  var frame = CurrentFrame.Parent;
85  while (frame != null) {
86  if (frame.Values.TryGetValue(name, out value)) return true;
87  frame = frame.Parent;
88  }
89  value = null;
90  return false;
91  }
92 
93  public void SetValue(string name, object value) {
94  CurrentFrame.Values[name] = value;
95  }
96 
97  public void Write(string text) {
98  OutputBuffer.Append(text);
99  }
100  public void WriteLine(string text) {
101  OutputBuffer.AppendLine(text);
102  }
103 
104  //Throws generic exception; it supposed to be caught in AstNode.Evaluate method and it will wrap it into RuntimeException
105  // with node location added
106  public void ThrowError(string message, params object[] args) {
107  if (args != null && args.Length > 0)
108  message = string.Format(message, args);
109  throw new Exception(message);
110  }
111 
112  }//class
113 
114 }
void ThrowError(string message, params object[] args)
void PushFrame(string methodName, AstNode node, StackFrame parent)
void SetValue(string name, object value)
The DynamicCallDispatcher class is responsible for fast dispatching to the implementation based on ar...
EvaluationContext(LanguageRuntime runtime)
bool TryGetValue(string name, out object value)