Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect input / output ports of components via getters, instead of overwriting object references #329

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions xircuits/compiler/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def execute(self, ctx):
}


connect_args = lambda target, source: ast.parse("setattr(%s, '_getter', lambda x: %s.value)" % (target, source))
set_value = lambda target, v: ast.parse("%s.value = %s" % (target, v))

# Set up component argument links
for node in component_nodes:
# Handle argument connections
Expand All @@ -141,7 +144,7 @@ def execute(self, ctx):
port.targetLabel
)
assignment_source = "self.%s" % arg_name
tpl = ast.parse("%s = %s" % (assignment_target, assignment_source))
tpl = connect_args(assignment_target, assignment_source)
init_code.append(tpl)
if arg_name not in existing_args:
in_arg_def = ast.parse("%s: InArg[%s]" % (arg_name, arg_type)).body[0]
Expand All @@ -157,10 +160,8 @@ def execute(self, ctx):
)

if port.source.id not in named_nodes:

# Literal
assignment_target += ".value"
tpl = ast.parse("%s = 1" % (assignment_target))
tpl = set_value(assignment_target, '1')
if port.source.type == "string":
value = port.sourceLabel
elif port.source.type == "list":
Expand All @@ -187,7 +188,7 @@ def execute(self, ctx):
named_nodes[port.source.id],
port.sourceLabel
)
tpl = ast.parse("%s = %s" % (assignment_target, assignment_source))
tpl = connect_args(assignment_target, assignment_source)
init_code.append(tpl)

# Handle dynamic connections
Expand Down Expand Up @@ -231,8 +232,6 @@ def execute(self, ctx):
value = "%s.%s" % (named_nodes[port.source.id], port.sourceLabel) # Variable reference
dynaport_values.append(RefOrValue(value, True))

assignment_target = "%s.%s.value" % (named_nodes[ports[0].target.id], ports[0].varName)

if ports[0].dataType == 'dynatuple':
tuple_elements = [item.value if item.is_ref else repr(item.value) for item in dynaport_values]
if len(tuple_elements) == 1:
Expand All @@ -243,7 +242,8 @@ def execute(self, ctx):
list_elements = [item.value if item.is_ref else repr(item.value) for item in dynaport_values]
assignment_value = '[' + ', '.join(list_elements) + ']'

tpl = ast.parse("%s = %s" % (assignment_target, assignment_value))
assignment_target = "%s.%s" % (named_nodes[ports[0].target.id], ports[0].varName)
tpl = set_value(assignment_target, assignment_value)
init_code.append(tpl)

# Handle output connections
Expand All @@ -255,8 +255,7 @@ def execute(self, ctx):
assignment_target = "self.%s" % port_name
if port.source.id not in named_nodes:
# Literal
assignment_target += ".value"
tpl = ast.parse("%s = 1" % (assignment_target))
tpl = set_value(assignment_target, '1')
if port.source.type == "string":
value = port.sourceLabel
elif port.source.type == "list":
Expand All @@ -282,12 +281,10 @@ def execute(self, ctx):
named_nodes[port.source.id],
port.sourceLabel
)
tpl = ast.parse("%s = %s" % (assignment_target, assignment_source))
tpl = connect_args(assignment_target, assignment_source)
args_code.append(ast.parse("%s: OutArg[%s]" % (port_name, port_type)).body[0])
init_code.append(tpl)



# Set up control flow
for node in component_nodes:
has_next = False
Expand Down
Loading