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"), ]
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"), ]
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"
def test_wasm_compiler_binop(compiler, cpack, operator): left, right = cpack astconstantdef = ast.parse( f"{left} {operator} {right}", "<test>", "eval" ).body resconstantdef = compiler.compile(astconstantdef) left_type = WASM_PY_TYPES[type(left)] assert resconstantdef == [ SubInstruction(f"{left_type}.const", left), SubInstruction(f"{left_type}.const", right), SubInstruction(f"{left_type}.{OP_TYPES[operator]}"), ]
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"))
def test_wasm_compiler_valid_constant(compiler, cpack): constant_type, constant = cpack astconstantdef = ast.parse(str(constant), "<test>", "eval").body resconstantdef = compiler.compile(astconstantdef) assert resconstantdef == SubInstruction( f"{WASM_TYPES[constant_type]}.const", constant )
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"))