Ejemplo n.º 1
0
	def apply_fncall_follower(self, factor, follower, local):
		if VERBOSE >= VERBOSITY_SHOW_FN_CALLS:
			print "Calling %s\n on %s"%(follower.prepr(), factor)
		
		func_name=factor
		factor = self.get_val(factor)
		if type(factor) is str:
			func_name = factor
			if factor in self.globals:
				factor = self.globals[factor]
			else:
				raise ExecuteError("Function %s does not exists", factor)
		
		if factor is None:
			if isinstance(func_name, VarRef):
				func_name = func_name.qualified_name()
			func_type = 'method' if '::' in func_name or '->' in func_name else 'function'
			self.report_error(constants.E_ERROR, 'Call to undefined %s %s() in %s on line %d'%(func_type, func_name, follower.filename, follower.line_num))
			raise ExecuteError("null is not callable.")
		elif isinstance(factor, phpbuiltins.builtin.builtin):
			args = [ self.visit(x, local) for x in follower.children[0].children]
			if VERBOSE >= VERBOSITY_SHOW_FN_CALLS:
				print "calling builtin %r with %r"%(factor, args)
			retval = factor(args, self, local)
		else:
			args = [ self.get_val(self.visit(x, local)) for x in follower.children[0].children]
			if VERBOSE >= VERBOSITY_SHOW_FN_CALLS:
				print "calling %r with %r"%(factor, args)
			retval = factor(*args, filename=follower.filename, line_num=follower.line_num)
		# print "Return value : ", retval
		return retval
Ejemplo n.º 2
0
	def apply_follower(self, factor, follower, local):
		# print follower
		follower_fn = "apply_%s_follower"%follower.name
		if hasattr(self, follower_fn):
			return getattr(self, follower_fn)(factor, follower, local)
		else:
			raise ExecuteError("Unknown follower for %s."%follower.prepr())
Ejemplo n.º 3
0
 def exec_primitive(self, node, local):
     subnode = node.children[0]
     if isinstance(subnode, parser.Token):
         token = subnode
         if token[0] in (parser.TOKEN_STRING, parser.TOKEN_NUMBER):
             return token[1]
         elif token[0] == parser.TOKEN_IDENTIFIER:
             lcaseid = token[1].lower()
             if lcaseid in primitives:
                 return primitives[lcaseid]
             else:
                 return VarRef(token[1], self, local)
         elif token[0] == parser.TOKEN_VARIABLE:
             # print "Var Ref %r on %r with %r"%(token[1], self, local.prepr())
             return VarRef(token[1], self, local)
         elif token[0] == parser.TOKEN_INTERPOLATED_STRING:
             text = ''.join([
                 coerce.to_string(
                     self.get_val(self.visit(x, local)) if isinstance(
                         x, compiler.TreeNode) else x) for x in token[1]
             ])
             return text
     elif isinstance(subnode, compiler.TreeNode):
         return self.visit(subnode, local)
     raise ExecuteError("invalid primitive %r" % subnode)
Ejemplo n.º 4
0
	def exec_classdef(self, node, local):
		print "%"*100
		cls_name, scls_name, body = node.children
		if scls_name and not scls_name[1] in local:
			raise ExecuteError("Undefined superclass %s"%scls_name[1])
		class_context = scope({}, local, name=cls_name[1])
		if body:
			body = self.visit(body, class_context)
		defined_class = phpclass.PHPClass(cls_name[1], local[scls_name[1]] if scls_name else None, body, class_context, filename=node.filename, line_num=node.line_num)
		#print "class %s defined"%defined_class
		#print "%"*100
		self.globals[defined_class.name] = defined_class
		return defined_class
Ejemplo n.º 5
0
	def apply_array_indexing_follower(self, factor, follower, local):
		index = self.get_val(self.visit(follower.children[0], local))
		if isinstance(factor, VarRef):
			return factor[index]
		else:
			raise ExecuteError("Cannot index %r "%factor)
Ejemplo n.º 6
0
	def set_val(self, var_ref, value):
		if isinstance(var_ref, VarRef):
			return var_ref.set(value)
		else:
			raise ExecuteError("Cannot assign %r to %r"%(var_ref, value))
Ejemplo n.º 7
0
	def visit_default(self, tree_node, local_dict=None):
		if VERBOSE >= VERBOSITY_SHOW_DEBUG:
			print tree_node.prepr()
		raise ExecuteError("Cannot visit node %r, visitor method 'exec_%s' not found.\n"%(tree_node.name, tree_node.name))