This repository has been archived by the owner on May 21, 2024. It is now read-only.
forked from boriel-basic/zxbasic
-
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.
Merge pull request boriel-basic#673 from boriel/refact/backend_interface
Refact/backend interface
- Loading branch information
Showing
29 changed files
with
1,802 additions
and
1,348 deletions.
There are no files selected for viewing
Empty file.
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,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 | ||
""" |
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,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] |
Oops, something went wrong.