Compare commits
2 Commits
6b66935c67
...
4f19a5e503
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f19a5e503 | ||
|
|
0953975980 |
5
TODO.md
5
TODO.md
@ -28,3 +28,8 @@
|
|||||||
- Functions don't seem to be a thing on typing level yet?
|
- Functions don't seem to be a thing on typing level yet?
|
||||||
- Related to the FIXME in phasm_type3?
|
- Related to the FIXME in phasm_type3?
|
||||||
- Type constuctor should also be able to constuct placeholders - somehow.
|
- Type constuctor should also be able to constuct placeholders - somehow.
|
||||||
|
|
||||||
|
|
||||||
|
- Check placeholders only used in constraints and constraintgenerator
|
||||||
|
- Simply typed check in compiler:
|
||||||
|
- isinstance(element.type3, type3types.Type3) => element.type3 is not None
|
||||||
|
|||||||
@ -6,6 +6,7 @@ It's intented to be a "any color, as long as it's black" kind of renderer
|
|||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
|
||||||
from . import ourlang, prelude
|
from . import ourlang, prelude
|
||||||
|
from .type3.placeholders import TYPE3_ASSERTION_ERROR, Type3OrPlaceholder
|
||||||
from .type3.types import Type3
|
from .type3.types import Type3
|
||||||
|
|
||||||
|
|
||||||
@ -17,10 +18,12 @@ def phasm_render(inp: ourlang.Module) -> str:
|
|||||||
|
|
||||||
Statements = Generator[str, None, None]
|
Statements = Generator[str, None, None]
|
||||||
|
|
||||||
def type3(inp: Type3) -> str:
|
def type3(inp: Type3OrPlaceholder) -> str:
|
||||||
"""
|
"""
|
||||||
Render: type's name
|
Render: type's name
|
||||||
"""
|
"""
|
||||||
|
assert isinstance(inp, Type3), TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp is prelude.none:
|
if inp is prelude.none:
|
||||||
return 'None'
|
return 'None'
|
||||||
|
|
||||||
|
|||||||
@ -9,12 +9,11 @@ from .runtime import calculate_alloc_size, calculate_member_offset
|
|||||||
from .stdlib import alloc as stdlib_alloc
|
from .stdlib import alloc as stdlib_alloc
|
||||||
from .stdlib import types as stdlib_types
|
from .stdlib import types as stdlib_types
|
||||||
from .type3 import functions as type3functions
|
from .type3 import functions as type3functions
|
||||||
|
from .type3 import placeholders as type3placeholders
|
||||||
from .type3 import typeclasses as type3classes
|
from .type3 import typeclasses as type3classes
|
||||||
from .type3 import types as type3types
|
from .type3 import types as type3types
|
||||||
from .wasmgenerator import Generator as WasmGenerator
|
from .wasmgenerator import Generator as WasmGenerator
|
||||||
|
|
||||||
TYPE3_ASSERTION_ERROR = 'You must call phasm_type3 after calling phasm_parse before your program can be compiled'
|
|
||||||
|
|
||||||
LOAD_STORE_TYPE_MAP = {
|
LOAD_STORE_TYPE_MAP = {
|
||||||
'i8': 'i32', # Have to use an u32, since there is no native i8 type
|
'i8': 'i32', # Have to use an u32, since there is no native i8 type
|
||||||
'u8': 'i32', # Have to use an u32, since there is no native u8 type
|
'u8': 'i32', # Have to use an u32, since there is no native u8 type
|
||||||
@ -269,14 +268,14 @@ def phasm_compile(inp: ourlang.Module) -> wasm.Module:
|
|||||||
"""
|
"""
|
||||||
return module(inp)
|
return module(inp)
|
||||||
|
|
||||||
def type3(inp: type3types.Type3) -> wasm.WasmType:
|
def type3(inp: type3placeholders.Type3OrPlaceholder) -> wasm.WasmType:
|
||||||
"""
|
"""
|
||||||
Compile: type
|
Compile: type
|
||||||
|
|
||||||
Types are used for example in WebAssembly function parameters
|
Types are used for example in WebAssembly function parameters
|
||||||
and return types.
|
and return types.
|
||||||
"""
|
"""
|
||||||
assert inp is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp == prelude.none:
|
if inp == prelude.none:
|
||||||
return wasm.WasmTypeNone()
|
return wasm.WasmTypeNone()
|
||||||
@ -328,7 +327,7 @@ def tuple_instantiation(wgn: WasmGenerator, inp: ourlang.TupleInstantiation) ->
|
|||||||
"""
|
"""
|
||||||
Compile: Instantiation (allocation) of a tuple
|
Compile: Instantiation (allocation) of a tuple
|
||||||
"""
|
"""
|
||||||
assert inp.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3)
|
||||||
|
|
||||||
args: list[type3types.Type3] = []
|
args: list[type3types.Type3] = []
|
||||||
|
|
||||||
@ -346,7 +345,7 @@ def tuple_instantiation(wgn: WasmGenerator, inp: ourlang.TupleInstantiation) ->
|
|||||||
|
|
||||||
comment_elements = ''
|
comment_elements = ''
|
||||||
for element in inp.elements:
|
for element in inp.elements:
|
||||||
assert element.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(element.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
comment_elements += f'{element.type3.name}, '
|
comment_elements += f'{element.type3.name}, '
|
||||||
|
|
||||||
tmp_var = wgn.temp_var_i32('tuple_adr')
|
tmp_var = wgn.temp_var_i32('tuple_adr')
|
||||||
@ -360,11 +359,17 @@ def tuple_instantiation(wgn: WasmGenerator, inp: ourlang.TupleInstantiation) ->
|
|||||||
# Store each element individually
|
# Store each element individually
|
||||||
offset = 0
|
offset = 0
|
||||||
for element, exp_type3 in zip(inp.elements, args, strict=True):
|
for element, exp_type3 in zip(inp.elements, args, strict=True):
|
||||||
|
if isinstance(exp_type3, type3placeholders.PlaceholderForType):
|
||||||
|
assert exp_type3.resolve_as is not None
|
||||||
|
assert isinstance(exp_type3.resolve_as, type3types.Type3)
|
||||||
|
exp_type3 = exp_type3.resolve_as
|
||||||
|
|
||||||
assert element.type3 == exp_type3
|
assert element.type3 == exp_type3
|
||||||
|
|
||||||
if (prelude.InternalPassAsPointer, (exp_type3, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
if (prelude.InternalPassAsPointer, (exp_type3, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
||||||
mtyp = 'i32'
|
mtyp = 'i32'
|
||||||
else:
|
else:
|
||||||
|
assert isinstance(exp_type3, type3types.Type3), NotImplementedError('Tuple of applied types / structs')
|
||||||
mtyp = LOAD_STORE_TYPE_MAP[exp_type3.name]
|
mtyp = LOAD_STORE_TYPE_MAP[exp_type3.name]
|
||||||
|
|
||||||
wgn.add_statement('nop', comment='PRE')
|
wgn.add_statement('nop', comment='PRE')
|
||||||
@ -387,7 +392,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
raise Exception
|
raise Exception
|
||||||
|
|
||||||
if isinstance(inp, ourlang.ConstantPrimitive):
|
if isinstance(inp, ourlang.ConstantPrimitive):
|
||||||
assert inp.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp.type3 in (prelude.i8, prelude.u8, ):
|
if inp.type3 in (prelude.i8, prelude.u8, ):
|
||||||
# No native u8 type - treat as i32, with caution
|
# No native u8 type - treat as i32, with caution
|
||||||
@ -428,7 +433,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(inp.variable, ourlang.ModuleConstantDef):
|
if isinstance(inp.variable, ourlang.ModuleConstantDef):
|
||||||
assert inp.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if (prelude.InternalPassAsPointer, (inp.type3, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
if (prelude.InternalPassAsPointer, (inp.type3, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
||||||
assert isinstance(inp.variable.constant, (ourlang.ConstantBytes, ourlang.ConstantStruct, ourlang.ConstantTuple, ))
|
assert isinstance(inp.variable.constant, (ourlang.ConstantBytes, ourlang.ConstantStruct, ourlang.ConstantTuple, ))
|
||||||
@ -438,8 +443,11 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
wgn.i32.const(address)
|
wgn.i32.const(address)
|
||||||
return
|
return
|
||||||
|
|
||||||
expression(wgn, inp.variable.constant)
|
if isinstance(inp.type3, type3types.Type3):
|
||||||
return
|
expression(wgn, inp.variable.constant)
|
||||||
|
return
|
||||||
|
|
||||||
|
raise NotImplementedError(expression, inp)
|
||||||
|
|
||||||
raise NotImplementedError(expression, inp.variable)
|
raise NotImplementedError(expression, inp.variable)
|
||||||
|
|
||||||
@ -447,7 +455,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
expression(wgn, inp.left)
|
expression(wgn, inp.left)
|
||||||
expression(wgn, inp.right)
|
expression(wgn, inp.right)
|
||||||
|
|
||||||
assert inp.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
type_var_map: Dict[type3functions.TypeVariable, type3types.Type3] = {}
|
type_var_map: Dict[type3functions.TypeVariable, type3types.Type3] = {}
|
||||||
|
|
||||||
@ -456,7 +464,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
# Fixed type, not part of the lookup requirements
|
# Fixed type, not part of the lookup requirements
|
||||||
continue
|
continue
|
||||||
|
|
||||||
assert arg_expr.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(arg_expr.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
type_var_map[type_var] = arg_expr.type3
|
type_var_map[type_var] = arg_expr.type3
|
||||||
|
|
||||||
instance_key = ','.join(
|
instance_key = ','.join(
|
||||||
@ -484,7 +492,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
# Fixed type, not part of the lookup requirements
|
# Fixed type, not part of the lookup requirements
|
||||||
continue
|
continue
|
||||||
|
|
||||||
assert arg_expr.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(arg_expr.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
type_var_map[type_var] = arg_expr.type3
|
type_var_map[type_var] = arg_expr.type3
|
||||||
|
|
||||||
instance_key = ','.join(
|
instance_key = ','.join(
|
||||||
@ -507,7 +515,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(inp, ourlang.Subscript):
|
if isinstance(inp, ourlang.Subscript):
|
||||||
assert inp.varref.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.varref.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp.varref.type3 is prelude.bytes_:
|
if inp.varref.type3 is prelude.bytes_:
|
||||||
expression(wgn, inp.varref)
|
expression(wgn, inp.varref)
|
||||||
@ -515,7 +523,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
wgn.call(stdlib_types.__subscript_bytes__)
|
wgn.call(stdlib_types.__subscript_bytes__)
|
||||||
return
|
return
|
||||||
|
|
||||||
assert inp.varref.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.varref.type3, type3types.Type3)
|
||||||
|
|
||||||
sa_args = prelude.static_array.did_construct(inp.varref.type3)
|
sa_args = prelude.static_array.did_construct(inp.varref.type3)
|
||||||
if sa_args is not None:
|
if sa_args is not None:
|
||||||
@ -541,6 +549,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
wgn.i32.mul()
|
wgn.i32.mul()
|
||||||
wgn.i32.add()
|
wgn.i32.add()
|
||||||
|
|
||||||
|
assert isinstance(el_type, type3types.Type3), NotImplementedError('Tuple of applied types / structs')
|
||||||
mtyp = LOAD_STORE_TYPE_MAP[el_type.name]
|
mtyp = LOAD_STORE_TYPE_MAP[el_type.name]
|
||||||
|
|
||||||
wgn.add_statement(f'{mtyp}.load')
|
wgn.add_statement(f'{mtyp}.load')
|
||||||
@ -553,17 +562,18 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
for el_type in tp_args[0:inp.index.value]:
|
for el_type in tp_args[0:inp.index.value]:
|
||||||
assert el_type is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(el_type, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
offset += calculate_alloc_size(el_type)
|
offset += calculate_alloc_size(el_type)
|
||||||
|
|
||||||
el_type = tp_args[inp.index.value]
|
el_type = tp_args[inp.index.value]
|
||||||
assert el_type is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(el_type, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
expression(wgn, inp.varref)
|
expression(wgn, inp.varref)
|
||||||
|
|
||||||
if (prelude.InternalPassAsPointer, (el_type, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
if (prelude.InternalPassAsPointer, (el_type, )) in prelude.PRELUDE_TYPE_CLASS_INSTANCES_EXISTING:
|
||||||
mtyp = 'i32'
|
mtyp = 'i32'
|
||||||
else:
|
else:
|
||||||
|
assert isinstance(el_type, type3types.Type3), NotImplementedError('Tuple of applied types / structs')
|
||||||
mtyp = LOAD_STORE_TYPE_MAP[el_type.name]
|
mtyp = LOAD_STORE_TYPE_MAP[el_type.name]
|
||||||
|
|
||||||
wgn.add_statement(f'{mtyp}.load', f'offset={offset}')
|
wgn.add_statement(f'{mtyp}.load', f'offset={offset}')
|
||||||
@ -572,13 +582,14 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
raise NotImplementedError(expression, inp, inp.varref.type3)
|
raise NotImplementedError(expression, inp, inp.varref.type3)
|
||||||
|
|
||||||
if isinstance(inp, ourlang.AccessStructMember):
|
if isinstance(inp, ourlang.AccessStructMember):
|
||||||
assert inp.struct_type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.struct_type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
st_args = prelude.struct.did_construct(inp.struct_type3)
|
st_args = prelude.struct.did_construct(inp.struct_type3)
|
||||||
assert st_args is not None
|
assert st_args is not None
|
||||||
|
|
||||||
member_type = st_args[inp.member]
|
member_type = st_args[inp.member]
|
||||||
|
|
||||||
|
assert isinstance(member_type, type3types.Type3), NotImplementedError('Tuple of applied types / structs')
|
||||||
mtyp = LOAD_STORE_TYPE_MAP[member_type.name]
|
mtyp = LOAD_STORE_TYPE_MAP[member_type.name]
|
||||||
|
|
||||||
expression(wgn, inp.varref)
|
expression(wgn, inp.varref)
|
||||||
@ -597,7 +608,7 @@ def expression_fold(wgn: WasmGenerator, inp: ourlang.Fold) -> None:
|
|||||||
"""
|
"""
|
||||||
Compile: Fold expression
|
Compile: Fold expression
|
||||||
"""
|
"""
|
||||||
assert inp.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp.iter.type3 is not prelude.bytes_:
|
if inp.iter.type3 is not prelude.bytes_:
|
||||||
raise NotImplementedError(expression_fold, inp, inp.iter.type3)
|
raise NotImplementedError(expression_fold, inp, inp.iter.type3)
|
||||||
@ -828,7 +839,7 @@ def module_data(inp: ourlang.ModuleData) -> bytes:
|
|||||||
data_list: List[bytes] = []
|
data_list: List[bytes] = []
|
||||||
|
|
||||||
for constant in block.data:
|
for constant in block.data:
|
||||||
assert constant.type3 is not None, TYPE3_ASSERTION_ERROR
|
assert isinstance(constant.type3, type3types.Type3), (id(constant), type3placeholders.TYPE3_ASSERTION_ERROR)
|
||||||
|
|
||||||
if isinstance(constant, ourlang.ConstantMemoryStored) and block is not constant.data_block:
|
if isinstance(constant, ourlang.ConstantMemoryStored) and block is not constant.data_block:
|
||||||
# It's stored in a different block
|
# It's stored in a different block
|
||||||
|
|||||||
@ -163,8 +163,8 @@ class OurVisitor:
|
|||||||
|
|
||||||
arg_type = self.visit_type(module, arg.annotation)
|
arg_type = self.visit_type(module, arg.annotation)
|
||||||
|
|
||||||
# FIXME: Allow TypeVariable in the function signature
|
# if isisntance(arg_type, TypeVariable):
|
||||||
# This would also require FunctionParam to accept a placeholder
|
# arg_type = PlaceHolderForType()
|
||||||
|
|
||||||
function.signature.args.append(arg_type)
|
function.signature.args.append(arg_type)
|
||||||
function.posonlyargs.append(FunctionParam(
|
function.posonlyargs.append(FunctionParam(
|
||||||
|
|||||||
@ -33,13 +33,13 @@ class RequireTypeSubstitutes:
|
|||||||
typing of the program, so this constraint can be updated.
|
typing of the program, so this constraint can be updated.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SubstitutionMap = Dict[placeholders.PlaceholderForType, types.Type3]
|
SubstitutionMap = Dict[PlaceholderForType, types.Type3]
|
||||||
|
|
||||||
NewConstraintList = List['ConstraintBase']
|
NewConstraintList = List['ConstraintBase']
|
||||||
|
|
||||||
CheckResult = Union[None, SubstitutionMap, Error, NewConstraintList, RequireTypeSubstitutes]
|
CheckResult = Union[None, SubstitutionMap, Error, NewConstraintList, RequireTypeSubstitutes]
|
||||||
|
|
||||||
HumanReadableRet = Tuple[str, Dict[str, Union[str, ourlang.Expression, types.Type3, placeholders.PlaceholderForType]]]
|
HumanReadableRet = Tuple[str, Dict[str, Union[str, ourlang.Expression, types.Type3, PlaceholderForType]]]
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
"""
|
"""
|
||||||
@ -99,9 +99,9 @@ class SameTypeConstraint(ConstraintBase):
|
|||||||
"""
|
"""
|
||||||
__slots__ = ('type_list', )
|
__slots__ = ('type_list', )
|
||||||
|
|
||||||
type_list: List[placeholders.Type3OrPlaceholder]
|
type_list: List[PlaceholderForType]
|
||||||
|
|
||||||
def __init__(self, *type_list: placeholders.Type3OrPlaceholder, comment: Optional[str] = None) -> None:
|
def __init__(self, *type_list: PlaceholderForType, comment: Optional[str] = None) -> None:
|
||||||
super().__init__(comment=comment)
|
super().__init__(comment=comment)
|
||||||
|
|
||||||
assert len(type_list) > 1
|
assert len(type_list) > 1
|
||||||
@ -111,18 +111,10 @@ class SameTypeConstraint(ConstraintBase):
|
|||||||
known_types: List[types.Type3] = []
|
known_types: List[types.Type3] = []
|
||||||
phft_list = []
|
phft_list = []
|
||||||
for typ in self.type_list:
|
for typ in self.type_list:
|
||||||
if isinstance(typ, types.Type3):
|
if typ.resolve_as is not None:
|
||||||
known_types.append(typ)
|
known_types.append(typ.resolve_as)
|
||||||
continue
|
else:
|
||||||
|
phft_list.append(typ)
|
||||||
if isinstance(typ, placeholders.PlaceholderForType):
|
|
||||||
if typ.resolve_as is not None:
|
|
||||||
known_types.append(typ.resolve_as)
|
|
||||||
else:
|
|
||||||
phft_list.append(typ)
|
|
||||||
continue
|
|
||||||
|
|
||||||
raise NotImplementedError(typ)
|
|
||||||
|
|
||||||
if not known_types:
|
if not known_types:
|
||||||
return RequireTypeSubstitutes()
|
return RequireTypeSubstitutes()
|
||||||
@ -158,7 +150,7 @@ class SameTypeConstraint(ConstraintBase):
|
|||||||
return f'SameTypeConstraint({args}, comment={repr(self.comment)})'
|
return f'SameTypeConstraint({args}, comment={repr(self.comment)})'
|
||||||
|
|
||||||
class TupleMatchConstraint(ConstraintBase):
|
class TupleMatchConstraint(ConstraintBase):
|
||||||
def __init__(self, exp_type: placeholders.Type3OrPlaceholder, args: Iterable[placeholders.Type3OrPlaceholder], comment: str):
|
def __init__(self, exp_type: PlaceholderForType, args: Iterable[PlaceholderForType], comment: str):
|
||||||
super().__init__(comment=comment)
|
super().__init__(comment=comment)
|
||||||
|
|
||||||
self.exp_type = exp_type
|
self.exp_type = exp_type
|
||||||
@ -166,7 +158,7 @@ class TupleMatchConstraint(ConstraintBase):
|
|||||||
|
|
||||||
def check(self) -> CheckResult:
|
def check(self) -> CheckResult:
|
||||||
exp_type = self.exp_type
|
exp_type = self.exp_type
|
||||||
if isinstance(exp_type, placeholders.PlaceholderForType):
|
if isinstance(exp_type, PlaceholderForType):
|
||||||
if exp_type.resolve_as is None:
|
if exp_type.resolve_as is None:
|
||||||
return RequireTypeSubstitutes()
|
return RequireTypeSubstitutes()
|
||||||
|
|
||||||
@ -206,13 +198,13 @@ class MustImplementTypeClassConstraint(ConstraintBase):
|
|||||||
|
|
||||||
context: Context
|
context: Context
|
||||||
type_class3: Union[str, typeclasses.Type3Class]
|
type_class3: Union[str, typeclasses.Type3Class]
|
||||||
types: list[placeholders.Type3OrPlaceholder]
|
types: list[PlaceholderForType]
|
||||||
|
|
||||||
DATA = {
|
DATA = {
|
||||||
'bytes': {'Foldable'},
|
'bytes': {'Foldable'},
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, context: Context, type_class3: Union[str, typeclasses.Type3Class], types: list[placeholders.Type3OrPlaceholder], comment: Optional[str] = None) -> None:
|
def __init__(self, context: Context, type_class3: Union[str, typeclasses.Type3Class], types: list[PlaceholderForType], comment: Optional[str] = None) -> None:
|
||||||
super().__init__(comment=comment)
|
super().__init__(comment=comment)
|
||||||
|
|
||||||
self.context = context
|
self.context = context
|
||||||
@ -222,10 +214,10 @@ class MustImplementTypeClassConstraint(ConstraintBase):
|
|||||||
def check(self) -> CheckResult:
|
def check(self) -> CheckResult:
|
||||||
typ_list = []
|
typ_list = []
|
||||||
for typ in self.types:
|
for typ in self.types:
|
||||||
if isinstance(typ, placeholders.PlaceholderForType) and typ.resolve_as is not None:
|
if isinstance(typ, PlaceholderForType) and typ.resolve_as is not None:
|
||||||
typ = typ.resolve_as
|
typ = typ.resolve_as
|
||||||
|
|
||||||
if isinstance(typ, placeholders.PlaceholderForType):
|
if isinstance(typ, PlaceholderForType):
|
||||||
return RequireTypeSubstitutes()
|
return RequireTypeSubstitutes()
|
||||||
|
|
||||||
typ_list.append(typ)
|
typ_list.append(typ)
|
||||||
@ -267,12 +259,12 @@ class LiteralFitsConstraint(ConstraintBase):
|
|||||||
"""
|
"""
|
||||||
__slots__ = ('type3', 'literal', )
|
__slots__ = ('type3', 'literal', )
|
||||||
|
|
||||||
type3: placeholders.Type3OrPlaceholder
|
type3: PlaceholderForType
|
||||||
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct]
|
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
type3: placeholders.Type3OrPlaceholder,
|
type3: PlaceholderForType,
|
||||||
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct],
|
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct],
|
||||||
comment: Optional[str] = None,
|
comment: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -296,7 +288,7 @@ class LiteralFitsConstraint(ConstraintBase):
|
|||||||
'f64': None,
|
'f64': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
if isinstance(self.type3, placeholders.PlaceholderForType):
|
if isinstance(self.type3, PlaceholderForType):
|
||||||
if self.type3.resolve_as is None:
|
if self.type3.resolve_as is None:
|
||||||
return RequireTypeSubstitutes()
|
return RequireTypeSubstitutes()
|
||||||
|
|
||||||
@ -436,17 +428,17 @@ class CanBeSubscriptedConstraint(ConstraintBase):
|
|||||||
"""
|
"""
|
||||||
__slots__ = ('ret_type3', 'type3', 'index', 'index_phft', )
|
__slots__ = ('ret_type3', 'type3', 'index', 'index_phft', )
|
||||||
|
|
||||||
ret_type3: placeholders.Type3OrPlaceholder
|
ret_type3: PlaceholderForType
|
||||||
type3: placeholders.Type3OrPlaceholder
|
type3: PlaceholderForType
|
||||||
index: ourlang.Expression
|
index: ourlang.Expression
|
||||||
index_phft: placeholders.Type3OrPlaceholder
|
index_phft: PlaceholderForType
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
ret_type3: placeholders.PlaceholderForType,
|
ret_type3: PlaceholderForType,
|
||||||
type3: placeholders.PlaceholderForType,
|
type3: PlaceholderForType,
|
||||||
index: ourlang.Expression,
|
index: ourlang.Expression,
|
||||||
index_phft: placeholders.PlaceholderForType,
|
index_phft: PlaceholderForType,
|
||||||
comment: Optional[str] = None,
|
comment: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(comment=comment)
|
super().__init__(comment=comment)
|
||||||
@ -458,7 +450,7 @@ class CanBeSubscriptedConstraint(ConstraintBase):
|
|||||||
|
|
||||||
def check(self) -> CheckResult:
|
def check(self) -> CheckResult:
|
||||||
exp_type = self.type3
|
exp_type = self.type3
|
||||||
if isinstance(exp_type, placeholders.PlaceholderForType):
|
if isinstance(exp_type, PlaceholderForType):
|
||||||
if exp_type.resolve_as is None:
|
if exp_type.resolve_as is None:
|
||||||
return RequireTypeSubstitutes()
|
return RequireTypeSubstitutes()
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@ from typing import Any, Iterable, List, Optional, Protocol, Union
|
|||||||
|
|
||||||
from .types import Type3
|
from .types import Type3
|
||||||
|
|
||||||
|
TYPE3_ASSERTION_ERROR = 'You must call phasm_type3 after calling phasm_parse before you can call any other method'
|
||||||
|
|
||||||
class ExpressionProtocol(Protocol):
|
class ExpressionProtocol(Protocol):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -5,6 +5,7 @@ from typing import Any, Generator, Iterable, List, TextIO, Union
|
|||||||
from phasm import compiler, prelude
|
from phasm import compiler, prelude
|
||||||
from phasm.codestyle import phasm_render
|
from phasm.codestyle import phasm_render
|
||||||
from phasm.runtime import calculate_alloc_size
|
from phasm.runtime import calculate_alloc_size
|
||||||
|
from phasm.type3 import placeholders as type3placeholders
|
||||||
from phasm.type3 import types as type3types
|
from phasm.type3 import types as type3types
|
||||||
|
|
||||||
from . import runners
|
from . import runners
|
||||||
@ -64,6 +65,9 @@ class Suite:
|
|||||||
runner.interpreter_dump_memory(sys.stderr)
|
runner.interpreter_dump_memory(sys.stderr)
|
||||||
|
|
||||||
for arg, arg_typ in zip(args, func_args, strict=True):
|
for arg, arg_typ in zip(args, func_args, strict=True):
|
||||||
|
assert not isinstance(arg_typ, type3placeholders.PlaceholderForType), \
|
||||||
|
'Cannot call polymorphic function from outside'
|
||||||
|
|
||||||
if arg_typ in (prelude.u8, prelude.u32, prelude.u64, ):
|
if arg_typ in (prelude.u8, prelude.u32, prelude.u64, ):
|
||||||
assert isinstance(arg, int)
|
assert isinstance(arg, int)
|
||||||
wasm_args.append(arg)
|
wasm_args.append(arg)
|
||||||
@ -84,6 +88,7 @@ class Suite:
|
|||||||
wasm_args.append(adr)
|
wasm_args.append(adr)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
assert isinstance(arg_typ, type3types.Type3)
|
||||||
sa_args = prelude.static_array.did_construct(arg_typ)
|
sa_args = prelude.static_array.did_construct(arg_typ)
|
||||||
if sa_args is not None:
|
if sa_args is not None:
|
||||||
adr = _allocate_memory_stored_value(runner, arg_typ, arg)
|
adr = _allocate_memory_stored_value(runner, arg_typ, arg)
|
||||||
@ -217,6 +222,8 @@ def _allocate_memory_stored_value(
|
|||||||
|
|
||||||
offset = adr
|
offset = adr
|
||||||
for val_el_val, val_el_typ in zip(val, tp_args, strict=True):
|
for val_el_val, val_el_typ in zip(val, tp_args, strict=True):
|
||||||
|
assert not isinstance(val_el_typ, type3placeholders.PlaceholderForType)
|
||||||
|
|
||||||
offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val)
|
offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val)
|
||||||
return adr
|
return adr
|
||||||
|
|
||||||
@ -233,6 +240,8 @@ def _allocate_memory_stored_value(
|
|||||||
|
|
||||||
offset = adr
|
offset = adr
|
||||||
for val_el_name, val_el_typ in st_args.items():
|
for val_el_name, val_el_typ in st_args.items():
|
||||||
|
assert not isinstance(val_el_typ, type3placeholders.PlaceholderForType)
|
||||||
|
|
||||||
val_el_val = val[val_el_name]
|
val_el_val = val[val_el_name]
|
||||||
offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val)
|
offset += _write_memory_stored_value(runner, offset, val_el_typ, val_el_val)
|
||||||
return adr
|
return adr
|
||||||
@ -290,6 +299,8 @@ def _load_memory_stored_returned_value(
|
|||||||
|
|
||||||
return _load_bytes_from_address(runner, ret_type3, wasm_value)
|
return _load_bytes_from_address(runner, ret_type3, wasm_value)
|
||||||
|
|
||||||
|
assert isinstance(ret_type3, type3types.Type3) # Type hint
|
||||||
|
|
||||||
sa_args = prelude.static_array.did_construct(ret_type3)
|
sa_args = prelude.static_array.did_construct(ret_type3)
|
||||||
if sa_args is not None:
|
if sa_args is not None:
|
||||||
assert isinstance(wasm_value, int), wasm_value
|
assert isinstance(wasm_value, int), wasm_value
|
||||||
@ -355,6 +366,8 @@ def _unpack(runner: runners.RunnerBase, typ: type3types.Type3, inp: bytes) -> An
|
|||||||
assert len(inp) == 4
|
assert len(inp) == 4
|
||||||
adr = struct.unpack('<I', inp)[0]
|
adr = struct.unpack('<I', inp)[0]
|
||||||
|
|
||||||
|
assert isinstance(typ, type3types.Type3)
|
||||||
|
|
||||||
sa_args = prelude.static_array.did_construct(typ)
|
sa_args = prelude.static_array.did_construct(typ)
|
||||||
if sa_args is not None:
|
if sa_args is not None:
|
||||||
sa_type, sa_len = sa_args
|
sa_type, sa_len = sa_args
|
||||||
@ -391,6 +404,9 @@ def _split_read_bytes(all_bytes: bytes, split_sizes: Iterable[int]) -> Generator
|
|||||||
def _load_static_array_from_address(runner: runners.RunnerBase, sub_typ: type3types.Type3, len_typ: type3types.IntType3, adr: int) -> Any:
|
def _load_static_array_from_address(runner: runners.RunnerBase, sub_typ: type3types.Type3, len_typ: type3types.IntType3, adr: int) -> Any:
|
||||||
sys.stderr.write(f'Reading 0x{adr:08x} {sub_typ:s} {len_typ:s}\n')
|
sys.stderr.write(f'Reading 0x{adr:08x} {sub_typ:s} {len_typ:s}\n')
|
||||||
|
|
||||||
|
assert not isinstance(sub_typ, type3placeholders.PlaceholderForType)
|
||||||
|
assert isinstance(len_typ, type3types.IntType3)
|
||||||
|
|
||||||
sa_len = len_typ.value
|
sa_len = len_typ.value
|
||||||
|
|
||||||
arg_size_1 = calculate_alloc_size(sub_typ, is_member=True)
|
arg_size_1 = calculate_alloc_size(sub_typ, is_member=True)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user