/
Scope.py
97 lines (86 loc) · 3.27 KB
/
Scope.py
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
from __future__ import print_function
# Copyright (C) 2007-2010 Samuel Abels.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from copy import deepcopy
from Exscript.parselib import Token
class Scope(Token):
def __init__(self, name, lexer, parser, parent = None, *args, **kwargs):
Token.__init__(self, name, lexer, parser, parent)
self.variables = kwargs.get('variables', {})
self.children = []
self.exit_requested = 0
for key in self.variables:
if key.find('.') < 0 and not key.startswith('_'):
assert type(self.variables[key]) == type([])
def exit_request(self):
self.exit_requested = 1
def define(self, **kwargs):
if self.parent is not None:
return self.parent.define(**kwargs)
for key in kwargs:
if key.find('.') >= 0 or key.startswith('_') \
or type(kwargs[key]) == type([]):
self.variables[key] = kwargs[key]
else:
self.variables[key] = [kwargs[key]]
def define_object(self, **kwargs):
self.variables.update(kwargs)
def is_defined(self, name):
if name in self.variables:
return 1
if self.parent is not None:
return self.parent.is_defined(name)
return 0
def get_vars(self):
"""
Returns a complete dict of all variables that are defined in this
scope, including the variables of the parent.
"""
if self.parent is None:
vars = {}
vars.update(self.variables)
return vars
vars = self.parent.get_vars()
vars.update(self.variables)
return vars
def copy_public_vars(self):
"""
Like get_vars(), but does not include any private variables and
deep copies each variable.
"""
vars = self.get_vars()
vars = dict([k for k in vars.iteritems() if not k[0].startswith('_')])
return deepcopy(vars)
def get(self, name, default = None):
if name in self.variables:
return self.variables[name]
if self.parent is None:
return default
return self.parent.get(name, default)
def value(self, context):
result = 1
for child in self.children:
result = child.value(context)
return result
def dump(self, indent = 0):
print((' ' * indent) + self.name, 'start')
for child in self.children:
child.dump(indent + 1)
print((' ' * indent) + self.name, 'end')
def dump1(self):
if self.parent is not None:
self.parent.dump1()
return
print("Scope:", self.variables)