Beispiel #1
0
def jsify_assign(
    node: typing.Union[ast.Assign, ast.AnnAssign]
) -> typing.List[unholy.Compilable]:
    # targets?, value, type_comment
    if isinstance(node, ast.Assign):
        t = node.targets
    else:
        t = [node.target]

    targets = []

    for i in t:
        targets.extend(unholy.jsify_node(i))
    if len(targets) == 1:
        return [
            unholy.JSExpression(
                [targets[0], ' = ', *unholy.jsify_node(node.value)])
        ]
    else:
        output = ['[']
        for i in targets:
            output.append(i)
            output.append(', ')
        output.append('] =')
        output.extend(unholy.jsify_node(node.value))
        return [unholy.JSExpression(output)]
Beispiel #2
0
def _parse_range_arguments(call: ast.Call):
    if len(call.args) == 1:
        return [unholy.JSExpression(['0'])], unholy.jsify_node(
            call.args[0]), [unholy.JSExpression(['1'])]
    elif len(call.args) == 2:
        return unholy.jsify_node(call.args[0]), unholy.jsify_node(
            call.args[1]), [unholy.JSExpression(['1'])]
    elif len(call.args) == 3:
        return unholy.jsify_node(call.args[0]), unholy.jsify_node(
            call.args[1]), unholy.jsify_node(call.args[2])
    else:
        raise unholy.CompilationError(
            'Bad arguments for range() function in for loop.')
Beispiel #3
0
def jsify_lambda(node: ast.Lambda) -> typing.List[unholy.Compilable]:
    # args, body
    arguments: typing.List[ast.arg] = node.args.args
    arg_list = ', '.join([i.arg for i in arguments])
    if node.args.kwarg:
        arg_list += f', options'
    if node.args.vararg:
        arg_list += f', ...{node.args.vararg.arg}'
    return [
        unholy.JSExpression([
            unholy.JSExpression([f'({arg_list}) =>']),
            unholy.JSBlock(unholy.jsify_node(node.body))
        ])
    ]
Beispiel #4
0
def jsify_subscript(node: ast.Subscript) -> typing.List[unholy.Compilable]:
    # value, slice, ctx,
    v = unholy.jsify_node(node.value)
    # node.slice: typing.Union[ast.Slice, ast.Index]
    if isinstance(node.slice, ast.Slice):
        raise unholy.CompilationError('Slicing is not yet implemented')
    elif isinstance(node.slice, ast.Index):
        return [
            unholy.JSExpression([*v, *unholy.jsify_node(node.slice.value)])
        ]
    elif isinstance(node.slice, (ast.Constant, ast.Name)):
        return [
            unholy.JSExpression([*v, '[', *unholy.jsify_node(node.slice), ']'])
        ]
    else:
        raise unholy.CompilationError(
            f'Unknown index type: {type(node.slice)}. No way to js-ify. Aborting'
        )
Beispiel #5
0
def jsify_call(node: ast.Call) -> typing.List[unholy.Compilable]:
    func_name = unholy.jsify_node(node.func)
    args = []
    for i in node.args:
        args.extend(unholy.jsify_node(i))

    if node.keywords:
        warnings.warn(
            f'keyword arguments were ignored while calling {func_name}')
    return [unholy.JSExpression([func_name[0], '(', *args, ')'])]
Beispiel #6
0
def jsify_attribute(node: ast.Attribute) -> typing.List[unholy.Compilable]:
    # value, attr, ctx
    return [
        unholy.JSExpression([*unholy.jsify_node(node.value), '.', node.attr])
    ]
Beispiel #7
0
def jsify_name(node: ast.Name) -> typing.List[unholy.Compilable]:
    logging.debug(f'asdf {unholy.map_name(node.id)=}')
    return [unholy.JSExpression([unholy.map_name(node.id)])]
Beispiel #8
0
def jsify_constant(node: ast.Constant) -> typing.List[unholy.Compilable]:
    return [unholy.JSExpression([json.dumps(node.value)])]
Beispiel #9
0
def jsify_num(node: ast.Num) -> typing.List[unholy.Compilable]:
    return [unholy.JSExpression([str(node.n)])]
Beispiel #10
0
def jsify_await(node: ast.Await) -> typing.List[unholy.Compilable]:
    return [unholy.JSExpression(['await', *unholy.jsify_node(node.value)])]