Skip to content

Commit

Permalink
[cocas] Make imports better and fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
cjvth committed Feb 8, 2024
1 parent 50d63d1 commit d6718d0
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cocas/assembler/ast_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def visitConnective_condition(self, ctx: AsmParser.Connective_conditionContext):
cond.conjunction = ctx.conjunction().getText()
if cond.conjunction != 'and' and cond.conjunction != 'or':
raise AssemblerException(AsmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
'Expected "and" or "or" in compound condition')
'Expected "and" or "or" in compound condition')
return cond

def visitCondition(self, ctx: AsmParser.ConditionContext):
Expand Down Expand Up @@ -219,7 +219,7 @@ def visitStandaloneLabel(self, ctx: AsmParser.StandaloneLabelContext) -> LabelDe
label_decl.external = ctx.Ext() is not None
if label_decl.entry and label_decl.external:
raise AssemblerException(AsmExceptionTag.ASM, self.source_path, ctx.start.line - self.line_offset,
f'Label {label_decl.label.name} cannot be both external and entry')
f'Label {label_decl.label.name} cannot be both external and entry')
return label_decl

def visitLabel_declaration(self, ctx: AsmParser.Label_declarationContext) -> LabelDeclarationNode:
Expand Down
2 changes: 1 addition & 1 deletion cocas/assembler/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from antlr4.error.ErrorListener import ErrorListener

from cocas.assembler.generated import AsmParser
from .generated import AsmParser


class AsmExceptionTag(Enum):
Expand Down
3 changes: 1 addition & 2 deletions cocas/assembler/object_generator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dataclasses import dataclass
from typing import Type

from cocas.assembler.targets import IVaryingLengthSegment
from cocas.object_module import CodeLocation, ObjectModule

from .ast_nodes import InstructionNode, LabelDeclarationNode, ProgramNode, TemplateSectionNode
from .code_block import Section
from .exceptions import AsmExceptionTag
from .targets import TargetInstructionsInterface
from .targets import IVaryingLengthSegment, TargetInstructionsInterface

TAG = AsmExceptionTag.ASM

Expand Down
4 changes: 1 addition & 3 deletions cocas/assembler/targets/abstract_code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
from math import lcm
from typing import TYPE_CHECKING

from cocas.object_module import CodeLocation
from cocas.object_module import CodeLocation, ObjectSectionRecord

from ..exceptions import AsmExceptionTag, AssemblerException

if TYPE_CHECKING:
from cocas.object_module import ObjectSectionRecord

from ..code_block import Section


Expand Down
2 changes: 1 addition & 1 deletion cocas/assembler/targets/abstract_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .abstract_code_segments import ICodeSegment

if TYPE_CHECKING:
from cocas.assembler.ast_nodes import InstructionNode
from ..ast_nodes import InstructionNode


class TargetInstructionsInterface(ABC):
Expand Down
4 changes: 1 addition & 3 deletions cocas/assembler/targets/cdm16/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

import bitstruct

from cocas.object_module import CodeLocation, ExternalEntry
from cocas.object_module import CodeLocation, ExternalEntry, ObjectSectionRecord

from ...ast_nodes import LabelNode, RegisterNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import AsmExceptionTag, AssemblerException
from .. import IAlignedSegment, IAlignmentPaddingSegment, ICodeSegment, IVaryingLengthSegment

if TYPE_CHECKING:
from cocas.object_module import ObjectSectionRecord

from ...code_block import Section


Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/targets/cdm16/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def assemble_instruction(line: InstructionNode, temp_storage: dict) -> list[ICod
if line.mnemonic.startswith('b'):
return TargetInstructions.branch(line)
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Unknown instruction "{line.mnemonic}"')
f'Unknown instruction "{line.mnemonic}"')
except CdmTempException as e:
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line, e.message)

Expand Down Expand Up @@ -185,7 +185,7 @@ def branch(line: InstructionNode, inverse=False) -> list[ICodeSegment]:
break
else:
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Invalid branch condition: {cond}')
f'Invalid branch condition: {cond}')
assert_count_args(line.arguments, RelocatableExpressionNode)
return [Branch(line.location, branch_code, line.arguments[0])]

Expand Down
5 changes: 2 additions & 3 deletions cocas/assembler/targets/cdm8/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from cocas.object_module import CodeLocation, ExternalEntry
from cocas.object_module import CodeLocation, ExternalEntry, ObjectSectionRecord

from ...ast_nodes import LabelNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import AsmExceptionTag, AssemblerException
from .. import IAlignmentPaddingSegment, ICodeSegment

if TYPE_CHECKING:
from cocas.assembler.code_block import Section
from cocas.object_module import ObjectSectionRecord
from ...code_block import Section


def _error(segment: ICodeSegment, message: str):
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/targets/cdm8/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def assemble_instruction(line: InstructionNode, temp_storage: dict) -> list[ICod
if line.mnemonic.startswith('b'):
return TargetInstructions.branch(line)
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Unknown instruction "{line.mnemonic}"')
f'Unknown instruction "{line.mnemonic}"')
except CdmTempException as e:
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line, e.message)

Expand Down Expand Up @@ -156,7 +156,7 @@ def branch(line: InstructionNode, inverse=False) -> list[ICodeSegment]:
break
else:
raise AssemblerException(AsmExceptionTag.ASM, line.location.file, line.location.line,
f'Invalid branch condition: {cond}')
f'Invalid branch condition: {cond}')
assert_count_args(line.arguments, RelocatableExpressionNode)
return [BytesSegment(pack('u4u4', 0xE, branch_code), line.location),
ExpressionSegment(line.arguments[0], line.location)]
Expand Down
4 changes: 1 addition & 3 deletions cocas/assembler/targets/cdm8e/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from cocas.object_module import CodeLocation, ExternalEntry
from cocas.object_module import CodeLocation, ExternalEntry, ObjectSectionRecord

from ...ast_nodes import LabelNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import AsmExceptionTag, AssemblerException
Expand All @@ -12,8 +12,6 @@
TAG = AsmExceptionTag.ASM

if TYPE_CHECKING:
from cocas.object_module import ObjectSectionRecord

from ...code_block import Section


Expand Down
2 changes: 1 addition & 1 deletion cocas/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def main():
if obj_files and args.compile:
print("Error: object files should not be provided with --compile option")
return 2
objects: list[tuple[Path, ObjectModule]] = []
objects: list[tuple[Path, ObjectModule]]
try:
objects: list[tuple[Path, ObjectModule]] = list(itertools.chain(
assemble_files(target, asm_files, bool(args.debug), relative_path, absolute_path, realpath),
Expand Down
5 changes: 0 additions & 5 deletions cocas/object_file/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from antlr4.error.ErrorListener import ErrorListener

from cocas.assembler.generated import AsmParser


class ObjectFileException(Exception):
def __init__(self, file: str, line: int, description: str):
Expand All @@ -15,7 +13,4 @@ def __init__(self, file):
self.file = file

def syntaxError(self, recognizer, offending_symbol, line, column, msg, e):
if isinstance(recognizer, AsmParser):
line = line - recognizer.current_offset
self.file = recognizer.current_file
raise ObjectFileException(self.file, line, msg)
12 changes: 5 additions & 7 deletions cocas/object_file/object_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from cocas.object_module import CodeLocation, ExternalEntry, ObjectModule, ObjectSectionRecord

from .exceptions import AntlrErrorListener, ObjectFileException
from .generated.ObjectFileLexer import ObjectFileLexer
from .generated.ObjectFileParser import ObjectFileParser
from .generated.ObjectFileParserVisitor import ObjectFileParserVisitor
from .generated import ObjectFileLexer, ObjectFileParser, ObjectFileParserVisitor
from .targets import TargetParams, import_target


Expand All @@ -29,13 +27,13 @@ def visitObject_file(self, ctx: ObjectFileParser.Object_fileContext) -> list[Obj
if header != exp_header:
if exp_header:
raise ObjectFileException(self.file, ctx.start.line,
f'Wrong target header {header}, expected {exp_header}')
f'Wrong target header {header}, expected {exp_header}')
else:
raise ObjectFileException(self.file, ctx.start.line,
f'Expected no header for {target_name} target, got {header}')
f'Expected no header for {target_name} target, got {header}')
elif exp_header:
raise ObjectFileException(self.file, ctx.start.line,
f'Expected non-empty target header for {target_name}, got empty')
f'Expected non-empty target header for {target_name}, got empty')

modules = []
for i in ctx.object_block():
Expand Down Expand Up @@ -67,7 +65,7 @@ def visitObject_block(self, ctx: ObjectFileParser.Object_blockContext) -> Object
if sect == '$abs':
if not asects:
raise ObjectFileException(self.file, xtrn.start.line,
'No absolute sections found, but needed for xtrn entry')
'No absolute sections found, but needed for xtrn entry')
# what is this?
ind = max(bisect.bisect_right(asect_addr, entry.offset) - 1, 0)
asects[asect_addr[ind]].external[label].append(entry)
Expand Down

0 comments on commit d6718d0

Please sign in to comment.