ScriptStack 1.0.4
Loading...
Searching...
No Matches
Operand.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
7
9{
10
21
22 public class Operand
23 {
24
25 #region Private Variables
26
27 private OperandType m_operandType;
28 private object m_objectValue;
29 private object m_objectIndex;
30
31 #endregion
32
33 #region Private Methods
34
35 private Operand(OperandType operandType, object objectValue, object objectIndex)
36 {
37 m_operandType = operandType;
38 m_objectValue = objectValue;
39 m_objectIndex = objectIndex;
40 }
41
42 private string ToString(object objectValue)
43 {
44 if (objectValue.GetType() == typeof(string))
45 return "\"" + objectValue + "\"";
46 else
47 return objectValue.ToString();
48 }
49
50 #endregion
51
52 #region Public Static Methods
53
54 public static Operand Literal(object val)
55 {
56 return new Operand(OperandType.Literal, val, null);
57 }
58
59 public static Operand Variable(string identifier)
60 {
61 return new Operand(OperandType.Variable, identifier, null);
62 }
63
64 public static Operand MemberVariable(string identifier, object val)
65 {
66 return new Operand(OperandType.Member, identifier, val);
67 }
68
69 public static Operand CreatePointer(string identifier, string pointer)
70 {
71 return new Operand(OperandType.Pointer, identifier, pointer);
72 }
73
74 public static Operand AllocateInstructionPointer(Instruction instruction)
75 {
76 return new Operand(OperandType.InstructionPointer, instruction, null);
77 }
78
79 public static Operand AllocateFunctionPointer(Function function)
80 {
81 return new Operand(OperandType.FunctionPointer, function, null);
82 }
83
84 public static Operand AllocateRoutinePointer(Routine routine)
85 {
86 return new Operand(OperandType.RoutinePointer, routine, null);
87 }
88
89 #endregion
90
91 #region Public Methods
92
93 private string ToLiteral(string input)
94 {
95
96 var literal = new StringBuilder(input.Length + 2);
97
98 foreach (var c in input)
99 {
100
101 switch (c)
102 {
103
104 case '\"':
105 literal.Append("\\\"");
106 break;
107
108 case '\a':
109 literal.Append("\\a");
110 break;
111
112 case '\b':
113 literal.Append("\\b");
114 break;
115
116 case '\n':
117 literal.Append("\\n");
118 break;
119
120 case '\r':
121 literal.Append("\\r");
122 break;
123
124 case '\t':
125 literal.Append("\\t");
126 break;
127
128 default:
129 if (char.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.Control)
130 literal.Append(c);
131 else
132 {
133 literal.Append(@"\u");
134 literal.Append(((ushort)c).ToString("x4"));
135 }
136 break;
137
138 }
139
140 }
141
142 return literal.ToString();
143
144 }
145
146 public override string ToString()
147 {
148
149 switch (m_operandType)
150 {
151
152 case OperandType.Literal:
153 {
154
155 // \todo improve
156 if (m_objectValue.GetType().ToString() == "System.String")
157 return ToLiteral((string)m_objectValue);
158
159 else if (m_objectValue.GetType().ToString() == "System.Char")
160 return ToLiteral("" + (char)m_objectValue);
161
162 else
163 return ToString(m_objectValue);
164
165 }
166
167 case OperandType.Variable:
168 return m_objectValue.ToString();
169
170 case OperandType.Member:
171 return m_objectValue + "[" + ToString(m_objectIndex) + "]";
172
173 case OperandType.Pointer:
174 return m_objectValue + "[" + m_objectIndex + "]";
175
176 case OperandType.InstructionPointer:
177 return "[" + ((Instruction) m_objectValue).Address.ToString("X8") + "]";
178
179 case OperandType.FunctionPointer:
180 {
181 Function scriptFunction = (Function)m_objectValue;
182 return "<" + scriptFunction.Name + "@" + scriptFunction.EntryPoint.Address.ToString("X8") + ">";
183 }
184
185 case OperandType.RoutinePointer:
186 return m_objectValue.ToString();
187
188 default:
189 return ToLiteral(m_operandType.ToString());
190
191 }
192 }
193
194 #endregion
195
196 #region Public Properties
197
199 {
200 get { return m_operandType; }
201 set { m_operandType = value; }
202 }
203
204 public object Value
205 {
206 get { return m_objectValue; }
207 }
208
209 public object Member
210 {
211 get
212 {
213 if (m_operandType != OperandType.Member)
214 throw new ExecutionException("Error in member access.");
215 return m_objectIndex;
216 }
217 set
218 {
219 if (m_operandType != OperandType.Member)
220 throw new ExecutionException("Error in member access.");
221 m_objectIndex = value;
222 }
223 }
224
225 public string Pointer
226 {
227 get
228 {
229 if (m_operandType != OperandType.Pointer)
230 throw new ExecutionException("Error in array access.");
231 return (string) m_objectIndex;
232 }
233 set
234 {
235 if (m_operandType != OperandType.Pointer)
236 throw new ExecutionException("Error in array access.");
237 m_objectIndex = value;
238 }
239 }
240
242 {
243 get
244 {
245 if (m_operandType != OperandType.InstructionPointer)
246 throw new ParserException("Error in instruction access.");
247
248 return (Instruction) m_objectValue;
249 }
250 set
251 {
252 if (m_operandType != OperandType.InstructionPointer)
253 throw new ParserException("Error in instruction access.");
254
255 m_objectValue = value;
256 }
257 }
258
260 {
261 get
262 {
263 if (m_operandType != OperandType.FunctionPointer)
264 throw new ParserException("Error in function call.");
265
266 return (Function)m_objectValue;
267 }
268 set
269 {
270 if (m_operandType != OperandType.FunctionPointer)
271 throw new ParserException("Error in function call.");
272
273 m_objectValue = value;
274 }
275 }
276
278 {
279
280 get
281 {
282 if (m_operandType != OperandType.RoutinePointer)
283 throw new ParserException("Error in routine access.");
284
285 return (Routine)m_objectValue;
286 }
287 set
288 {
289 if (m_operandType != OperandType.RoutinePointer)
290 throw new ParserException("Error in routine access.");
291
292 m_objectValue = value;
293 }
294
295 }
296
297 #endregion
298
299 }
300
301}
A function, forward declared in a script.
Definition Function.cs:15
An instruction in a virtual intermediate language.
Instruction InstructionPointer
Definition Operand.cs:242
static Operand MemberVariable(string identifier, object val)
Definition Operand.cs:64
static Operand Literal(object val)
Definition Operand.cs:54
override string ToString()
Definition Operand.cs:146
static Operand AllocateInstructionPointer(Instruction instruction)
Definition Operand.cs:74
static Operand CreatePointer(string identifier, string pointer)
Definition Operand.cs:69
static Operand AllocateFunctionPointer(Function function)
Definition Operand.cs:79
static Operand AllocateRoutinePointer(Routine routine)
Definition Operand.cs:84
static Operand Variable(string identifier)
Definition Operand.cs:59
A Routine is an abstract representation of a method.
Definition Routine.cs:70