示例#1
0
def scheme_read(src):
    """Read the next expression from SRC, a Buffer of tokens.

    >>> lines = ["(+ 1 ", "(+ 23 4)) ("]
    >>> src = Buffer(tokenize_lines(lines))
    >>> print(scheme_read(src))
    (+ 1 (+ 23 4))
    >>> read_line("'hello")
    Pair('quote', Pair('hello', nil))
    >>> print(read_line("(car '(1 2))"))
    (car (quote (1 2)))
    """
    if src.current() is None:
        raise EOFError
    val = src.pop()
    if val == "nil":
        return nil
    elif type(val) is int or type(val) is float:
        return scnum(val)
    elif type(val) is bool:
        return scbool(val)
    elif val not in DELIMITERS:
        if val[0] == '"':
            return scstr(eval(val))
        else:
            return intern(val)
    elif val == "'":
        return Pair(intern('quote'), Pair(scheme_read(src), nil))
    elif val == "(":
        return read_tail(src)
    else:
        raise SyntaxError("unexpected token: {0}".format(val))
def scheme_read(src):
    """Read the next expression from SRC, a Buffer of tokens.

    >>> lines = ["(+ 1 ", "(+ 23 4)) ("]
    >>> src = Buffer(tokenize_lines(lines))
    >>> print(scheme_read(src))
    (+ 1 (+ 23 4))
    >>> read_line("'hello")
    Pair('quote', Pair('hello', nil))
    >>> print(read_line("(car '(1 2))"))
    (car (quote (1 2)))
    """
    if src.current() is None:
        raise EOFError
    val = src.pop()
    if type(val) is int or type(val) is float:
        return scnum(val)
    elif type(val) is bool:
        return scbool(val)
    elif val not in DELIMITERS:
        if val[0] == '"':
            return scstr(eval(val))
        else:
            return intern(val)
    elif val == "'":
        "*** YOUR CODE HERE ***"
        return Pair('quote', Pair(scheme_read(src), nil))
    elif val == "(":
        return read_tail(src)
    else:
        raise SyntaxError("unexpected token: {0}".format(val))
示例#3
0
def scheme_read(src):
    #    import pdb; pdb.set_trace()
    """Read the next expression from SRC, a Buffer of tokens.

    >>> lines = ["(+ 1 ", "(+ 23 4)) ("]
    >>> src = Buffer(tokenize_lines(lines))
    >>> print(scheme_read(src))
    (+ 1 (+ 23 4))
    >>> read_line("(+ 1 2)")
    Pair('+', Pair(1, Pair(2, nil)))
    >>> read_line("'hello")
    Pair('quote', Pair('hello', nil))
    >>> print(read_line("(car '(1 2))"))
    (car (quote (1 2)))
    """
    if src.current() is None:
        raise EOFError
    val = src.pop()
    if val == "nil":
        return nil
    elif type(val) is int or type(val) is float:
        return scnum(val)
    elif type(val) is bool:
        return scbool(val)
    # self-evaluating
    elif val not in DELIMITERS:
        if val[0] == '"':
            return scstr(eval(val))
        else:
            return intern(val)
    elif val == "'":
        "*** YOUR CODE HERE ***"
        # nested Pair eventually matches the required structure
        rest = scheme_read(src)
        return Pair("quote", Pair(rest, nil))
    elif val == "(":
        return read_tail(src)
    else:
        raise SyntaxError("unexpected token: {0}".format(val))
示例#4
0
def scheme_read(src):
#    import pdb; pdb.set_trace()
    """Read the next expression from SRC, a Buffer of tokens.

    >>> lines = ["(+ 1 ", "(+ 23 4)) ("]
    >>> src = Buffer(tokenize_lines(lines))
    >>> print(scheme_read(src))
    (+ 1 (+ 23 4))
    >>> read_line("(+ 1 2)")
    Pair('+', Pair(1, Pair(2, nil)))
    >>> read_line("'hello")
    Pair('quote', Pair('hello', nil))
    >>> print(read_line("(car '(1 2))"))
    (car (quote (1 2)))
    """
    if src.current() is None:
        raise EOFError
    val = src.pop()
    if val == "nil":
        return nil
    elif type(val) is int or type(val) is float:
        return scnum(val)
    elif type(val) is bool:
        return scbool(val)
    # self-evaluating
    elif val not in DELIMITERS:
        if val[0] == '"':
            return scstr(eval(val))
        else:
            return intern(val)
    elif val == "'":
        "*** YOUR CODE HERE ***"
        # nested Pair eventually matches the required structure
        rest = scheme_read(src)
        return Pair("quote", Pair(rest, nil))
    elif val == "(":
        return read_tail(src)
    else:
        raise SyntaxError("unexpected token: {0}".format(val))