Skip to content

Commit

Permalink
Minimize code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Jul 18, 2024
1 parent 5cec4da commit 201d060
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 459 deletions.
22 changes: 9 additions & 13 deletions tested/languages/c/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tested.datatypes import AllTypes
from tested.dodona import AnnotateCode, Message
from tested.features import Construct, TypeSupport
from tested.languages.c.generators import CGenerator
from tested.languages.conventionalize import (
EXECUTION_PREFIX,
Conventionable,
Expand Down Expand Up @@ -103,7 +104,7 @@ def modify_solution(self, solution: Path):
# First, check if we have a no-arg main function.
# If so, replace it with a renamed main function that does have args.
no_args = re.compile(r"(int|void)(\s+)main(\s*)\((\s*)\)(\s*{)")
replacement = r"int\2solution_main\3(\4int argc, char** argv)\5"
replacement = r"int\2solution_main\3(\4int argc, char* argv[])\5"
contents, nr = re.subn(no_args, replacement, contents, count=1)
if nr == 0:
# There was no main function without arguments. Now we try a main
Expand Down Expand Up @@ -139,26 +140,21 @@ def cleanup_stacktrace(self, stacktrace: str) -> str:

def is_source_file(self, file: Path) -> bool:
return file.suffix in (".c", ".h")

def generator(self) -> CGenerator:
return CGenerator(self.file_extension())

def generate_statement(self, statement: Statement) -> str:
from tested.languages.c import generators

return generators.convert_statement(statement, full=True)
return self.generator().convert_statement(statement, full=True)

def generate_execution_unit(self, execution_unit: "PreparedExecutionUnit") -> str:
from tested.languages.c import generators

return generators.convert_execution_unit(execution_unit)
return self.generator().convert_execution_unit(execution_unit)

def generate_selector(self, contexts: list[str]) -> str:
from tested.languages.c import generators

return generators.convert_selector(contexts)
return self.generator().convert_selector(contexts)

def generate_encoder(self, values: list[Value]) -> str:
from tested.languages.c import generators

return generators.convert_encoder(values)
return self.generator().convert_encoder(values)

def get_declaration_metadata(self) -> TypeDeclarationMetadata:
return {
Expand Down
Loading

0 comments on commit 201d060

Please sign in to comment.