Esempio n. 1
0
def parser(token):
    if token == '':
        return Atom('')

    if type(token) == list:
        if len(token) == 0:
            raise Exception("empty compounds not allowed")

        return Compound([parser(x) for x in token])

    if type(token) == str:
        if len(token) >= 2 and token[0] == '"' and token[-1] == '"':
            return String(token[1:-1])

        if token == '#f':
            return Boolean(False)

        if token == '#t':
            return Boolean(True)

        num = maybe_number(token)
        if num is not None:
            return Number(num)

        if is_identifier(token):
            return Identifier(token)

    raise Exception(f"invalid token: {token}")
Esempio n. 2
0
def parser(token):
    if token == '':
        return Atom('')

    if type(token) == list:
        ## What about empty compounds, are they allowed by the grammar?
        return Compound([parser(x) for x in token])

    if type(token) == str:
        if len(token) >= 2 and token[0] == '"' and token[-1] == '"':
            return String(token[1:-1])

        if token == '#f':
            return Boolean(False)

        if token == '#t':
            return Boolean(True)

        num = maybe_number(token)
        if num is not None:
            return Number(num)

        if is_identifier(token):
            return Identifier(token)

    ## Actually nice error messages
    raise Exception(f"invalid token: {token}")