Compare commits
No commits in common. "0aa8207987a1fde5031ee28e6bf1017ce4733388" and "e6610a6e965afc2a497fba4696e7c2dc4f87e87c" have entirely different histories.
0aa8207987
...
e6610a6e96
@ -229,13 +229,7 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
wgn.f64.const(inp.value)
|
wgn.f64.const(inp.value)
|
||||||
return
|
return
|
||||||
|
|
||||||
raise NotImplementedError(f'Constants with type {inp.type3:s}')
|
raise NotImplementedError(f'Constants with type {inp.type3}')
|
||||||
|
|
||||||
if isinstance(inp, ourlang.ConstantBytes):
|
|
||||||
assert inp.data_block is not None, 'Bytes must be memory stored'
|
|
||||||
assert inp.data_block.address is not None, 'Value not allocated'
|
|
||||||
wgn.i32.const(inp.data_block.address)
|
|
||||||
return
|
|
||||||
|
|
||||||
if isinstance(inp, ourlang.VariableReference):
|
if isinstance(inp, ourlang.VariableReference):
|
||||||
if isinstance(inp.variable, ourlang.FunctionParam):
|
if isinstance(inp.variable, ourlang.FunctionParam):
|
||||||
@ -245,12 +239,6 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
|
|||||||
if isinstance(inp.variable, ourlang.ModuleConstantDef):
|
if isinstance(inp.variable, ourlang.ModuleConstantDef):
|
||||||
assert isinstance(inp.type3, type3types.Type3), type3types.TYPE3_ASSERTION_ERROR
|
assert isinstance(inp.type3, type3types.Type3), type3types.TYPE3_ASSERTION_ERROR
|
||||||
|
|
||||||
if inp.type3 is type3types.bytes:
|
|
||||||
assert inp.variable.data_block is not None, 'Bytes must be memory stored'
|
|
||||||
assert inp.variable.data_block.address is not None, 'Value not allocated'
|
|
||||||
wgn.i32.const(inp.variable.data_block.address)
|
|
||||||
return
|
|
||||||
|
|
||||||
if isinstance(inp.type3, type3types.StructType3):
|
if isinstance(inp.type3, type3types.StructType3):
|
||||||
assert inp.variable.data_block is not None, 'Structs must be memory stored'
|
assert inp.variable.data_block is not None, 'Structs must be memory stored'
|
||||||
assert inp.variable.data_block.address is not None, 'Value not allocated'
|
assert inp.variable.data_block.address is not None, 'Value not allocated'
|
||||||
@ -599,7 +587,7 @@ def import_(inp: ourlang.Function) -> wasm.Import:
|
|||||||
assert inp.imported
|
assert inp.imported
|
||||||
|
|
||||||
return wasm.Import(
|
return wasm.Import(
|
||||||
inp.imported,
|
'imports',
|
||||||
inp.name,
|
inp.name,
|
||||||
inp.name,
|
inp.name,
|
||||||
[
|
[
|
||||||
@ -733,12 +721,6 @@ def module_data(inp: ourlang.ModuleData) -> bytes:
|
|||||||
data_list.append(module_data_f64(constant.value))
|
data_list.append(module_data_f64(constant.value))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if constant.type3 == type3types.bytes:
|
|
||||||
assert isinstance(constant.value, bytes)
|
|
||||||
data_list.append(module_data_u32(len(constant.value)))
|
|
||||||
data_list.append(constant.value)
|
|
||||||
continue
|
|
||||||
|
|
||||||
raise NotImplementedError(constant, constant.type3, constant.value)
|
raise NotImplementedError(constant, constant.type3, constant.value)
|
||||||
|
|
||||||
block_data = b''.join(data_list)
|
block_data = b''.join(data_list)
|
||||||
|
|||||||
@ -47,32 +47,15 @@ class ConstantPrimitive(Constant):
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'ConstantPrimitive({repr(self.value)})'
|
return f'ConstantPrimitive({repr(self.value)})'
|
||||||
|
|
||||||
class ConstantBytes(Constant):
|
|
||||||
"""
|
|
||||||
A bytes constant value expression within a statement
|
|
||||||
"""
|
|
||||||
__slots__ = ('value', 'data_block', )
|
|
||||||
|
|
||||||
value: bytes
|
|
||||||
data_block: 'ModuleDataBlock'
|
|
||||||
|
|
||||||
def __init__(self, value: bytes, data_block: 'ModuleDataBlock') -> None:
|
|
||||||
super().__init__()
|
|
||||||
self.value = value
|
|
||||||
self.data_block = data_block
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
return f'ConstantBytes({repr(self.value)}, @{repr(self.data_block.address)})'
|
|
||||||
|
|
||||||
class ConstantTuple(Constant):
|
class ConstantTuple(Constant):
|
||||||
"""
|
"""
|
||||||
A Tuple constant value expression within a statement
|
A Tuple constant value expression within a statement
|
||||||
"""
|
"""
|
||||||
__slots__ = ('value', )
|
__slots__ = ('value', )
|
||||||
|
|
||||||
value: List[Union[ConstantPrimitive, ConstantBytes]]
|
value: List[ConstantPrimitive]
|
||||||
|
|
||||||
def __init__(self, value: List[Union[ConstantPrimitive, ConstantBytes]]) -> None: # FIXME: Tuple of tuples?
|
def __init__(self, value: List[ConstantPrimitive]) -> None: # FIXME: Tuple of tuples?
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
@ -86,9 +69,9 @@ class ConstantStruct(Constant):
|
|||||||
__slots__ = ('struct_name', 'value', )
|
__slots__ = ('struct_name', 'value', )
|
||||||
|
|
||||||
struct_name: str
|
struct_name: str
|
||||||
value: List[Union[ConstantPrimitive, ConstantBytes]]
|
value: List[ConstantPrimitive]
|
||||||
|
|
||||||
def __init__(self, struct_name: str, value: List[Union[ConstantPrimitive, ConstantBytes]]) -> None: # FIXME: Struct of structs?
|
def __init__(self, struct_name: str, value: List[ConstantPrimitive]) -> None: # FIXME: Struct of structs?
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.struct_name = struct_name
|
self.struct_name = struct_name
|
||||||
self.value = value
|
self.value = value
|
||||||
@ -292,7 +275,7 @@ class Function:
|
|||||||
name: str
|
name: str
|
||||||
lineno: int
|
lineno: int
|
||||||
exported: bool
|
exported: bool
|
||||||
imported: Optional[str]
|
imported: bool
|
||||||
statements: List[Statement]
|
statements: List[Statement]
|
||||||
returns_type3: Type3
|
returns_type3: Type3
|
||||||
posonlyargs: List[FunctionParam]
|
posonlyargs: List[FunctionParam]
|
||||||
@ -301,7 +284,7 @@ class Function:
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.lineno = lineno
|
self.lineno = lineno
|
||||||
self.exported = False
|
self.exported = False
|
||||||
self.imported = None
|
self.imported = False
|
||||||
self.statements = []
|
self.statements = []
|
||||||
self.returns_type3 = type3types.none # FIXME: This could be a placeholder
|
self.returns_type3 = type3types.none # FIXME: This could be a placeholder
|
||||||
self.posonlyargs = []
|
self.posonlyargs = []
|
||||||
@ -365,10 +348,10 @@ class ModuleDataBlock:
|
|||||||
"""
|
"""
|
||||||
__slots__ = ('data', 'address', )
|
__slots__ = ('data', 'address', )
|
||||||
|
|
||||||
data: List[Union[ConstantPrimitive, ConstantBytes]]
|
data: List[ConstantPrimitive]
|
||||||
address: Optional[int]
|
address: Optional[int]
|
||||||
|
|
||||||
def __init__(self, data: List[Union[ConstantPrimitive, ConstantBytes]]) -> None:
|
def __init__(self, data: List[ConstantPrimitive]) -> None:
|
||||||
self.data = data
|
self.data = data
|
||||||
self.address = None
|
self.address = None
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@ from .ourlang import (
|
|||||||
|
|
||||||
Expression,
|
Expression,
|
||||||
BinaryOp,
|
BinaryOp,
|
||||||
ConstantPrimitive, ConstantBytes, ConstantTuple, ConstantStruct,
|
ConstantPrimitive, ConstantTuple, ConstantStruct,
|
||||||
TupleInstantiation,
|
TupleInstantiation,
|
||||||
|
|
||||||
FunctionCall, AccessStructMember, Subscript,
|
FunctionCall, AccessStructMember, Subscript,
|
||||||
@ -133,34 +133,16 @@ class OurVisitor:
|
|||||||
# Do stmts at the end so we have the return value
|
# Do stmts at the end so we have the return value
|
||||||
|
|
||||||
for decorator in node.decorator_list:
|
for decorator in node.decorator_list:
|
||||||
if isinstance(decorator, ast.Call):
|
if not isinstance(decorator, ast.Name):
|
||||||
if not isinstance(decorator.func, ast.Name):
|
_raise_static_error(decorator, 'Function decorators must be string')
|
||||||
_raise_static_error(decorator, 'Function decorators must be string')
|
if not isinstance(decorator.ctx, ast.Load):
|
||||||
if not isinstance(decorator.func.ctx, ast.Load):
|
_raise_static_error(decorator, 'Must be load context')
|
||||||
_raise_static_error(decorator, 'Must be load context')
|
_not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators')
|
||||||
_not_implemented(decorator.func.id == 'imported', 'Custom decorators')
|
|
||||||
|
|
||||||
if 1 != len(decorator.args):
|
if decorator.id == 'exported':
|
||||||
_raise_static_error(decorator, 'One argument expected')
|
function.exported = True
|
||||||
if not isinstance(decorator.args[0], ast.Constant):
|
|
||||||
_raise_static_error(decorator, 'Service name must be a constant')
|
|
||||||
if not isinstance(decorator.args[0].value, str):
|
|
||||||
_raise_static_error(decorator, 'Service name must be a stirng')
|
|
||||||
if 0 != len(decorator.keywords): # TODO: Allow for namespace keyword
|
|
||||||
_raise_static_error(decorator, 'No keyword arguments expected')
|
|
||||||
|
|
||||||
function.imported = decorator.args[0].value
|
|
||||||
else:
|
else:
|
||||||
if not isinstance(decorator, ast.Name):
|
function.imported = True
|
||||||
_raise_static_error(decorator, 'Function decorators must be string')
|
|
||||||
if not isinstance(decorator.ctx, ast.Load):
|
|
||||||
_raise_static_error(decorator, 'Must be load context')
|
|
||||||
_not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators')
|
|
||||||
|
|
||||||
if decorator.id == 'exported':
|
|
||||||
function.exported = True
|
|
||||||
else:
|
|
||||||
function.imported = 'imports'
|
|
||||||
|
|
||||||
if node.returns is not None: # Note: `-> None` would be a ast.Constant
|
if node.returns is not None: # Note: `-> None` would be a ast.Constant
|
||||||
function.returns_type3 = self.visit_type(module, node.returns)
|
function.returns_type3 = self.visit_type(module, node.returns)
|
||||||
@ -208,20 +190,12 @@ class OurVisitor:
|
|||||||
|
|
||||||
if isinstance(node.value, ast.Constant):
|
if isinstance(node.value, ast.Constant):
|
||||||
type3 = self.visit_type(module, node.annotation)
|
type3 = self.visit_type(module, node.annotation)
|
||||||
|
|
||||||
value_data = self.visit_Module_Constant(module, node.value)
|
|
||||||
|
|
||||||
if isinstance(value_data, ConstantBytes):
|
|
||||||
data_block = value_data.data_block
|
|
||||||
else:
|
|
||||||
data_block = None
|
|
||||||
|
|
||||||
return ModuleConstantDef(
|
return ModuleConstantDef(
|
||||||
node.target.id,
|
node.target.id,
|
||||||
node.lineno,
|
node.lineno,
|
||||||
type3,
|
type3,
|
||||||
value_data,
|
self.visit_Module_Constant(module, node.value),
|
||||||
data_block,
|
None,
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(node.value, ast.Tuple):
|
if isinstance(node.value, ast.Tuple):
|
||||||
@ -576,20 +550,14 @@ class OurVisitor:
|
|||||||
|
|
||||||
return Subscript(varref, slice_expr)
|
return Subscript(varref, slice_expr)
|
||||||
|
|
||||||
def visit_Module_Constant(self, module: Module, node: ast.Constant) -> Union[ConstantPrimitive, ConstantBytes]:
|
def visit_Module_Constant(self, module: Module, node: ast.Constant) -> ConstantPrimitive:
|
||||||
|
del module
|
||||||
|
|
||||||
_not_implemented(node.kind is None, 'Constant.kind')
|
_not_implemented(node.kind is None, 'Constant.kind')
|
||||||
|
|
||||||
if isinstance(node.value, (int, float, )):
|
if isinstance(node.value, (int, float, )):
|
||||||
return ConstantPrimitive(node.value)
|
return ConstantPrimitive(node.value)
|
||||||
|
|
||||||
if isinstance(node.value, bytes):
|
|
||||||
data_block = ModuleDataBlock([])
|
|
||||||
module.data.blocks.append(data_block)
|
|
||||||
|
|
||||||
result = ConstantBytes(node.value, data_block)
|
|
||||||
data_block.data.append(result)
|
|
||||||
return result
|
|
||||||
|
|
||||||
raise NotImplementedError(f'{node.value} as constant')
|
raise NotImplementedError(f'{node.value} as constant')
|
||||||
|
|
||||||
def visit_type(self, module: Module, node: ast.expr) -> type3types.Type3:
|
def visit_type(self, module: Module, node: ast.expr) -> type3types.Type3:
|
||||||
|
|||||||
@ -323,12 +323,12 @@ class LiteralFitsConstraint(ConstraintBase):
|
|||||||
__slots__ = ('type3', 'literal', )
|
__slots__ = ('type3', 'literal', )
|
||||||
|
|
||||||
type3: types.Type3OrPlaceholder
|
type3: types.Type3OrPlaceholder
|
||||||
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct]
|
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantTuple, ourlang.ConstantStruct]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
type3: types.Type3OrPlaceholder,
|
type3: types.Type3OrPlaceholder,
|
||||||
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct],
|
literal: Union[ourlang.ConstantPrimitive, ourlang.ConstantTuple, ourlang.ConstantStruct],
|
||||||
comment: Optional[str] = None,
|
comment: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(comment=comment)
|
super().__init__(comment=comment)
|
||||||
@ -380,12 +380,6 @@ class LiteralFitsConstraint(ConstraintBase):
|
|||||||
|
|
||||||
return Error('Must be real') # FIXME: Add line information
|
return Error('Must be real') # FIXME: Add line information
|
||||||
|
|
||||||
if self.type3 is types.bytes:
|
|
||||||
if isinstance(self.literal.value, bytes):
|
|
||||||
return None
|
|
||||||
|
|
||||||
return Error('Must be bytes') # FIXME: Add line information
|
|
||||||
|
|
||||||
res: NewConstraintList
|
res: NewConstraintList
|
||||||
|
|
||||||
if isinstance(self.type3, types.AppliedType3):
|
if isinstance(self.type3, types.AppliedType3):
|
||||||
|
|||||||
@ -25,7 +25,7 @@ def phasm_type3_generate_constraints(inp: ourlang.Module) -> List[ConstraintBase
|
|||||||
return [*module(ctx, inp)]
|
return [*module(ctx, inp)]
|
||||||
|
|
||||||
def constant(ctx: Context, inp: ourlang.Constant) -> ConstraintGenerator:
|
def constant(ctx: Context, inp: ourlang.Constant) -> ConstraintGenerator:
|
||||||
if isinstance(inp, (ourlang.ConstantPrimitive, ourlang.ConstantBytes, ourlang.ConstantTuple, ourlang.ConstantStruct)):
|
if isinstance(inp, (ourlang.ConstantPrimitive, ourlang.ConstantTuple, ourlang.ConstantStruct)):
|
||||||
yield LiteralFitsConstraint(inp.type3, inp)
|
yield LiteralFitsConstraint(inp.type3, inp)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user