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


Python Reader.get_sexpr方法代码示例

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


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

示例1: Elisp

# 需要导入模块: from reader import Reader [as 别名]
# 或者: from reader.Reader import get_sexpr [as 别名]
class Elisp(Lisp):
    def __init__(self):
        self.stdin = sys.stdin
        self.stdout = sys.stdout
        self.stderr = sys.stderr

        self.debug = False
        self.verbose = True
        self.core = True
        self.closure = True

        self.reader = Reader()
        self.env = Environment()

        self.init()

    def init(self):
        # core functions
        self.env.set('eq',      Function(self.eq))
        self.env.set('quote',   Function(self.quote))
        self.env.set('car',     Function(self.car))
        self.env.set('cdr',     Function(self.cdr))
        self.env.set('cons',    Function(self.cons))
        self.env.set('atom',    Function(self.atom))
        self.env.set('cond',    Function(self.cond))

        # utility functions
        self.env.set('print',   Function(self.println))

        # special forms
        self.env.set('lambda',  Function(self.lambda_fun))
        self.env.set('label',   Function(self.label))

        # meta-elements
        self.env.set('__elisp__',   self)
        self.env.set('__global__',  self.env)

    def lambda_fun(self, env, args):
        if self.env != env.get('__global__') and self.closure:
            return Closure(env, args[0], args[1:])
        else:
            return Lambda(args[0], args[1:])

    def usage(self):
        self.print_banner()
        print ('%s <options> [elisp files]\n' % NAME.lower())

    def print_banner(self):
        print ('The %s programming shell %s' % (NAME, VERSION))
        print ('    Type `help` for more information\n')

    def print_help(self):
        print ('Help for eLisp %s' % VERSION)
        print ('    Type `help` for more information')
        print ('    Type `env` to see the bindings in current environment')
        print ('    Type `load` followed by one or more filenames to load source files')
        print ('    Type `quit` to exit the interpreter')

    def push(self, env=None):
        if env:
            self.env = self.env.push(env)
        else:
            self.env = self.env.push()

    def pop(self):
        self.env = self.env.pop()

    def repl(self):
        while True:
            source = self.get_complete_command()

            try:
                if source in ['quit']:
                    break
                elif source in ['help']:
                    self.print_help()
                elif source.startswith('load'):
                    files = source.split(' ')[1:]
                    self.process_files(files)
                elif source in ['env']:
                    print (self.env)
                else:
                    self.process(source)
            except AttributeError:
                print ('Could not process command: ', source)
                return

    def process(self, source):
        sexpr = self.reader.get_sexpr(source)

        while sexpr:
            result = None

            try:
                result = self.eval(sexpr)
            except Error as err:
                print (err)

            if self.verbose:
                self.stdout.write('     %s\n' % result)
#.........这里部分代码省略.........
开发者ID:tsingcoo,项目名称:study,代码行数:103,代码来源:elisp.py

示例2: Lithp

# 需要导入模块: from reader import Reader [as 别名]
# 或者: from reader.Reader import get_sexpr [as 别名]
class Lithp(Lisp):
    """ The Lithper class is the interpreter driver.  It does the following:
            1. Initialize the global environment
            2. Parse the cl arguments and act on them as appropriate
            3. Initialize the base Lisp functions
            4. Read input
            5. Evaluate
            6. Print
            7. Loop back to #4
    """
    def __init__( self):
        iostreams=(sys.stdin, sys.stdout, sys.stderr)
        (self.stdin, self.stdout, self.stderr) = iostreams

        self.verbose = False
        self.core = True
        self.closures = True

        self.rdr = Reader()
        self.environment = Environment()

        self.init()

    def init(self):
        # Define core functions
        self.environment.set("eq",     Function(self.eq))
        self.environment.set("quote",  Function(self.quote))
        self.environment.set("car",    Function(self.car))
        self.environment.set("cdr",    Function(self.cdr))
        self.environment.set("cons",   Function(self.cons))
        self.environment.set("atom",   Function(self.atom))
        self.environment.set("cond",   Function(self.cond))

        # Define utility function
        self.environment.set("print",  Function(self.println))

        # Special forms
        self.environment.set("lambda", Function(self.lambda_))
        self.environment.set("label",  Function(self.label))

        # Define meta-elements
        self.environment.set("__lithp__",  self)
        self.environment.set("__global__", self.environment)

    def usage(self):
        self.print_banner()
        print
        print NAME.lower(), " <options> [lithp files]\n"

    def print_banner(self):
        print "The", NAME, "programming shell", VERSION
        print "   by Fogus,", WWW
        print "   Type :help for more information"
        print

    def print_help(self):
        print "Help for Lithp v", VERSION
        print "  Type :help for more information"
        print "  Type :env to see the bindings in the current environment"
        print "  Type :load followed by one or more filenames to load source files"
        print "  Type :quit to exit the interpreter"

    def push(self, env=None):
        if env:
            self.environment = self.environment.push(env)
        else:
            self.environment = self.environment.push()

    def pop(self):
        self.environment = self.environment.pop()

    def repl(self):
        while True:
            # Stealing the s-expression parsing approach from [CLIPS](http://clipsrules.sourceforge.net/)
            source = self.get_complete_command()

            # Check for any REPL directives
            try:
                if source in [":quit"]:
                    break
                elif source in [":help"]:
                    self.print_help()
                elif source.startswith(":load"):
                    files = source.split(" ")[1:]
                    self.process_files(files)
                elif source in [":env"]:
                    print(self.environment)
                else:
                    self.process(source)
            except AttributeError:
                print "Could not process command: ", source
                return


    # Source is processed one s-expression at a time.
    def process(self, source):
        sexpr = self.rdr.get_sexpr(source)

        while sexpr:
            result = None
#.........这里部分代码省略.........
开发者ID:k4rtik,项目名称:lithp,代码行数:103,代码来源:lithp.py


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