Exemplo n.º 1
0
def test_wasm_compiler_name_precedence(compiler):
    compiler._symbol_table.local[Definition("a")] = 1
    compiler._symbol_table.module[Definition("a")] = 1
    compiler._scope = Scopes.LOCAL
    astnamedef = ast.parse("a", "<test>", "eval").body
    resnamedef = compiler.compile(astnamedef)
    assert resnamedef == SubInstruction("local.get", Definition("a"))
Exemplo n.º 2
0
def test_wasm_compiler_binop_local_const(compiler):
    compiler._symbol_table.local[Definition("a")] = 1.0
    compiler._scope = Scopes.LOCAL
    astconstantdef = ast.parse(f"1 + a", "<test>", "eval").body
    resconstantdef = compiler.compile(astconstantdef)
    assert resconstantdef == [
        SubInstruction(f"i32.const", 1),
        SubInstruction(f"local.get", Definition("a")),
        SubInstruction(f"f32.add"),
    ]
Exemplo n.º 3
0
def test_wasm_functiondef_return_none(compiler):
    astfuncdef = ast.parse("def __test(a: integer, b: integer) -> None: pass")
    resfuncdef = compiler.compile(astfuncdef)
    assert resfuncdef == Instruction(
        "module",
        Instruction(
            "func",
            Instruction("param", Definition("a"), WASM_TYPES["integer"]),
            Instruction("param", Definition("b"), WASM_TYPES["integer"]),
        ),
    )
Exemplo n.º 4
0
def test_wasm_compiler_binop_module_module(compiler):
    compiler._symbol_table.module[Definition("a")] = 1
    compiler._symbol_table.module[Definition("b")] = 1
    compiler._scope = Scopes.LOCAL
    astconstantdef = ast.parse(f"a + b", "<test>", "eval").body
    resconstantdef = compiler.compile(astconstantdef)
    assert resconstantdef == [
        SubInstruction(f"global.get", Definition("a")),
        SubInstruction(f"global.get", Definition("b")),
        SubInstruction(f"i32.add"),
    ]
Exemplo n.º 5
0
def test_wasm_functiondef(compiler, types):
    astfuncdef = ast.parse(
        "def __test(a: {}, b: {}) -> {}: pass".format(*types)
    )
    resfuncdef = compiler.compile(astfuncdef)
    assert resfuncdef == Instruction(
        "module",
        Instruction(
            "func",
            Instruction("param", Definition("a"), WASM_TYPES[types[0]]),
            Instruction("param", Definition("b"), WASM_TYPES[types[1]]),
            Instruction("result", WASM_TYPES[types[2]]),
        ),
    )
Exemplo n.º 6
0
def test_wasm_compiler_module_scope_local_name(compiler):
    compiler._symbol_table.local[Definition("a")] = 1
    astnamedef = ast.parse("a", "<test>", "eval").body
    with pytest.raises(NanoSymbolError) as symbol_error:
        compiler.compile(astnamedef)

    symbol_error = symbol_error.value
    assert symbol_error.msg.startswith(f"Couldn't find {Definition('a')}")
Exemplo n.º 7
0
def test_subinstruction_complex():
    instr = SubInstruction("test")
    instr.parameters.append("bla")
    instr.parameters.append(Definition("bla"))
    assert str(instr) == "test bla $bla"

    tempinstr = Instruction("bla")
    instr.parameters.append(tempinstr)
    assert str(instr) == "test bla $bla (bla)"

    instr.parameters.append(SubInstruction("bla"))
    assert str(instr) == "test bla $bla (bla) bla"
Exemplo n.º 8
0
def test_definition():
    defx = Definition("x")
    assert defx.name == "x"
    assert str(defx) == "$x"
Exemplo n.º 9
0
def test_wasm_compiler_module_name(compiler):
    compiler._symbol_table.module[Definition("a")] = 1
    astnamedef = ast.parse("a", "<test>", "eval").body
    resnamedef = compiler.compile(astnamedef)
    assert resnamedef == SubInstruction("global.get", Definition("a"))