Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#673 from boriel/refact/backend_interface
Browse files Browse the repository at this point in the history
Refact/backend interface
  • Loading branch information
boriel committed Sep 8, 2023
2 parents 22a2741 + d8db5ff commit 0e8017c
Show file tree
Hide file tree
Showing 29 changed files with 1,802 additions and 1,348 deletions.
Empty file added src/arch/interface/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions src/arch/interface/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from abc import ABC, abstractmethod

__all__ = ("BackendInterface",)


class BackendInterface(ABC):
"""Generic Backend interface"""

def __init__(self):
self.init()

@abstractmethod
def init(self) -> None:
"""Initializes this module"""

@staticmethod
@abstractmethod
def emit_prologue() -> list[str]:
"""Emits Program Start routine"""

@staticmethod
@abstractmethod
def emit_epilogue() -> list[str]:
"""Emits Program End routine"""

@abstractmethod
def emit(self, *, optimize: bool = True) -> list[str]:
"""Begin converting each quad instruction to asm
by iterating over the "mem" array, and called its
associated function. Each function returns an array of
ASM instructions
"""
35 changes: 35 additions & 0 deletions src/arch/interface/quad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass

from src.symbols.symbol_ import Symbol

__all__ = ("Quad",)


@dataclass(init=False, frozen=True)
class Quad:
"""Implements a Quad code instruction."""

instr: str
args: tuple[str, ...]

def __init__(self, instr: str, *args) -> None:
"""Creates a quad-uple checking it has the current params.
Operators should be passed as Quad(ICInstruction, tSymbol, val1, val2)
"""
args = tuple(str(x.t) if isinstance(x, Symbol) else str(x) for x in args)
object.__setattr__(self, "instr", instr)
object.__setattr__(self, "args", args)

def __str__(self) -> str:
"""String representation"""
return f"({self.instr} {', '.join(x for x in self.args)})"

def __len__(self) -> int:
"""Returns the number of arguments + 1 (the instruction)"""
return len(self.args) + 1

def __iter__(self):
return iter((self.instr, *self.args))

def __getitem__(self, item):
return (self.instr, *self.args)[item]

0 comments on commit 0e8017c

Please sign in to comment.