Skip to content

Commit

Permalink
[cocas] Fix remaining warnings in cocas
Browse files Browse the repository at this point in the history
  • Loading branch information
cjvth committed Feb 2, 2024
1 parent 6f3815c commit 869ccae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 6 additions & 2 deletions cocas/assembler/macro_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from base64 import b64encode
from dataclasses import dataclass
from pathlib import Path
from typing import Optional

from antlr4 import CommonTokenStream, FileStream, InputStream
from antlr4.TokenStreamRewriter import TokenStreamRewriter
Expand Down Expand Up @@ -102,7 +103,9 @@ def sub_all(ps):

# noinspection PyPep8Naming
class ExpandMacrosVisitor(MacroVisitor):
def __init__(self, rewriter: TokenStreamRewriter, mlb_macros, filepath: str):
def __init__(self, rewriter: Optional[TokenStreamRewriter], mlb_macros, filepath: str):
# rewriter should be None if then will be called .visit(MlbContext)
# rewriter should be valid if then will be called .visit(ProgramContext)
self.nonce = 0
self.macros = {name: mlb_macros[name].copy() for name in mlb_macros}
self.rewriter = rewriter
Expand Down Expand Up @@ -294,7 +297,8 @@ def read_mlb(filepath: Path):
token_stream = CommonTokenStream(lexer)
parser = MacroParser(token_stream)
cst = parser.mlb()
return ExpandMacrosVisitor(None, dict(), str_path).visit(cst)
emv = ExpandMacrosVisitor(None, dict(), str_path)
return emv.visit(cst)


def process_macros(input_stream: InputStream, library_macros, filepath: Path):
Expand Down
13 changes: 8 additions & 5 deletions cocas/assembler/targets/cdm8e/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def location(self, value):

@dataclass
class RelocatableExpressionSegment(CodeSegment, ABC):
expr: RelocatableExpressionNode
expr: RelocatableExpressionNode = field(init=True)


@dataclass
Expand Down Expand Up @@ -133,10 +133,13 @@ def fill(self, object_record: "ObjectSectionRecord", section: "Section", labels:
object_record.data.extend(val.to_bytes(self.size, 'little', signed=(val < 0)))


@dataclass
class GotoSegment(VaryingLengthSegment):
branch_mnemonic: str
expr: RelocatableExpressionNode
class GotoSegment(RelocatableExpressionSegment, VaryingLengthSegment):
def __init__(self, branch_mnemonic: str, expr: RelocatableExpressionNode):
# noinspection PyArgumentList
RelocatableExpressionSegment.__init__(self, expr)
VaryingLengthSegment.__init__(self)
self.branch_mnemonic = branch_mnemonic

base_size = 2
expanded_size = 5

Expand Down
6 changes: 3 additions & 3 deletions cocas/linker/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any

from cocas.error import CdmLinkException
from cocas.object_module import CodeLocation, ExternalEntry, ObjectModule, ObjectSectionRecord
from cocas.object_module import CodeLocation, ObjectModule, ObjectSectionRecord


def init_bins(asects: list[ObjectSectionRecord]):
Expand Down Expand Up @@ -119,7 +119,7 @@ def link(objects: list[tuple[Any, ObjectModule]]):
image_end = image_begin + len(rsect.data)
image[image_begin:image_end] = rsect.data
entry_bytes: range
for offset, entry_bytes, sign in map(ExternalEntry.as_tuple, rsect.relative):
for offset, entry_bytes, sign in map(lambda x: x.as_tuple(), rsect.relative):
pos = image_begin + offset
lower_limit = 1 << 8 * entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry_bytes)], 'little', signed=False) * lower_limit
Expand All @@ -134,7 +134,7 @@ def link(objects: list[tuple[Any, ObjectModule]]):

for sect in asects + rsects:
for ext_name in sect.external:
for offset, entry_bytes, sign in map(ExternalEntry.as_tuple, sect.external[ext_name]):
for offset, entry_bytes, sign in map(lambda x: x.as_tuple(), sect.external[ext_name]):
pos = sect_addresses[sect.name] + offset
lower_limit = 1 << 8 * entry_bytes.start
val = int.from_bytes(image[pos:pos + len(entry_bytes)], 'little', signed=False) * lower_limit
Expand Down

0 comments on commit 869ccae

Please sign in to comment.