Exemplo n.º 1
0
def unquote(asker, value):
    #FIXME more circular dependencies
    import dictionaries, strings
    if isinstance(value, representations.Representation):
        return value.represents
    #FIXME all of this should be done with requirements embedded in questions
    #rather than asking and then converting
    bindings = asker.ask(fields.get_field(representations.bindings(), value)).firm_answer
    proto_literal_bindings = dictionaries.to_dict(asker, bindings)
    literal_bindings = frozendict({strings.to_str(asker, k):unquote(asker, v) 
            for k, v in proto_literal_bindings.iteritems()})
    head = asker.ask(fields.get_field(representations.head(), value)).firm_answer
    literal_head = strings.to_str(asker, head)
    return T(literal_head, literal_bindings)
Exemplo n.º 2
0
def update_view(asker, view, input):
    relayer = asker.pass_through(should_return.head)
    #FIXME using the name view_in is a hack to keep view from being locally scoped...
    @relayer(should_print.head)
    def print_string(s):
        view_in = asker.refresh(view)
        asker.update(add_line(input), view_in)
        #FIXME more things going wrong with representation levels...
        s = convert.unquote(asker, s)
        line = asker.ask_firmly(render(s))
        asker.update(add_line(line), view_in)
        return properties.trivial()
    @relayer(should_dispatch.head)
    def dispatch(x):
        view_in = asker.refresh(view)
        asker.update(add_line(input), view_in)
        head = fields.get(asker, representations.head(), x)
        bindings = fields.get(asker, representations.bindings(), x)
        view_in = asker.refresh(view_in)
        asker.update(add_line(head), view_in)
        for p in dictionaries.item_iterator(asker, bindings):
            var, val = pairs.to_pair(asker, p)
            view_in = asker.refresh(view_in)
            asker.update(bind_variable(var, val), view_in)
        return properties.trivial()
    @relayer(should_assign.head)
    def assign(s, x):
        view_in = asker.refresh(view)
        asker.update(add_line(input), view_in)
        view_in = asker.refresh(view_in)
        asker.update(bind_variable(s, x), view_in)
        return properties.trivial()

    interpret_response = asker.ask(interpret_input(view, input), handler=relayer)
    if interpret_response.has_answer():
        bindings = dictionaries.to_dict(asker, fields.get(asker, bindings_field(), view))
        bindings_str = {strings.to_str(asker, k):v for k, v in bindings.items()}
        for char in "xyzw" + string.letters:
            if char not in bindings_str:
                break
        asker.update(bind_variable(T.from_str(char), interpret_response.answer), view)
        new_line = strings.concat(
            T.from_str("{} = ".format(char)),
            input
        )
        asker.update(add_line(new_line), view)
    return asker.reply()