-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move preprocessor logic to its own file (#17)
* refactor: Move preprocessor logic to its own file * chore: Minor change
- Loading branch information
1 parent
e89f418
commit 4908d52
Showing
2 changed files
with
28 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import re | ||
from abc import ABC, abstractmethod | ||
from typing import override, List | ||
|
||
|
||
class Preprocessor(ABC): | ||
@abstractmethod | ||
def process(self, program: str) -> str: | ||
pass | ||
|
||
|
||
class CommentsPreprocessor(Preprocessor): | ||
@override | ||
def process(self, program: str): | ||
cleaned_program = map(lambda line: re.sub(r"#.*$", "", line), program.splitlines()) | ||
return "\n".join(cleaned_program) | ||
|
||
|
||
class PipelinePreprocessor(Preprocessor): | ||
def __init__(self, preprocessor_actions: List[Preprocessor]): | ||
self.preprocessor_actions = preprocessor_actions | ||
|
||
@override | ||
def process(self, program: str): | ||
for action in self.preprocessor_actions: | ||
program = action.process(program) | ||
return program |