Skip to content

Add visitors to TESTed-AST to write better code #564

Open
@BrentBlanckaert

Description

@BrentBlanckaert

Each language implements their own generator.py. Almost all of them have functions that look like the following:

def convert_statement(statement: Statement, internal=False, full=False) -> str:
    if isinstance(statement, Identifier):
        return statement
    elif isinstance(statement, FunctionCall):
        return convert_function_call(statement, internal)
    elif isinstance(statement, Value):
        return convert_value(statement)
    elif isinstance(statement, PropertyAssignment):
        return (
            f"{convert_statement(statement.property, True)} = "
            f"{convert_statement(statement.expression, True)}"
        )
    elif isinstance(statement, VariableAssignment):
        if full:
            prefix = "let "
        else:
            prefix = ""
        return (
            f"{prefix}{statement.variable} = "
            f"{convert_statement(statement.expression, True)}"
        )
    raise AssertionError(f"Unknown statement: {statement!r}")

To write this better a visitor could be used. This way all the if-statements will be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions