Skip to content

Commit

Permalink
mypyc support #2
Browse files Browse the repository at this point in the history
  • Loading branch information
x0r committed Mar 9, 2024
1 parent 96d616c commit 6c8c94c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions masm2c/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def getsize(self) -> int:
def __str__(self) -> str:
return str(self.__class__)

#@property
#def data(self):
def accept(self, visitor: "Cpp") -> str:
raise NotImplementedError()

class var:

Expand Down Expand Up @@ -1044,6 +1044,7 @@ def __init__(self, name: str) -> None:
self.original_type = ""
self.implemented = False
self.size = 0
self.value: Expression

def gettype(self):
return self.original_type
Expand All @@ -1065,6 +1066,7 @@ def __init__(self, args: list[Union[str, Expression]]) -> None:
self.original_type = ""
self.implemented = False
self.size = 0
self.value: Expression

def gettype(self):
return self.original_type
Expand All @@ -1087,6 +1089,7 @@ class _mov(baseop):
def __init__(self, args: list[Expression]) -> None:
super().__init__()
self.children = args
self.syntetic = False

def accept(self, visitor: "Cpp") -> str:
return visitor._mov(*self.children)
20 changes: 12 additions & 8 deletions masm2c/proc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import logging
import re
from typing import Any
from typing import Any, TYPE_CHECKING, ClassVar

from lark import lark

Expand All @@ -32,11 +32,14 @@
from . import op
from .Token import Token

if TYPE_CHECKING:
from .cpp import Cpp

PTRDIR = "ptrdir"


class Proc:
last_addr = 0xc000
last_addr: ClassVar = 0xc000

def __init__(self, name: str, far: bool = False, line_number: int = 0, extern: bool = False, offset: int | None=0,
real_offset: int | None=0,
Expand All @@ -51,21 +54,21 @@ def __init__(self, name: str, far: bool = False, line_number: int = 0, extern: b
self.name = name
self.original_name = name

self.stmts = []
self.stmts: list[baseop] = []
self.provided_labels = {name}
self.line_number = line_number
self.far = far
self.used = False
self.extern = extern
self.used_labels = set()
self.used_labels: set[str] = set()
self.group = None
self.segment = segment

self.offset = offset or Proc.last_addr
Proc.last_addr += 4
self.real_offset, self.real_seg = real_offset, real_seg

def merge_two_procs(self, newname, other):
def merge_two_procs(self, newname: str, other: Proc):
self.name, self.original_name = newname, newname
self.stmts += other.stmts
self.provided_labels |= other.provided_labels
Expand All @@ -83,7 +86,7 @@ def optimize(self, keep_labels=None):
# trivial simplifications


def create_instruction_object(self, instruction: Token, args: list[Any | Expression] | None=None) -> baseop:
def create_instruction_object(self, instruction: lark.Token, args: list[Any | Expression] | None=None) -> baseop:
""":param instruction: the instruction name
:param args: a list of strings, each string is an argument to the instruction
:return: An object of type cl, which is a subclass of Instruction.
Expand All @@ -96,14 +99,14 @@ def create_instruction_object(self, instruction: Token, args: list[Any | Express
o.syntetic = False
return o

def find_op_class(self, cmd: Token, args: list[Any | Expression]) -> Any:
def find_op_class(self, cmd: lark.Token, args: list[Any | Expression]) -> Any:
try:
cl = getattr(op, f"_{cmd.lower()}")
except AttributeError:
cl = self.find_op_common_class(cmd, args)
return cl

def find_op_common_class(self, cmd: str, args: list[Any | Expression]) -> type[_instruction3 | _instruction2 | _instruction0 | _jump | _instruction1]:
def find_op_common_class(self, cmd: str, args: list[Any | Expression]) -> type[baseop]:
logging.debug(cmd)
try:
cl = (
Expand All @@ -125,6 +128,7 @@ def create_equ_op(label: str, value: Expression, line_number: int) -> _equ: # T
o = op._equ(label)
if ptrdir := Token.find_tokens(value, PTRDIR):
if isinstance(ptrdir[0], Token):
assert isinstance(ptrdir[0].children, str)
o.original_type = ptrdir[0].children.lower()
elif isinstance(ptrdir[0], str):
o.original_type = ptrdir[0].lower()
Expand Down

0 comments on commit 6c8c94c

Please sign in to comment.