Commit 905e015a authored by Sean Leavey's avatar Sean Leavey

WIP

parent 4168e38e
......@@ -22,7 +22,7 @@ import logging
LOGGER = logging.getLogger(__name__)
def _create_model_parameter(cls, name, value):
def create_model_parameter(cls, name, value):
"""Instantiate a model parameter given its name and value.
This function should not be used by user code.
......@@ -116,7 +116,7 @@ class ModelElement:
f"Creating parameter {name}.{param_name} with initial value {value} "
f"({type(value).__name__})."
)
parameter = _create_model_parameter(self.__class__, param_name, value)
parameter = create_model_parameter(self.__class__, param_name, value)
# Set the parameter's owner to this instance.
parameter._set_component(self)
......
......@@ -10,8 +10,9 @@ from networkx.exception import NetworkXUnfeasible
from .base import BaseKatParser
from .spec import ParsedStatement, KatSpec
from .lexer import KatLexer
from .parser import KatYACC, BaseClosure
from .parser import KatYACC
from .exceptions import KatParserError, KatReferenceError, DirectiveNotFoundError
from ..element import create_model_parameter
from ..utilities import ngettext, option_list
LOGGER = logging.getLogger(__name__)
......@@ -172,7 +173,12 @@ class KatParser(BaseKatParser):
# built due to the topological ordering.
item = item.resolve(memo)
except AttributeError:
pass
# Create the parameter.
name = item
value = parser.ast.nodes[node_name]["value"]
item = create_model_parameter(
item_statement.adapter.ptype, name, value
)
# Update the corresponding statement value and memo.
item_statement.setarg(node_name, item)
......@@ -250,7 +256,7 @@ class ModelMemo(UserDict):
def __getitem__(self, item):
try:
return self[item]
return self.data[item]
except KeyError:
return self.model.reduce_get_attr(item)
......
......@@ -86,7 +86,7 @@ class ModelParameterClosure(StringClosure):
"""Closure for strings that should resolve to Finesse model parameters."""
def resolve(self, memo):
return create_model_parameter()
return memo[self.string]
class ModelParameterReferenceClosure(StringClosure):
......@@ -96,7 +96,7 @@ class ModelParameterReferenceClosure(StringClosure):
super().__init__(string.lstrip("&"))
def resolve(self, memo):
raise NotImplementedError
return super().resolve(memo).ref
def __repr__(self):
# Used for tests, such as `tests/unit/script/parser/syntax/conftest.py`.
......@@ -257,7 +257,9 @@ class KatYACC(Parser):
# Add the parameter as an AST node, and draw an edge to the owning statement so it gets
# built after.
self.ast.add_node(node_name, item=parameter, statement=statement)
self.ast.add_node(
node_name, item=name, value=parameter, statement=statement
)
self.ast.add_edge(node_name, statement.node_name)
# Add edges to the parameter's dependencies, to ensure they get built first.
......
......@@ -2,13 +2,13 @@
import pytest
from finesse.element import _create_model_parameter
from finesse.element import create_model_parameter
from finesse.components import Mirror, Beamsplitter
def test_model_parameter_as_constructor_arg():
"""Test that passing model parameter as constructor argument is allowed."""
R = _create_model_parameter(Mirror, "R", 0.5)
R = create_model_parameter(Mirror, "R", 0.5)
m1 = Mirror("m1", R=R, T=0.5)
# Should be the same object.
......@@ -17,8 +17,8 @@ def test_model_parameter_as_constructor_arg():
def test_multiple_model_parameters_as_constructor_args():
"""Test that passing model parameters as constructor arguments is allowed."""
R = _create_model_parameter(Mirror, "R", 0.5)
T = _create_model_parameter(Mirror, "T", 0.5)
R = create_model_parameter(Mirror, "R", 0.5)
T = create_model_parameter(Mirror, "T", 0.5)
m1 = Mirror("m1", R=R, T=T)
# Should be the same objects.
......@@ -29,12 +29,12 @@ def test_multiple_model_parameters_as_constructor_args():
def test_unregistered_model_parameter_invalid():
"""Test that creating a model parameter that isn't registered on the element is invalid."""
with pytest.raises(KeyError):
_create_model_parameter(Mirror, "fake_param", 0.5)
create_model_parameter(Mirror, "fake_param", 0.5)
def test_unregistered_model_parameter_as_constructor_arg_invalid():
"""Test that passing a model parameter that is registered on a different element is invalid."""
param = _create_model_parameter(Beamsplitter, "R", 0.5)
param = create_model_parameter(Beamsplitter, "R", 0.5)
with pytest.raises(ValueError):
Mirror("m1", R=param, T=0.5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment