Skip to content

Commit d8db5ff

Browse files
committed
refact: convert backent into a class
This allows better integration, and to define a compliance interface via an ABC which, fortunately, will help others to create other backends.
1 parent 91fea11 commit d8db5ff

28 files changed

Lines changed: 1800 additions & 1343 deletions

src/arch/interface/__init__.py

Whitespace-only changes.

src/arch/interface/backend.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from abc import ABC, abstractmethod
2+
3+
__all__ = ("BackendInterface",)
4+
5+
6+
class BackendInterface(ABC):
7+
"""Generic Backend interface"""
8+
9+
def __init__(self):
10+
self.init()
11+
12+
@abstractmethod
13+
def init(self) -> None:
14+
"""Initializes this module"""
15+
16+
@staticmethod
17+
@abstractmethod
18+
def emit_prologue() -> list[str]:
19+
"""Emits Program Start routine"""
20+
21+
@staticmethod
22+
@abstractmethod
23+
def emit_epilogue() -> list[str]:
24+
"""Emits Program End routine"""
25+
26+
@abstractmethod
27+
def emit(self, *, optimize: bool = True) -> list[str]:
28+
"""Begin converting each quad instruction to asm
29+
by iterating over the "mem" array, and called its
30+
associated function. Each function returns an array of
31+
ASM instructions
32+
"""

src/arch/interface/quad.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from dataclasses import dataclass
2+
3+
from src.symbols.symbol_ import Symbol
4+
5+
__all__ = ("Quad",)
6+
7+
8+
@dataclass(init=False, frozen=True)
9+
class Quad:
10+
"""Implements a Quad code instruction."""
11+
12+
instr: str
13+
args: tuple[str, ...]
14+
15+
def __init__(self, instr: str, *args) -> None:
16+
"""Creates a quad-uple checking it has the current params.
17+
Operators should be passed as Quad(ICInstruction, tSymbol, val1, val2)
18+
"""
19+
args = tuple(str(x.t) if isinstance(x, Symbol) else str(x) for x in args)
20+
object.__setattr__(self, "instr", instr)
21+
object.__setattr__(self, "args", args)
22+
23+
def __str__(self) -> str:
24+
"""String representation"""
25+
return f"({self.instr} {', '.join(x for x in self.args)})"
26+
27+
def __len__(self) -> int:
28+
"""Returns the number of arguments + 1 (the instruction)"""
29+
return len(self.args) + 1
30+
31+
def __iter__(self):
32+
return iter((self.instr, *self.args))
33+
34+
def __getitem__(self, item):
35+
return (self.instr, *self.args)[item]

0 commit comments

Comments
 (0)