Skip to content
Snippets Groups Projects
Select Git revision
  • de60e366c687c8cf014470132d3150d47716aae8
  • main default protected
2 results

CalibrationQuick_07_02_24.py

Blame
  • PrintInst.py 2.08 KiB
    #! /usr/bin/python3
    from __future__ import annotations
    
    from typing_extensions import override
    import re
    
    from Infrastructure.Instruction import Instruction
    from Infrastructure.Variables import ERROR_MARKER_COMMENT_BEGIN, ERROR_MARKER_COMMENT_END, ERROR_MARKER_COMMENT_BEGIN_FORT, ERROR_MARKER_COMMENT_END_FORT, adjust_var_language
    import Infrastructure.Variables as infvars
    
    print_template_c = "printf(\"@STRING@\"@ARGS@);"
    print_template_fort = "print *, \"@STRING@\"@ARGS@"
    
    """
     Class Overview:
                The `PrintInst` class is a helper for creating print instructions
    
            Methods:
                - `__init__(self)`: Initializes a new print instruction
                - `__str__(self)`: Converts the print instance to a string, replacing placeholders.
    """
    
    class PrintInst(Instruction):
        @override
        def __init__(self, string: str, args: List[str] = [], rank: str | int = 'all', identifier: str = None):
            """
            Creates a new print instruction
    
            Args:
                string: String to print
                args: List of variables to print (postfix)
            """
            super().__init__("", rank, identifier)
            self._string = string
            self._args = args
    
        @override
        def __str__(self):
            actual_template = print_template_c if infvars.generator_language == "c" else print_template_fort
            actual_string = self._string
            
            arg_str = ""
            for arg in self._args:
                arg_str = ", " + adjust_var_language(arg)
                if infvars.generator_language == "c":
                    actual_string += " %d"
            if infvars.generator_language == "c":
                actual_string += "\\n"
            result = actual_template.replace("@STRING@", actual_string).replace("@ARGS@", arg_str)
            if infvars.generator_language == "c":
                error_begin = ERROR_MARKER_COMMENT_BEGIN
                error_end = ERROR_MARKER_COMMENT_END
            else:
                error_begin = ERROR_MARKER_COMMENT_BEGIN_FORT
                error_end = ERROR_MARKER_COMMENT_END_FORT
            if self.has_error():
                result = error_begin + result + error_end
            return result