Skip to content

Commit

Permalink
Update aas-core-codegen to 6df5c9e8 (#345)
Browse files Browse the repository at this point in the history
We update to [aas-core-codegen 6df5c9e8] for better type inference
related to optional value types and nullness checks.

[aas-core-codegen 6df5c9e8]: aas-core-works/aas-core-codegen@6df5c9e8
  • Loading branch information
mristin authored Aug 31, 2024
1 parent 35aa2cc commit f9cbdb3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 50 deletions.
2 changes: 1 addition & 1 deletion htmlgen/for_metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ def _generate_page_for_verification_function(
code_div, error = htmlgen.transpilation.transpile_body_of_verification(
verification=verification,
symbol_table=symbol_table,
environment=environment,
base_environment=environment,
)
if error is not None:
return None, error
Expand Down
102 changes: 54 additions & 48 deletions htmlgen/transpilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Union,
Set,
Sequence,
cast,
)

from aas_core_codegen import intermediate
Expand Down Expand Up @@ -1074,10 +1075,7 @@ def __init__(
],
environment: intermediate_type_inference.Environment,
symbol_table: intermediate.SymbolTable,
verification: Union[
intermediate.TranspilableVerification,
intermediate.PatternVerification,
],
verification: intermediate.TranspilableVerification,
) -> None:
"""Initialize with the given values."""
htmlgen.transpilation._Transpiler.__init__(
Expand Down Expand Up @@ -1132,7 +1130,7 @@ def transpile_body_of_verification(
intermediate.ImplementationSpecificVerification,
],
symbol_table: intermediate.SymbolTable,
environment: intermediate_type_inference.Environment,
base_environment: intermediate_type_inference.Environment,
) -> Tuple[Optional[Stripped], Optional[Error]]:
"""Transpile a verification function to HTML."""
parsed_body = None # type: Optional[Sequence[parse_tree.Node]]
Expand All @@ -1152,50 +1150,62 @@ def transpile_body_of_verification(
),
None,
)

else:
assert_never(verification)

assert parsed_body is not None
assert not isinstance(verification, intermediate.ImplementationSpecificVerification)

canonicalizer = intermediate_type_inference.Canonicalizer()
for node in parsed_body:
_ = canonicalizer.transform(node)

environment_with_args = intermediate_type_inference.MutableEnvironment(
parent=environment
environment = intermediate_type_inference.MutableEnvironment(
parent=base_environment
)
for arg in verification.arguments:
environment_with_args.set(
identifier=arg.name,
type_annotation=intermediate_type_inference.convert_type_annotation(
arg.type_annotation
if isinstance(verification, intermediate.PatternVerification):
# NOTE (mristin):
# This type is wrong for the ``re.match`` function. However, we just populate
# it here so that we do not have to re-implement the code highlighting for
# pattern verification.
environment.set(
Identifier("match"),
intermediate_type_inference.BuiltinFunctionTypeAnnotation(
intermediate_type_inference.BuiltinFunction(
name=Identifier("match"),
returns=intermediate_type_inference.OptionalTypeAnnotation(
value=intermediate_type_inference.PrimitiveTypeAnnotation(
a_type=intermediate_type_inference.PrimitiveType.BOOL
)
),
)
),
)

type_inferrer = intermediate_type_inference.Inferrer(
symbol_table=symbol_table,
environment=environment_with_args,
representation_map=canonicalizer.representation_map,
transpilable_verification = cast(
intermediate.TranspilableVerification, verification
)
elif isinstance(verification, intermediate.TranspilableVerification):
transpilable_verification = verification
else:
assert_never(verification)
raise AssertionError("Unexpected execution path")

# fmt: off
type_inference, error = (
intermediate_type_inference.infer_for_verification(
verification=transpilable_verification,
base_environment=environment
)
)
# fmt: on

for node in verification.parsed.body:
_ = type_inferrer.transform(node)
if error is not None:
return None, error

if len(type_inferrer.errors):
return None, Error(
verification.parsed.node,
f"Failed to infer the types "
f"in the verification function {verification.name!r}",
type_inferrer.errors,
)
assert type_inference is not None

transpiler = _TranspilableVerificationTranspiler(
type_map=type_inferrer.type_map,
environment=environment_with_args,
type_map=type_inference.type_map,
environment=type_inference.environment_with_args,
symbol_table=symbol_table,
verification=verification,
verification=transpilable_verification,
)

body = [] # type: List[Stripped]
Expand Down Expand Up @@ -1283,26 +1293,22 @@ def transpile_invariant(
environment: intermediate_type_inference.Environment,
) -> Tuple[Optional[Stripped], Optional[Error]]:
"""Translate the invariant from the meta-model into HTML."""
canonicalizer = intermediate_type_inference.Canonicalizer()
_ = canonicalizer.transform(invariant.body)

type_inferrer = intermediate_type_inference.Inferrer(
symbol_table=symbol_table,
environment=environment,
representation_map=canonicalizer.representation_map,
# fmt: off
type_map, inference_error = (
intermediate_type_inference.infer_for_invariant(
invariant=invariant,
environment=environment
)
)
# fmt: on

_ = type_inferrer.transform(invariant.body)
if inference_error is not None:
return None, inference_error

if len(type_inferrer.errors):
return None, Error(
invariant.parsed.node,
"Failed to infer the types in the invariant",
type_inferrer.errors,
)
assert type_map is not None

transpiler = _InvariantTranspiler(
type_map=type_inferrer.type_map,
type_map=type_map,
environment=environment,
symbol_table=symbol_table,
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"mypy==0.910",
"pylint==2.17.0",
"asttokens>=2.0.8,<3",
"aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@bd310fc2#egg=aas-core-codegen",
"aas-core-codegen@git+https://github.com/aas-core-works/aas-core-codegen@6df5c9e8#egg=aas-core-codegen",
"astpretty==3.0.0",
"pygments>=2,<3"
],
Expand Down

0 comments on commit f9cbdb3

Please sign in to comment.