Adds a separte typing system #3
@ -172,7 +172,7 @@ def module_constant_def(ctx: Context, inp: ourlang.ModuleConstantDef) -> None:
|
|||||||
if inp.type_str is None:
|
if inp.type_str is None:
|
||||||
inp.type_var = ctx.new_var()
|
inp.type_var = ctx.new_var()
|
||||||
else:
|
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
|
assert inp.constant.type_var is not None
|
||||||
ctx.unify(inp.type_var, inp.constant.type_var)
|
ctx.unify(inp.type_var, inp.constant.type_var)
|
||||||
|
|||||||
@ -483,7 +483,7 @@ def simplify(inp: TypeVar) -> Optional[str]:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def make_u8(ctx: Context, location: str) -> TypeVar:
|
def make_u8(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a u8 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(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=8, maxb=8))
|
result.add_constraint(TypeConstraintBitWidth(minb=8, maxb=8))
|
||||||
result.add_constraint(TypeConstraintSigned(False))
|
result.add_constraint(TypeConstraintSigned(False))
|
||||||
result.add_location(location)
|
result.add_location('u8')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_u32(ctx: Context, location: str) -> TypeVar:
|
def make_u32(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a u32 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(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
||||||
result.add_constraint(TypeConstraintSigned(False))
|
result.add_constraint(TypeConstraintSigned(False))
|
||||||
result.add_location(location)
|
result.add_location('u32')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_u64(ctx: Context, location: str) -> TypeVar:
|
def make_u64(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a u64 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(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
||||||
result.add_constraint(TypeConstraintSigned(False))
|
result.add_constraint(TypeConstraintSigned(False))
|
||||||
result.add_location(location)
|
result.add_location('u64')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_i32(ctx: Context, location: str) -> TypeVar:
|
def make_i32(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a i32 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(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
||||||
result.add_constraint(TypeConstraintSigned(True))
|
result.add_constraint(TypeConstraintSigned(True))
|
||||||
result.add_location(location)
|
result.add_location('i32')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_i64(ctx: Context, location: str) -> TypeVar:
|
def make_i64(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a i64 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(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.INT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
||||||
result.add_constraint(TypeConstraintSigned(True))
|
result.add_constraint(TypeConstraintSigned(True))
|
||||||
result.add_location(location)
|
result.add_location('i64')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_f32(ctx: Context, location: str) -> TypeVar:
|
def make_f32(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a f32 TypeVar
|
Makes a f32 TypeVar
|
||||||
"""
|
"""
|
||||||
result = ctx.new_var()
|
result = ctx.new_var()
|
||||||
result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT))
|
result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
result.add_constraint(TypeConstraintBitWidth(minb=32, maxb=32))
|
||||||
result.add_location(location)
|
result.add_location('f32')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def make_f64(ctx: Context, location: str) -> TypeVar:
|
def make_f64(ctx: Context) -> TypeVar:
|
||||||
"""
|
"""
|
||||||
Makes a f64 TypeVar
|
Makes a f64 TypeVar
|
||||||
"""
|
"""
|
||||||
result = ctx.new_var()
|
result = ctx.new_var()
|
||||||
result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT))
|
result.add_constraint(TypeConstraintPrimitive(TypeConstraintPrimitive.Primitive.FLOAT))
|
||||||
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
result.add_constraint(TypeConstraintBitWidth(minb=64, maxb=64))
|
||||||
result.add_location(location)
|
result.add_location('f64')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
BUILTIN_TYPES: Dict[str, Callable[[Context, str], TypeVar]] = {
|
BUILTIN_TYPES: Dict[str, Callable[[Context], TypeVar]] = {
|
||||||
'u8': make_u8,
|
'u8': make_u8,
|
||||||
'u32': make_u32,
|
'u32': make_u32,
|
||||||
'u64': make_u64,
|
'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]+)\]')
|
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
|
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.
|
with the context creation.
|
||||||
"""
|
"""
|
||||||
if inp in BUILTIN_TYPES:
|
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)
|
match = TYPE_MATCH_STATIC_ARRAY.fullmatch(inp)
|
||||||
if match:
|
if match:
|
||||||
@ -596,7 +599,11 @@ def from_str(ctx: Context, inp: str, location: str) -> TypeVar:
|
|||||||
from_str(ctx, match[1], match[1])
|
from_str(ctx, match[1], match[1])
|
||||||
for _ in range(int(match[2]))
|
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
|
return result
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,17 @@ def testEntry() -> {type_}:
|
|||||||
assert 32.125 == result.returned_value
|
assert 32.125 == result.returned_value
|
||||||
assert TYPE_MAP[type_] == type(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.integration_test
|
||||||
@pytest.mark.parametrize('type_', ALL_INT_TYPES)
|
@pytest.mark.parametrize('type_', ALL_INT_TYPES)
|
||||||
def test_module_constant_int(type_):
|
def test_module_constant_int(type_):
|
||||||
|
|||||||
@ -90,6 +90,18 @@ CONSTANT: u8[3] = (24, 57, 280, )
|
|||||||
with pytest.raises(TypingError, match='u8.*280'):
|
with pytest.raises(TypingError, match='u8.*280'):
|
||||||
Suite(code_py).run_code()
|
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
|
@pytest.mark.integration_test
|
||||||
def test_module_constant_type_mismatch_not_subscriptable():
|
def test_module_constant_type_mismatch_not_subscriptable():
|
||||||
code_py = """
|
code_py = """
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user