Compare commits

...

2 Commits

Author SHA1 Message Date
Johan B.W. de Vries
4a4c62c728 Support static array of tuples 2023-11-15 11:40:24 +01:00
Johan B.W. de Vries
fcbd32a880 Support using structs in tuples 2023-11-15 11:36:25 +01:00
6 changed files with 60 additions and 25 deletions

View File

@ -39,7 +39,7 @@ def type3(inp: Type3OrPlaceholder) -> str:
assert isinstance(inp.args[0], Type3), TYPE3_ASSERTION_ERROR assert isinstance(inp.args[0], Type3), TYPE3_ASSERTION_ERROR
assert isinstance(inp.args[1], type3types.IntType3), TYPE3_ASSERTION_ERROR assert isinstance(inp.args[1], type3types.IntType3), TYPE3_ASSERTION_ERROR
return inp.args[0].name + '[' + inp.args[1].name + ']' return type3(inp.args[0]) + '[' + inp.args[1].name + ']'
return inp.name return inp.name

View File

@ -194,7 +194,7 @@ def tuple_instantiation(wgn: WasmGenerator, inp: ourlang.TupleInstantiation) ->
assert element.type3 == exp_type3 assert element.type3 == exp_type3
if isinstance(exp_type3, type3types.AppliedType3) and exp_type3.base is type3types.tuple: if isinstance(exp_type3, type3types.StructType3) or isinstance(exp_type3, type3types.AppliedType3):
mtyp = 'i32' mtyp = 'i32'
else: else:
assert isinstance(exp_type3, type3types.PrimitiveType3), NotImplementedError('Tuple of applied types / structs') assert isinstance(exp_type3, type3types.PrimitiveType3), NotImplementedError('Tuple of applied types / structs')
@ -859,7 +859,7 @@ def _generate_struct_constructor(wgn: WasmGenerator, inp: ourlang.StructConstruc
# Store each member individually # Store each member individually
for memname, mtyp3 in inp.struct_type3.members.items(): for memname, mtyp3 in inp.struct_type3.members.items():
mtyp: Optional[str] mtyp: Optional[str]
if isinstance(mtyp3, type3types.StructType3): if isinstance(mtyp3, type3types.StructType3) or isinstance(mtyp3, type3types.AppliedType3):
mtyp = 'i32' mtyp = 'i32'
else: else:
mtyp = LOAD_STORE_TYPE_MAP.get(mtyp3.name) mtyp = LOAD_STORE_TYPE_MAP.get(mtyp3.name)

View File

@ -431,7 +431,7 @@ class OurVisitor:
arguments = [ arguments = [
self.visit_Module_FunctionDef_expr(module, function, our_locals, arg_node) self.visit_Module_FunctionDef_expr(module, function, our_locals, arg_node)
for arg_node in node.elts for arg_node in node.elts
if isinstance(arg_node, (ast.Constant, ast.Tuple, )) if isinstance(arg_node, (ast.Constant, ast.Tuple, ast.Call, ))
] ]
if len(arguments) != len(node.elts): if len(arguments) != len(node.elts):
@ -644,8 +644,6 @@ class OurVisitor:
_raise_static_error(node, f'Unrecognized type {node.id}') _raise_static_error(node, f'Unrecognized type {node.id}')
if isinstance(node, ast.Subscript): if isinstance(node, ast.Subscript):
if not isinstance(node.value, ast.Name):
_raise_static_error(node, 'Must be name')
if isinstance(node.slice, ast.Slice): if isinstance(node.slice, ast.Slice):
_raise_static_error(node, 'Must subscript using an index') _raise_static_error(node, 'Must subscript using an index')
if not isinstance(node.slice, ast.Constant): if not isinstance(node.slice, ast.Constant):
@ -655,9 +653,6 @@ class OurVisitor:
if not isinstance(node.ctx, ast.Load): if not isinstance(node.ctx, ast.Load):
_raise_static_error(node, 'Must be load context') _raise_static_error(node, 'Must be load context')
if node.value.id not in type3types.LOOKUP_TABLE: # FIXME: Tuple of tuples?
_raise_static_error(node, f'Unrecognized type {node.value.id}')
return type3types.AppliedType3( return type3types.AppliedType3(
type3types.static_array, type3types.static_array,
[self.visit_type(module, node.value), type3types.IntType3(node.slice.value)], [self.visit_type(module, node.value), type3types.IntType3(node.slice.value)],

View File

@ -47,22 +47,26 @@ def generate_assertion_expect_type_error(result, error_msg, error_comment = None
result.append(f'assert {repr(error_msg)} == exc_info.value.args[0][0].msg') result.append(f'assert {repr(error_msg)} == exc_info.value.args[0][0].msg')
result.append(f'assert {repr(error_comment)} == exc_info.value.args[0][0].comment') result.append(f'assert {repr(error_comment)} == exc_info.value.args[0][0].comment')
def json_does_not_support_byte_values_fix(inp: Dict[Any, Any]): def json_does_not_support_byte_or_tuple_values_fix(inp: Any):
key_names = list(inp) if isinstance(inp, (int, float, )):
for key in key_names: return inp
value = inp[key]
if isinstance(value, str): if isinstance(inp, str):
if value.startswith('bytes:'): if inp.startswith('bytes:'):
inp[key] = value[6:].encode() return inp[6:].encode()
continue return inp
if isinstance(value, dict): if isinstance(inp, list):
json_does_not_support_byte_values_fix(value) return tuple(map(json_does_not_support_byte_or_tuple_values_fix, inp))
continue
if isinstance(value, list): if isinstance(inp, dict):
raise NotImplementedError key_names = list(inp)
return {
key: json_does_not_support_byte_or_tuple_values_fix(val)
for key, val in inp.items()
}
raise NotImplementedError(inp)
def generate_assertions(settings, result_code): def generate_assertions(settings, result_code):
result = [] result = []
@ -75,13 +79,11 @@ def generate_assertions(settings, result_code):
} }
if 'PYTHON' in settings: if 'PYTHON' in settings:
locals_.update(settings['PYTHON']) locals_.update(json_does_not_support_byte_or_tuple_values_fix(settings['PYTHON']))
if 'VAL0' not in locals_: if 'VAL0' not in locals_:
locals_['VAL0'] = eval(settings['VAL0']) locals_['VAL0'] = eval(settings['VAL0'])
json_does_not_support_byte_values_fix(locals_)
exec(result_code, {}, locals_) exec(result_code, {}, locals_)
return ' ' + '\n '.join(result) + '\n' return ' ' + '\n '.join(result) + '\n'

View File

@ -0,0 +1,5 @@
{
"TYPE_NAME": "static_array_tuple_u32_u32_3",
"TYPE": "(u32, u32, )[3]",
"VAL0": "((1, 100, ), (2, 200, ), (3, 300, ), )"
}

View File

@ -0,0 +1,33 @@
{
"TYPE_NAME": "static_array_with_structs",
"TYPE": "StructMain[3]",
"VAL0": "(StructMain(1, (StructCode(-4), 4, 4.0, ), (StructCode(-1), StructCode(-2), )), StructMain(2, (StructCode(-16), 16, 16.0, ), (StructCode(3), StructCode(14), )), StructMain(3, (StructCode(-256), 256, 256.0, ), (StructCode(-9), StructCode(-98), )), )",
"CODE_HEADER": [
"class StructCode:",
" code: i32",
"",
"class StructMain:",
" val00: u8",
" val01: (StructCode, u64, f32, )",
" val02: StructCode[2]"
],
"PYTHON": {
"VAL0": [
{
"val00": 1,
"val01": [{"code": -4}, 4, 4.0],
"val02": [{"code": -1}, {"code": -2}]
},
{
"val00": 2,
"val01": [{"code": -16}, 16, 16.0],
"val02": [{"code": 3}, {"code": 14}]
},
{
"val00": 3,
"val01": [{"code": -256}, 256, 256.0],
"val02": [{"code": -9}, {"code": -98}]
}
]
}
}