Skip to content

Commit

Permalink
added new transformer tests, updated language to handle dereferenced …
Browse files Browse the repository at this point in the history
…function-call returns
  • Loading branch information
engineerjoe440 committed Apr 12, 2023
1 parent 02a4c66 commit f214d07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions blark/iec.lark
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ UNARY_OPERATOR: LOGICAL_NOT
| MINUS
| PLUS

function_call: symbolic_variable "(" [ param_assignment ( "," param_assignment )* ","? ] ")"
function_call: symbolic_variable "(" [ param_assignment ( "," param_assignment )* ","? ] ")" DEREFERENCED?

?primary_expression: "(" expression ")" -> parenthesized_expression
| function_call
Expand Down Expand Up @@ -749,7 +749,7 @@ reset_statement: _variable "R="i expression ";"+
reference_assignment_statement: _variable "REF="i expression ";"+

// method ::= expression [DEREFERENCED] '.' _identifier '(' ')';
method_statement: symbolic_variable "(" ")" ";"+
method_statement: symbolic_variable "(" ")" DEREFERENCED? ";"+

// B.3.2.2
return_statement.1: "RETURN"i ";"*
Expand Down
12 changes: 11 additions & 1 deletion blark/tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,17 @@ def test_action_roundtrip(rule_name, value):
)),
param("chained_function_call_statement", tf.multiline_code_block(
"""
uut.call1().call2().call3.call4().done();
uut.call1().call2().call3().call4().done();
"""
)),
param("chained_function_call_statement", tf.multiline_code_block(
"""
uut.call1()^.call2().call3()^.call4().done();
"""
)),
param("chained_function_call_statement", tf.multiline_code_block(
"""
uut.call1()^.call2(A := 1).call3(B := 2)^.call4().done();
"""
)),
],
Expand Down
14 changes: 13 additions & 1 deletion blark/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@ def __str__(self) -> str:
class FunctionCall(Expression):
name: SymbolicVariable
parameters: List[ParameterAssignment]
dereferenced: bool
meta: Optional[Meta] = meta_field()

@property
Expand Down Expand Up @@ -1621,6 +1622,12 @@ def from_lark(
first_parameter: Optional[ParameterAssignment] = None,
*remaining_parameters: ParameterAssignment,
) -> FunctionCall:
# Remove the Dereference Token if Present in the Remaining Parameters
dereferenced = False
if remaining_parameters:
if str(remaining_parameters[-1]) == "^":
dereferenced = True
remaining_parameters = remaining_parameters[:-1]
# Condition parameters (which may be `None`) to represent empty tuple
if first_parameter is None:
parameters = []
Expand All @@ -1630,11 +1637,15 @@ def from_lark(
return FunctionCall(
name=name,
parameters=parameters,
dereferenced=dereferenced
)

def __str__(self) -> str:
dereference = ""
parameters = ", ".join(str(param) for param in self.parameters)
return f"{self.name}({parameters})"
if self.dereferenced:
dereference = "^"
return f"{self.name}({parameters}){dereference}"


@dataclass
Expand Down Expand Up @@ -2620,6 +2631,7 @@ def from_lark(
return FunctionCallStatement(
name=invocation.name,
parameters=invocation.parameters,
dereferenced=invocation.dereferenced,
meta=invocation.meta,
)

Expand Down

0 comments on commit f214d07

Please sign in to comment.