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

Update "val foo_def = Define ..." to use Definition syntax #1057

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

dnezam
Copy link
Contributor

@dnezam dnezam commented Sep 22, 2024

Since there are so many changes, it is a bit hard to check everything manually, which is why I included the code I used in the respective commits.

@dnezam dnezam added the test failing regression test failed on the latest commit of this pull request label Sep 23, 2024
Assume that content after ` starts on a new line; thus we don't
remove whitespace on the left.

Python code used:
```py
import os
import re
import sys

def transform_definition(content):
    pattern = r"^val[ \t]+([a-zA-Z0-9][a-zA-Z0-9_']*)\s*=\s*Define\s*`\n([^`]+?)\s*`;?"

    replacement = r"Definition \1:\n\2\nEnd"

    transformed_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
    return transformed_content

def process_sml_file(filepath):
    with open(filepath, 'r') as file:
        content = file.read()

    transformed_content = transform_definition(content)

    # Overwriting the file with transformed content
    with open(filepath, 'w') as file:
        file.write(transformed_content)

def find_and_transform_sml_files(directory):
    for root, _, files in os.walk(directory):
        for file in files:
            if (file.endswith("Script.sml")):
                    filepath = os.path.join(root, file)
                    print(f"Processing file: {filepath}")
                    process_sml_file(filepath)

if __name__ == "__main__":
    # Check if the directory is passed as an argument
    if len(sys.argv) != 2:
        print("Usage: python script.py <directory>")
        sys.exit(1)

    directory_to_search = sys.argv[1]

    if not os.path.isdir(directory_to_search):
        print(f"Error: {directory_to_search} is not a valid directory.")
        sys.exit(1)

    # Start the search and transformation process
    find_and_transform_sml_files(directory_to_search)
```
Used Python code:
```py
import os
import re
import sys

def transform_definition(content):
    # Only Define with content surrounded by ` starting in the same line should
    # remain at this point; for those we set the initial indentation to 2
    pattern = r"^val[ \t]+([a-zA-Z0-9][a-zA-Z0-9_']*)\s*=\s*Define\s*`[ \t]*([^`]+?)\s*`;?"

    replacement = r"Definition \1:\n  \2\nEnd"

    transformed_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
    return transformed_content

def process_sml_file(filepath):
    with open(filepath, 'r') as file:
        content = file.read()

    transformed_content = transform_definition(content)

    # Overwriting the file with transformed content
    with open(filepath, 'w') as file:
        file.write(transformed_content)

def find_and_transform_sml_files(directory):
    for root, _, files in os.walk(directory):
        for file in files:
            if (file.endswith("Script.sml")):
                    filepath = os.path.join(root, file)
                    print(f"Processing file: {filepath}")
                    process_sml_file(filepath)

if __name__ == "__main__":
    # Check if the directory is passed as an argument
    if len(sys.argv) != 2:
        print("Usage: python script.py <directory>")
        sys.exit(1)

    directory_to_search = sys.argv[1]

    if not os.path.isdir(directory_to_search):
        print(f"Error: {directory_to_search} is not a valid directory.")
        sys.exit(1)

    # Start the search and transformation process
    find_and_transform_sml_files(directory_to_search)
```
@dnezam dnezam removed the test failing regression test failed on the latest commit of this pull request label Sep 23, 2024
Seems like it matched
(*
val app_opn_def = Define `
  app_opn opn i1 i2 H Q =
    if (opn = Divide \/ opn = Modulo) /\ i2 = 0 then
      H ==>> Q (Exn (prim_exn "Div"))
    else
      H ==>> Q (Val (Litv (IntLit (opn_lookup opn i1 i2))))
*)
which is missing the ending `.
@dnezam dnezam added the test failing regression test failed on the latest commit of this pull request label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test failing regression test failed on the latest commit of this pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant