language

some fools attempt at an interpreted language
Log | Files | Refs | README

namespace.py (2588B)


      1 
      2 class Namespace():
      3 	# Namespace data structure:
      4 	# {
      5 	#  "<name>"   : [ <index, int>, None ],
      6 	#  "<object>" : [ <index, int>, {"property" : [ <index, int>, None ]}
      7 	# }
      8 	def __init__(self):
      9 		self.sc = 0
     10 		self.ns = [{}]
     11 		self.scopes = [self.ns]
     12 		self.stack  = []
     13 
     14 		self.t = None
     15 		self.tShared = []
     16 
     17 	def name_dec(self, name):
     18 		if name in self.ns[self.sc].keys():
     19 			print("Declaring name that already exists!")
     20 		else:
     21 			self.ns[self.sc][name] = [len(self.ns[self.sc].keys()) + 1, None]
     22 
     23 	def inc_scope(self):
     24 		self.ns.append({})
     25 		self.sc += 1
     26 
     27 	def dec_scope(self):
     28 		self.ns.pop()
     29 		self.sc -= 1
     30 	
     31 	def push(self):
     32 		self.stack.append([self.sc, self.ns])
     33 		self.sc = 0
     34 		self.ns = [{}]
     35 
     36 	def pop(self):
     37 		tmp = self.stack.pop()
     38 		self.sc = tmp[0]
     39 		self.ns = tmp[1]
     40 
     41 	# New namespace
     42 	def target(self, name):
     43 		self.t = name
     44 		self.push()
     45 
     46 		self.sc = 0
     47 		self.ns = [{}]
     48 
     49 	# Releases target
     50 	def release(self):
     51 		tmp = self.ns[0]
     52 		for name in self.tShared:
     53 			self.ns[self.sc][name][1] = tmp
     54 
     55 		self.pop()
     56 		self.ns[0][self.t][1] = tmp
     57 		self.t = None
     58 		self.tShared = []
     59 	
     60 	def copy(self, new_name, name):
     61 		if self.t == name:
     62 			self.ns[self.sc][new_name][1] = self.ns[0]
     63 		else:
     64 			self.ns[self.sc][new_name][1] = self.obj_resolve(name)
     65 
     66 	def is_obj(self, name):
     67 		return False	
     68 
     69 	# Resolves name into object
     70 	def obj_resolve(self, name):
     71 		rv = None
     72 		if name in self.ns[0].keys():
     73 			rv = self.ns[0][name][1]
     74 		else:
     75 			for namespace in self.scopes:
     76 				for names in namespace:
     77 					if name in names.keys() and rv == None:
     78 						rv = names[name][1]
     79 					elif rv != None:
     80 						print("{} was found, but found again".format(name))
     81 
     82 		return rv
     83 
     84 	def resolve_with_obj(self, parent, name):
     85 		rv = None
     86 
     87 		if parent.is_property == True:
     88 			obj = self.resolve_with_obj(parent.parent, parent.name)
     89 		else:
     90 			obj = self.obj_resolve(parent.name)
     91 
     92 		if type(obj) == dict:
     93 			if name in obj:
     94 				rv = obj[name]
     95 		elif type(obj) == list and obj[1] != None:
     96 			if name in obj[1]:
     97 				rv = obj[1][name]
     98 		elif type(obj) == type(None):
     99 			print("Object referenced before definition: {}.{}".format(
    100 				parent.name, name)
    101 			)
    102 
    103 		return rv
    104 
    105 	# Resolves name into scope and address
    106 	def resolve(self, name):
    107 		rv = None
    108 		if name in self.ns[0].keys():
    109 			rv = [1, self.ns[0][name][0]]
    110 		elif name in self.ns[-1].keys():
    111 			rv = [0, self.ns[-1][name][0]]
    112 		else:
    113 			for x, names in enumerate(self.scopes):
    114 				if name in names[0].keys():
    115 					rv = [((x + 1) << 1) + 1, names[0][name][0]]
    116 				elif name in names[-1].keys():
    117 					rv = [((x + 1) << 1) + 0, names[-1][name][0]]
    118 		return rv