Skip to content

Commit

Permalink
MATLAB switch statement support (#634)
Browse files Browse the repository at this point in the history
## MATLAB switch statement support
This PR adds Tree-sitter coverage of MATLAB switch statements. CI tests
are also included.

``` matlab
switch x
    case 1
        n = 1;
        j = 1;
    case {3, three, 'three'}
        n = 3;
    case {{1, 2, 3,}, {7, 8, 9}}
        n = 6;
    otherwise
        n = 0;
end
```

## Relevant features
- Switch statements are translated into CAST conditional logic
- Multiple argument cases are reduced to single list inclusion test
- Case arguments may be of differing datatypes (string, number,
identifier)

## Related issues

- Resolves issue #561

---------

Co-authored-by: Joseph Astier <jastier@arizona.edu>
  • Loading branch information
jastier and Joseph Astier authored Nov 13, 2023
1 parent dfe1512 commit 391786e
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 180 deletions.
60 changes: 45 additions & 15 deletions skema/program_analysis/CAST/matlab/cast_out
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,55 @@
import json
import sys
from skema.program_analysis.CAST.matlab.matlab_to_cast import MatlabToCast
from typing import List

# Show a CAST object as pretty printed JSON with keys filtered for clarity.

# Keys to remove from the output
#KEY_FILTER = ["source_refs", "default_value", "interpreter"]
KEY_FILTER = ["source_refs"]

def remove_keys(json_obj, target_keys: List):
""" remove all instances of the target keys from the json object"""

def remove_key(json_obj, target_key):
""" remove all instances of the target key from the json object"""
if isinstance(json_obj, dict):
for target_key in target_keys:
if target_key in json_obj.keys():
json_obj.pop(target_key)
for key in json_obj.keys():
remove_key(json_obj[key], target_keys)
elif isinstance(json_obj, list):
for item in json_obj:
remove_key(item, target_keys)
return(json_obj)

print(f"Removed keys: {target_keys}")
return remove_key(json_obj, target_keys)


def show_cast(filename):
""" Run a file of any type through the Tree-sitter MATLAB parser"""
parser = MatlabToCast(filename)
print("\nINPUT FILE:")
print(parser.filename)
print("\nSOURCE:")
print(parser.source)
print('\nCAST:')
cast_list = parser.out_cast
for cast in cast_list:
json_obj = cast.to_json_object()
# declutter JSON by filtering keys
json_obj = remove_keys(json_obj, KEY_FILTER)
# pretty print JSON to string
output = json.dumps(json_obj, sort_keys=True, indent=2)
print(output)

""" Run a file of any type through the Tree-sitter MATLAB parser"""

if __name__ == "__main__":
if len(sys.argv) > 1:
for i in range(1, len(sys.argv)):
parser = MatlabToCast(sys.argv[i])
print("\n\nINPUT:")
print(parser.filename)
print("\nSOURCE:")
print(parser.source)
print('\nCAST:')
cast_list = parser.out_cast
for cast_index in range(0, len(cast_list)):
jd = json.dumps(
cast_list[cast_index].to_json_object(),
sort_keys=True,
indent=2,
)
print(jd)
show_cast(sys.argv[i])
else:
print("Please enter one filename to parse")
Loading

0 comments on commit 391786e

Please sign in to comment.