Open
Description
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.