Adds a separte typing system #3
@ -83,8 +83,12 @@ def expression(ctx: 'Context', inp: ourlang.Expression) -> 'TypeVar':
|
||||
|
||||
if isinstance(inp, ourlang.FunctionCall):
|
||||
assert inp.function.returns_type_var is not None
|
||||
if inp.function.posonlyargs:
|
||||
raise NotImplementedError('TODO: Functions with arguments')
|
||||
|
||||
for param, expr in zip(inp.function.posonlyargs, inp.arguments):
|
||||
assert param.type_var is not None
|
||||
|
||||
arg = expression(ctx, expr)
|
||||
ctx.unify(param.type_var, arg)
|
||||
|
||||
return inp.function.returns_type_var
|
||||
|
||||
@ -105,6 +109,8 @@ def function(ctx: 'Context', inp: ourlang.Function) -> None:
|
||||
def module_constant_def(ctx: 'Context', inp: ourlang.ModuleConstantDef) -> None:
|
||||
inp.type_var = _convert_old_type(ctx, inp.type, inp.name)
|
||||
constant(ctx, inp.constant)
|
||||
|
||||
assert inp.constant.type_var is not None
|
||||
ctx.unify(inp.type_var, inp.constant.type_var)
|
||||
|
||||
def module(inp: ourlang.Module) -> None:
|
||||
|
||||
@ -3,6 +3,7 @@ import pytest
|
||||
from .helpers import Suite
|
||||
|
||||
ALL_INT_TYPES = ['u8', 'u32', 'u64', 'i32', 'i64']
|
||||
COMLETE_INT_TYPES = ['u32', 'u64', 'i32', 'i64']
|
||||
ALL_FLOAT_TYPES = ['f32', 'f64']
|
||||
|
||||
TYPE_MAP = {
|
||||
@ -45,7 +46,7 @@ def testEntry() -> {type_}:
|
||||
assert TYPE_MAP[type_] == type(result.returned_value)
|
||||
|
||||
@pytest.mark.integration_test
|
||||
@pytest.mark.parametrize('type_', ALL_INT_TYPES)
|
||||
@pytest.mark.parametrize('type_', COMLETE_INT_TYPES)
|
||||
def test_addition_int(type_):
|
||||
code_py = f"""
|
||||
@exported
|
||||
@ -73,7 +74,7 @@ def testEntry() -> {type_}:
|
||||
assert TYPE_MAP[type_] == type(result.returned_value)
|
||||
|
||||
@pytest.mark.integration_test
|
||||
@pytest.mark.parametrize('type_', ALL_INT_TYPES)
|
||||
@pytest.mark.parametrize('type_', COMLETE_INT_TYPES)
|
||||
def test_subtraction_int(type_):
|
||||
code_py = f"""
|
||||
@exported
|
||||
@ -390,8 +391,8 @@ def helper(left: i32, right: i32) -> i32:
|
||||
assert 7 == result.returned_value
|
||||
|
||||
@pytest.mark.integration_test
|
||||
@pytest.mark.parametrize('type_', COMPLETE_SIMPLE_TYPES)
|
||||
def test_call_with_expression(type_):
|
||||
@pytest.mark.parametrize('type_', COMLETE_INT_TYPES)
|
||||
def test_call_with_expression_int(type_):
|
||||
code_py = f"""
|
||||
@exported
|
||||
def testEntry() -> {type_}:
|
||||
@ -406,6 +407,23 @@ def helper(left: {type_}, right: {type_}) -> {type_}:
|
||||
assert 22 == result.returned_value
|
||||
assert TYPE_MAP[type_] == type(result.returned_value)
|
||||
|
||||
@pytest.mark.integration_test
|
||||
@pytest.mark.parametrize('type_', ALL_FLOAT_TYPES)
|
||||
def test_call_with_expression_float(type_):
|
||||
code_py = f"""
|
||||
@exported
|
||||
def testEntry() -> {type_}:
|
||||
return helper(10.078125 + 90.046875, 63.0 + 5.0)
|
||||
|
||||
def helper(left: {type_}, right: {type_}) -> {type_}:
|
||||
return left - right
|
||||
"""
|
||||
|
||||
result = Suite(code_py).run_code()
|
||||
|
||||
assert 32.125 == result.returned_value
|
||||
assert TYPE_MAP[type_] == type(result.returned_value)
|
||||
|
||||
@pytest.mark.integration_test
|
||||
@pytest.mark.skip('Not yet implemented')
|
||||
def test_assign():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user