1 /*
2    AngelCode Scripting Library
3    Copyright (c) 2003-2015 Andreas Jonsson
4 
5    This software is provided 'as-is', without any express or implied
6    warranty. In no event will the authors be held liable for any
7    damages arising from the use of this software.
8 
9    Permission is granted to anyone to use this software for any
10    purpose, including commercial applications, and to alter it and
11    redistribute it freely, subject to the following restrictions:
12 
13    1. The origin of this software must not be misrepresented; you
14       must not claim that you wrote the original software. If you use
15       this software in a product, an acknowledgment in the product
16       documentation would be appreciated but is not required.
17 
18    2. Altered source versions must be plainly marked as such, and
19       must not be misrepresented as being the original software.
20 
21    3. This notice may not be removed or altered from any source
22       distribution.
23 
24    The original version of this library can be located at:
25    http://www.angelcode.com/angelscript/
26 
27    Andreas Jonsson
28    andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_scriptnode.cpp
34 //
35 // A node in the script tree built by the parser for compilation
36 //
37 
38 
39 
40 #include "as_scriptnode.h"
41 #include "as_scriptengine.h"
42 
43 BEGIN_AS_NAMESPACE
44 
asCScriptNode(eScriptNode type)45 asCScriptNode::asCScriptNode(eScriptNode type)
46 {
47 	nodeType    = type;
48 	tokenType   = ttUnrecognizedToken;
49 	tokenPos    = 0;
50 	tokenLength = 0;
51 
52 	parent      = 0;
53 	next        = 0;
54 	prev        = 0;
55 	firstChild  = 0;
56 	lastChild   = 0;
57 }
58 
Destroy(asCScriptEngine * engine)59 void asCScriptNode::Destroy(asCScriptEngine *engine)
60 {
61 	// Destroy all children
62 	asCScriptNode *node = firstChild;
63 	asCScriptNode *nxt;
64 
65 	while( node )
66 	{
67 		nxt = node->next;
68 		node->Destroy(engine);
69 		node = nxt;
70 	}
71 
72 	// Return the memory to the memory manager
73 	engine->memoryMgr.FreeScriptNode(this);
74 }
75 
CreateCopy(asCScriptEngine * engine)76 asCScriptNode *asCScriptNode::CreateCopy(asCScriptEngine *engine)
77 {
78 	void *ptr = engine->memoryMgr.AllocScriptNode();
79 	if( ptr == 0 )
80 	{
81 		// Out of memory
82 		return 0;
83 	}
84 
85 	new(ptr) asCScriptNode(nodeType);
86 
87 	asCScriptNode *node = reinterpret_cast<asCScriptNode*>(ptr);
88 	node->tokenLength = tokenLength;
89 	node->tokenPos    = tokenPos;
90 	node->tokenType   = tokenType;
91 
92 	asCScriptNode *child = firstChild;
93 	while( child )
94 	{
95 		node->AddChildLast(child->CreateCopy(engine));
96 		child = child->next;
97 	}
98 
99 	return node;
100 }
101 
SetToken(sToken * token)102 void asCScriptNode::SetToken(sToken *token)
103 {
104 	tokenType   = token->type;
105 }
106 
UpdateSourcePos(size_t pos,size_t length)107 void asCScriptNode::UpdateSourcePos(size_t pos, size_t length)
108 {
109 	if( pos == 0 && length == 0 ) return;
110 
111 	if( tokenPos == 0 && tokenLength == 0 )
112 	{
113 		tokenPos = pos;
114 		tokenLength = length;
115 	}
116 	else
117 	{
118 		if( tokenPos > pos )
119 		{
120 			tokenLength = tokenPos + tokenLength - pos;
121 			tokenPos = pos;
122 		}
123 
124 		if( pos + length > tokenPos + tokenLength )
125 		{
126 			tokenLength = pos + length - tokenPos;
127 		}
128 	}
129 }
130 
AddChildLast(asCScriptNode * node)131 void asCScriptNode::AddChildLast(asCScriptNode *node)
132 {
133 	// We might get a null pointer if the parser encounter an out-of-memory situation
134 	if( node == 0 ) return;
135 
136 	if( lastChild )
137 	{
138 		lastChild->next = node;
139 		node->next      = 0;
140 		node->prev      = lastChild;
141 		node->parent    = this;
142 		lastChild       = node;
143 	}
144 	else
145 	{
146 		firstChild   = node;
147 		lastChild    = node;
148 		node->next   = 0;
149 		node->prev   = 0;
150 		node->parent = this;
151 	}
152 
153 	UpdateSourcePos(node->tokenPos, node->tokenLength);
154 }
155 
DisconnectParent()156 void asCScriptNode::DisconnectParent()
157 {
158 	if( parent )
159 	{
160 		if( parent->firstChild == this )
161 			parent->firstChild = next;
162 		if( parent->lastChild == this )
163 			parent->lastChild = prev;
164 	}
165 
166 	if( next )
167 		next->prev = prev;
168 
169 	if( prev )
170 		prev->next = next;
171 
172 	parent = 0;
173 	next = 0;
174 	prev = 0;
175 }
176 
177 END_AS_NAMESPACE
178 
179