예제 #1
0
파일: converter.py 프로젝트: plusgood/lithp
	def make_func_bodies(self):
		"""Extracts bodies of lambda functions
		(as opposed to their headers). These bodies
		will then be translated to a series of function
		calls.
		"""
		for name in self.func_dict:
			tok = Lexer(self.func_dict[name]).get_tokens()
			end = tok.match_paren(0)
			header_end = tok.match_paren(2)

			if tok[header_end+2] == '(': #Function returns another function
				start = body.match_paren(header_end+2)+3
			else: #Function returns a concrete type
				start = header_end+3
			
			self.func_bodies[name] = tok[start:end].get_joined()

		self.func_bodies['main'] = self.main
예제 #2
0
파일: converter.py 프로젝트: plusgood/lithp
	def make_func_declarations(self):
		"""Creates function declarations with parameter
		types and return types. Higher order functions
		are supported. Function declarations are also
		used as function signatures.

		Precondition: make_func_dict must have been called
		"""

		for name in self.func_dict:
			body = Lexer(self.func_dict[name]).get_tokens()
			i = body.index('\\')  + 1 #Start of parameters
			j = body.match_paren(i)
			param_tokens = body[i + 1: j] #Stuff inside parentheses
			#			print "param list:", param_tokens

			params = self.split_params(param_tokens)
			params = map(lambda n: n.split(':'), params)
			#params is now [[<name>,<type>],...]
			c_types = map(lambda n: self.convert_type(*n), params)
			#			print c_types

			return_type = ''
			#     +2 to skip over ")" and ":"
			if body[j+2] == '(': #Function returns another function
				#                                  +3 for [")","->","<type>"]
				for x in xrange(j+2, body.match_paren(j+2)+3):
					return_type += body[x]
			else: #Function returns a concrete type
				return_type = body[j+2] #+2 to skip over ")" and ":"

			func_type = self.convert_type(name, return_type)
			#			print "params", params
			#			print "c_types", c_types
			#while True:exec raw_input() in globals(), locals()
			self.cpp_declarations[name] = func_type + '(' + ', '.join(c_types) + ')'

		self.cpp_declarations['main'] = 'int main()' #actually this isn't used