Exemplo n.º 1
0
def generate_parameters(constructor):
    args, aux = [], []
    for i in range(constructor.size):
        argument = constructor.arguments[i]
        name = _ffi.string(_an.names[argument.attribute]).decode()
        args.extend(generate_parameter(name, argument.type))
        aux.extend(generate_aux(name, argument.type))
    return args, aux
Exemplo n.º 2
0
def generate_arguments(constructor):
    args = []
    for i in range(constructor.size):
        argument = constructor.arguments[i]
        argument_type = argument_type_str(argument.type)
        name = _ffi.string(_an.names[argument.attribute]).decode()
        args.append((name, argument_type))
    return args
Exemplo n.º 3
0
def _handle_error(ret: bool, handler=None):
    if not ret:
        code = _lib.clingo_error_code()
        if code == _lib.clingo_error_unknown and handler is not None and handler.error is not None:
            raise handler.error[0](handler.error[1]).with_traceback(handler.error[2])
        msg = _ffi.string(_lib.clingo_error_message()).decode()
        if code == _lib.clingo_error_bad_alloc:
            raise MemoryError(msg)
        raise RuntimeError(msg)
Exemplo n.º 4
0
def generate_python():
    for i in range(_cs.size):
        constructor = _cs.constructors[i]
        c_name = _ffi.string(constructor.name).decode()
        name = to_camel_case(c_name)
        sys.stdout.write(f'\n{generate_sig(name, generate_arguments(constructor))}\n')
        sys.stdout.write("    '''\n")
        sys.stdout.write(f'    Construct an AST node of type `ASTType.{name}`.\n')
        sys.stdout.write("    '''\n")
        parameters, aux = generate_parameters(constructor)
        sys.stdout.write(f"    p_ast = _ffi.new('clingo_ast_t**')\n")
        for x in aux:
            sys.stdout.write(f"    {x}\n")
        sys.stdout.write(f"    _handle_error(_lib.clingo_ast_build(\n")
        sys.stdout.write(f"        _lib.clingo_ast_type_{c_name}, p_ast")
        for param in parameters:
            sys.stdout.write(f",\n        {param}")
        sys.stdout.write(f"))\n")

        sys.stdout.write(f"    return AST(p_ast[0])\n")
Exemplo n.º 5
0
def _to_str(c_str) -> str:
    return _ffi.string(c_str).decode()
Exemplo n.º 6
0
def _str(f_size, f_str, *args, handler=None):
    p_size = _ffi.new('size_t*')
    _handle_error(f_size(*args, p_size), handler)
    p_str = _ffi.new('char[]', p_size[0])
    _handle_error(f_str(*args, p_str, p_size[0]), handler)
    return _ffi.string(p_str).decode()