Esempio n. 1
0
def evaluate_expression(input, environment=None):
    """
    evaluate a string or file object in the scheme evaluator as an expression,
    and return the result as a scheme object.
    """
    environment = make_global_environment() if environment is None else environment
    return full_evaluate(string_to_scheme(input, start_parsing=EXPRESSION),
                         environment)
Esempio n. 2
0
def evaluate_expression(input, environment=None):
    """
    evaluate a string or file object in the scheme evaluator as an expression,
    and return the result as a scheme object.
    """
    environment = make_global_environment(
    ) if environment is None else environment
    return full_evaluate(string_to_scheme(input, start_parsing=EXPRESSION),
                         environment)
Esempio n. 3
0
def evaluate(input, environment=None):
    """
    evaluate a string or file object in the scheme evaluator as a program, and
    return the result as a scheme object.
    """
    environment = make_global_environment() if environment is None else environment
    expressions = string_to_scheme(input)

    for expression in expressions:
        result = full_evaluate(expression, environment)
    return result
Esempio n. 4
0
def evaluate(input, environment=None):
    """
    evaluate a string or file object in the scheme evaluator as a program, and
    return the result as a scheme object.
    """
    environment = make_global_environment(
    ) if environment is None else environment
    expressions = string_to_scheme(input)

    for expression in expressions:
        result = full_evaluate(expression, environment)
    return result
Esempio n. 5
0
def repl():
    #: the built-in scheme forms and special repl commands
    KEYWORDS = ('lambda', 'macro', 'if', 'quote', 'eval', 'define', 'delay',
                '.reset', '.exit', '.quit', '.help')

    # the scheme auto-completer
    def completer(text, state):
        # look through SCHEME_KEYWORDS
        for w in KEYWORDS:
            if w.startswith(text):
                if state <= 0:
                    return w
                state -= 1

        # look through the environment names
        for w in environment.iterkeys():
            if w.startswith(text):
                if state <= 0:
                    return w
                state -= 1

    histfile = os.path.join(os.path.expanduser("~"), ".pyscheme-hist")
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)

    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind("set blink-matching-paren on")
    readline.set_completer(completer)

    environment = make_global_environment()
    while True:

        try:
            text = raw_input(">>  ")

            # test for special commands
            if text == '.reset':
                print "reseting environment..."
                environment = make_global_environment()
                continue
            elif text == '.help':
                print "Just type scheme expression and have fun."
                continue
            elif text in ('.exit', '.quit'):
                break

            result = evaluate_expression(InterpreterInput(text), environment)

            print "=>", pretty_print(result)

            # set % as the last evaluated expression in environment
            environment['%'] = quote(result)
        except EOFError:
            break
        except KeyboardInterrupt:
            print "\ninterrupt."
        except Exception as e:
            print "error:", e.message

    print "\nexiting..."
Esempio n. 6
0
def repl():
    #: the built-in scheme forms and special repl commands
    KEYWORDS = ('lambda', 'macro', 'if', 'quote', 'eval', 'define', 'delay',
                '.reset', '.exit', '.quit', '.help')

    # the scheme auto-completer
    def completer(text, state):
        # look through SCHEME_KEYWORDS
        for w in KEYWORDS:
            if w.startswith(text):
                if state <= 0:
                    return w
                state -= 1

        # look through the environment names
        for w in environment.iterkeys():
            if w.startswith(text):
                if state <= 0:
                    return w
                state -= 1

    histfile = os.path.join(os.path.expanduser("~"), ".pyscheme-hist")
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)

    readline.parse_and_bind("tab: complete")
    readline.parse_and_bind("set blink-matching-paren on")
    readline.set_completer(completer)

    environment = make_global_environment()
    while True:

        try:
            text = raw_input(">>  ")

            # test for special commands
            if text == '.reset':
                print "reseting environment..."
                environment = make_global_environment()
                continue
            elif text == '.help':
                print "Just type scheme expression and have fun."
                continue
            elif text in ('.exit', '.quit'):
                break

            result = evaluate_expression(InterpreterInput(text),
                                         environment)

            print "=>", pretty_print(result)

            # set % as the last evaluated expression in environment
            environment['%'] = quote(result)
        except EOFError:
            break
        except KeyboardInterrupt:
            print "\ninterrupt."
        except Exception as e:
            print "error:", e.message

    print "\nexiting..."