From 2a6da91eb9e27978b3b3893f17971974c27c1efa Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Mon, 19 Sep 2022 14:53:22 +0200 Subject: [PATCH] Simplified locations, adds typing tests --- phasm/typer.py | 2 +- phasm/typing.py | 43 +++++++++++-------- .../integration/test_lang/test_primitives.py | 11 +++++ .../test_lang/test_static_array.py | 12 ++++++ 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/phasm/typer.py b/phasm/typer.py index 7bc6434..56b1f05 100644 --- a/phasm/typer.py +++ b/phasm/typer.py @@ -172,7 +172,7 @@ def module_constant_def(ctx: Context, inp: ourlang.ModuleConstantDef) -> None: if inp.type_str is None: inp.type_var = ctx.new_var() else: - inp.type_var = from_str(ctx, inp.type_str, inp.type_str) + inp.type_var = from_str(ctx, inp.type_str) assert inp.constant.type_var is not None ctx.unify(inp.type_var, inp.constant.type_var) diff --git a/phasm/typing.py b/phasm/typing.py index 84e5666..468e537 100644 --- a/phasm/typing.py +++ b/phasm/typing.py @@ -483,7 +483,7 @@ def simplify(inp: TypeVar) -> Optional[str]: return None -def make_u8(ctx: Context, location: str) -> TypeVar: +def make_u8(ctx: Context) -> TypeVar: """ Makes a u8 TypeVar """ @@ -491,10 +491,10 @@ def make_u8(ctx: Context, location: str) -> TypeVar: result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT)) result.add_constraint(TypeConstraintBitWidth(minb=8, maxb=8)) result.add_constraint(TypeConstraintSigned(False)) - result.add_location(location) + result.add_location('u8') return result -def make_u32(ctx: Context, location: str) -> TypeVar: +def make_u32(ctx: Context) -> TypeVar: """ Makes a u32 TypeVar """ @@ -502,10 +502,10 @@ def make_u32(ctx: Context, location: str) -> TypeVar: result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT)) result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32)) result.add_constraint(TypeConstraintSigned(False)) - result.add_location(location) + result.add_location('u32') return result -def make_u64(ctx: Context, location: str) -> TypeVar: +def make_u64(ctx: Context) -> TypeVar: """ Makes a u64 TypeVar """ @@ -513,10 +513,10 @@ def make_u64(ctx: Context, location: str) -> TypeVar: result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT)) result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64)) result.add_constraint(TypeConstraintSigned(False)) - result.add_location(location) + result.add_location('u64') return result -def make_i32(ctx: Context, location: str) -> TypeVar: +def make_i32(ctx: Context) -> TypeVar: """ Makes a i32 TypeVar """ @@ -524,10 +524,10 @@ def make_i32(ctx: Context, location: str) -> TypeVar: result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT)) result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32)) result.add_constraint(TypeConstraintSigned(True)) - result.add_location(location) + result.add_location('i32') return result -def make_i64(ctx: Context, location: str) -> TypeVar: +def make_i64(ctx: Context) -> TypeVar: """ Makes a i64 TypeVar """ @@ -535,30 +535,30 @@ def make_i64(ctx: Context, location: str) -> TypeVar: result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT)) result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64)) result.add_constraint(TypeConstraintSigned(True)) - result.add_location(location) + result.add_location('i64') return result -def make_f32(ctx: Context, location: str) -> TypeVar: +def make_f32(ctx: Context) -> TypeVar: """ Makes a f32 TypeVar """ result = ctx.new_var() result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT)) result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32)) - result.add_location(location) + result.add_location('f32') return result -def make_f64(ctx: Context, location: str) -> TypeVar: +def make_f64(ctx: Context) -> TypeVar: """ Makes a f64 TypeVar """ result = ctx.new_var() result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT)) result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64)) - result.add_location(location) + result.add_location('f64') return result -BUILTIN_TYPES: Dict[str, Callable[[Context, str], TypeVar]] = { +BUILTIN_TYPES: Dict[str, Callable[[Context], TypeVar]] = { 'u8': make_u8, 'u32': make_u32, 'u64': make_u64, @@ -570,7 +570,7 @@ BUILTIN_TYPES: Dict[str, Callable[[Context, str], TypeVar]] = { TYPE_MATCH_STATIC_ARRAY = re.compile(r'^([uif][0-9]+)\[([0-9]+)\]') -def from_str(ctx: Context, inp: str, location: str) -> TypeVar: +def from_str(ctx: Context, inp: str, location: Optional[str] = None) -> TypeVar: """ Creates a new TypeVar from the string @@ -583,7 +583,10 @@ def from_str(ctx: Context, inp: str, location: str) -> TypeVar: with the context creation. """ if inp in BUILTIN_TYPES: - return BUILTIN_TYPES[inp](ctx, location) + result = BUILTIN_TYPES[inp](ctx) + if location is not None: + result.add_location(location) + return result match = TYPE_MATCH_STATIC_ARRAY.fullmatch(inp) if match: @@ -596,7 +599,11 @@ def from_str(ctx: Context, inp: str, location: str) -> TypeVar: from_str(ctx, match[1], match[1]) for _ in range(int(match[2])) ))) - result.add_location(location) + + result.add_location(inp) + + if location is not None: + result.add_location(location) return result diff --git a/tests/integration/test_lang/test_primitives.py b/tests/integration/test_lang/test_primitives.py index 5736f0b..076b308 100644 --- a/tests/integration/test_lang/test_primitives.py +++ b/tests/integration/test_lang/test_primitives.py @@ -33,6 +33,17 @@ def testEntry() -> {type_}: assert 32.125 == result.returned_value assert TYPE_MAP[type_] == type(result.returned_value) +@pytest.mark.integration_test +def test_expr_constant_entanglement(): + code_py = """ +@exported +def testEntry() -> u8: + return 1000 +""" + + with pytest.raises(TypingError, match='u8.*1000'): + Suite(code_py).run_code() + @pytest.mark.integration_test @pytest.mark.parametrize('type_', ALL_INT_TYPES) def test_module_constant_int(type_): diff --git a/tests/integration/test_lang/test_static_array.py b/tests/integration/test_lang/test_static_array.py index 5708fb1..9f549da 100644 --- a/tests/integration/test_lang/test_static_array.py +++ b/tests/integration/test_lang/test_static_array.py @@ -90,6 +90,18 @@ CONSTANT: u8[3] = (24, 57, 280, ) with pytest.raises(TypingError, match='u8.*280'): Suite(code_py).run_code() +@pytest.mark.integration_test +def test_return_as_int(): + code_py = """ +CONSTANT: u8[3] = (24, 57, 80, ) + +def testEntry() -> u32: + return CONSTANT +""" + + with pytest.raises(TypingError, match=r'u32.*u8\[3\]'): + Suite(code_py).run_code() + @pytest.mark.integration_test def test_module_constant_type_mismatch_not_subscriptable(): code_py = """