当前位置: 首页>>代码示例>>Python>>正文


Python Lexer.lexe方法代码示例

本文整理汇总了Python中Lexer.Lexer.lexe方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.lexe方法的具体用法?Python Lexer.lexe怎么用?Python Lexer.lexe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Lexer.Lexer的用法示例。


在下文中一共展示了Lexer.lexe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import lexe [as 别名]
class Calculator:
	""" Very cool calculator class using all the other cool classes """
	
	def __init__(self):
		self.lexer = Lexer()
		self.context = EvaluationContext()
		self.builtins = ["exit", "debug", "help", "vars", "functions"]
		self.debug = False
	
	def main(self):
		""" Main function. Who would have expected this? """
		
		print("Welcome to {}.py v0.7".format(self.__class__.__name__))
		print()
		print("Note: Not every function will work with complex numbers.")
		print("Note: use debug command to toggle debug mode (syntax tree output).")
		print("Note: this version does not (yet?) support function definitions at runtime.")
		#TODO: some mechanism to insert lambda function definitions at runtime?
		print()
		self.handle_builtin("help")
		
		while True:
			try:
				print()
				formula = input(">>> ")
				
				if formula in self.builtins:
					self.handle_builtin(formula)
					continue
				
				lexemes = self.lexer.lexe(formula)
				parsed = Sum.parse(lexemes)
				if len(lexemes) != 0:
					raise ParserError("Formula did not parse completely," +
										"remaining lexemes are: {}".format(lexemes))
				
				if (self.debug):
					print("Input was parsed as this simplified syntax tree:")
					self.pretty_print(str(parsed))
				
				result = parsed.evaluate(self.context)
				self.context.set_variable("ans", result)
				
				print(self.format_result(result))
			
			except ParserError as e:
				print("ParserError:", e)
				continue
			except LexerError as e:
				print("LexerError:", e)
				continue
			except (EvaluationError, ValueError, TypeError) as e:
				print("EvaluationError:", e)
				continue
			except (KeyboardInterrupt, EOFError):
				print("Exiting.")
				break	
	
	def pretty_print(self, tree, indent=4):
		""" Very, very, very bad pretty print function.
			I couldn't imagine a clean way to pretty print these syntax tree strings... """
		cur_indent = 0
		cur_line = ""
		for i in range(len(tree) - 1):
			if tree[i] in [":", "("]:
				cur_line += tree[i]
				cur_indent += indent
				print(cur_line)
				cur_line = " " * cur_indent
				continue
			if tree[i] == " ":
				if cur_line == " " * len(cur_line):
					continue #don't print empty lines
				print(cur_line)
				cur_line = " " * cur_indent
				continue
			if tree[i] in ["]", ")"]:
				print(cur_line)
				cur_indent -= indent
				cur_line = " " * cur_indent + tree[i]
				continue
			cur_line += tree[i]
		
		print(cur_line)
		print(tree[-1])
	
	def handle_builtin(self, builtin):
		if builtin == "help":
			print("Built-in commands:")
			print(*self.builtins, sep=", ")
		
		elif builtin == "exit":
			raise EOFError
		
		elif builtin == "vars":
			variables = sorted(self.context.variables)
			max_length = max([len(v) for v in variables]) + 1
			
			print("There are currently {} known variables:".format(len(variables)))
			print("(Constants are marked with [*])")
#.........这里部分代码省略.........
开发者ID:ebbes,项目名称:MathParser,代码行数:103,代码来源:Calculator.py


注:本文中的Lexer.Lexer.lexe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。