Skip to content
Snippets Groups Projects

Draft: Fortran Support

4 files
+ 102
25
Compare changes
  • Side-by-side
  • Inline

Files

#! /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
for_template_c = "for (int i = @START@; i < @END@; ++i)"
for_template_fort = "do i=@START@, @END@"
if_template_c = "if (@COND@) {"
if_template_fort = "if (@COND@) then"
"""
Class Overview:
The `Branch` class is a prototype for the specific IfBranch/ForBranch helpers for creating if/for branches/loops.
It should not be used directly
Methods:
- `__init__(self)`: Initializes a new branch
- `header(self)`: Returns a string representing the branch header
- `trailer(self)`: Returns a string representing the branch trailer
"""
class Branch:
@override
def __init__(self):
pass
def header(self):
return ""
@staticmethod
def trailer():
return ""
class IfBranch(Branch):
@override
def __init__(self, cond: str):
self._cond = cond
@override
def header(self):
if infvars.generator_language == "c":
return if_template_c.replace("@COND@", self._cond)
else:
actual_cond = self._cond
# Replace array access [*] with (*)
actual_cond = re.sub(r"([A-z0-9]+)\s*\[(.+)\]", r"\1(\2)", actual_cond)
# Replace not-equal comparator
actual_cond = actual_cond.replace("!=", "/=")
return if_template_fort.replace("@COND@", actual_cond)
@override
@staticmethod
def trailer():
if infvars.generator_language == "c":
return "}"
else:
return "end if"
class ForLoop(Branch):
@override
def __init__(self, start: str, end: str):
self._start = start
self._end = end
@override
def header(self):
if infvars.generator_language == "c":
return for_template_c.replace("@START@", str(self._start)).replace("@END@", str(self._end))
else:
return for_template_fort.replace("@START@", str(self._end)).replace("@END@", str(self._end))
@override
@staticmethod
def trailer():
if infvars.generator_language == "c":
return "}"
else:
return "end do"
Loading