/
context.h
165 lines (138 loc) · 4.84 KB
/
context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2004-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBDENG2_CONTEXT_H
#define LIBDENG2_CONTEXT_H
#include "../Evaluator"
#include "../Record"
namespace de {
class Statement;
class Process;
/**
* Entry in the process's call stack.
*
* @ingroup script
*/
class DENG2_PUBLIC Context
{
public:
/// Attempting a jump when there is no suitable target (continue or break). @ingroup errors
DENG2_ERROR(JumpError);
/// There is no instance scope defined for the context. @ingroup errors
DENG2_ERROR(UndefinedScopeError);
enum Type {
BaseProcess,
GlobalNamespace,
FunctionCall
};
public:
/**
* Constructor.
*
* @param type Type of the execution context.
* @param owner Process that owns the context.
* @param globals Optionally a global namespace. Lookups will stop here.
*/
Context(Type type, Process *owner, Record *globals = 0);
/// Determines the type of the execution context.
Type type();
/// Returns the process that owns this context.
Process &process();
/// Returns the namespace of the context.
Record &names();
/// Returns the expression evaluator of the context.
Evaluator &evaluator();
/**
* Start the execution of a series of statements.
*
* @param statement The first statement to execute.
* @param flow The statement to continue program flow when no more
* statements follow the current one.
* @param jumpContinue The statement to jump to when a "continue"
* statement is encountered.
* @param jumpBreak The statement to jump to when a "break" statement
* is encountered.
*/
void start(Statement const *statement,
Statement const *flow = NULL,
Statement const *jumpContinue = NULL,
Statement const *jumpBreak = NULL);
/**
* Clears the evaluator and control flow. Does not empty the namespace.
* This needs to be called if the process is aborted.
*/
void reset();
/// Returns the currently executed statement.
/// @return Statement, or @c NULL if no control flow information exists.
Statement const *current();
/**
* Execute the current statement.
*
* @return @c false if there are no more statements to execute.
*/
bool execute();
/**
* Proceed to the next statement as dictated by the control flow.
*/
void proceed();
/**
* Jump to the topmost continue target in the control flow stack.
* If there are no continue targets available, an error will be
* thrown.
*/
void jumpContinue();
/**
* Jump to the topmost break target in the control flow stack.
* If there are no break targets available, an error will be
* thrown.
*
* @param count Number of times to break out. Allows breaking
* out of nested loops.
*/
void jumpBreak(duint count = 1);
/// Returns the current iteration value of the context.
Value *iterationValue();
/**
* Sets the iteration value of the context.
*
* @param value Value to be iterated within the context.
*/
void setIterationValue(Value *value);
/**
* Sets the instance scope of the context. This is equivalent to the value
* of the "self" variable, however only used for native function calls.
*
* @param scope Value that specifies the instance whose scope the context
* is being evaluated in. Ownership taken.
*/
void setInstanceScope(Value *scope);
/**
* Returns the current instance scope. A scope must exist if this is called.
*/
Value &instanceScope() const;
/**
* Returns the throwaway variable. This can be used for dumping
* values that are not needed. For instance, the weak assignment operator
* will use this when the identifier already exists.
*/
Variable &throwaway();
private:
DENG2_PRIVATE(d)
};
} // namespace de
#endif /* LIBDENG2_CONTEXT_H */