diff --git a/cocas/assembler/ast_builder.py b/cocas/assembler/ast_builder.py index 6dbbe11..a6b6a8e 100644 --- a/cocas/assembler/ast_builder.py +++ b/cocas/assembler/ast_builder.py @@ -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): @@ -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: diff --git a/cocas/assembler/exceptions.py b/cocas/assembler/exceptions.py index 646e9ac..3f549e4 100644 --- a/cocas/assembler/exceptions.py +++ b/cocas/assembler/exceptions.py @@ -2,7 +2,7 @@ from antlr4.error.ErrorListener import ErrorListener -from cocas.assembler.generated import AsmParser +from .generated import AsmParser class AsmExceptionTag(Enum): diff --git a/cocas/assembler/object_generator.py b/cocas/assembler/object_generator.py index 94a5c6e..79bc3cb 100644 --- a/cocas/assembler/object_generator.py +++ b/cocas/assembler/object_generator.py @@ -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 diff --git a/cocas/assembler/targets/abstract_code_segments.py b/cocas/assembler/targets/abstract_code_segments.py index 5d719c6..67b81df 100644 --- a/cocas/assembler/targets/abstract_code_segments.py +++ b/cocas/assembler/targets/abstract_code_segments.py @@ -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 diff --git a/cocas/assembler/targets/abstract_instructions.py b/cocas/assembler/targets/abstract_instructions.py index bb2c85c..1105925 100644 --- a/cocas/assembler/targets/abstract_instructions.py +++ b/cocas/assembler/targets/abstract_instructions.py @@ -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): diff --git a/cocas/assembler/targets/cdm16/code_segments.py b/cocas/assembler/targets/cdm16/code_segments.py index c1617a5..191d1bf 100644 --- a/cocas/assembler/targets/cdm16/code_segments.py +++ b/cocas/assembler/targets/cdm16/code_segments.py @@ -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 diff --git a/cocas/assembler/targets/cdm16/target_instructions.py b/cocas/assembler/targets/cdm16/target_instructions.py index 58c7e30..c696528 100644 --- a/cocas/assembler/targets/cdm16/target_instructions.py +++ b/cocas/assembler/targets/cdm16/target_instructions.py @@ -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) @@ -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])] diff --git a/cocas/assembler/targets/cdm8/code_segments.py b/cocas/assembler/targets/cdm8/code_segments.py index 005e202..4aadf39 100644 --- a/cocas/assembler/targets/cdm8/code_segments.py +++ b/cocas/assembler/targets/cdm8/code_segments.py @@ -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): diff --git a/cocas/assembler/targets/cdm8/target_instructions.py b/cocas/assembler/targets/cdm8/target_instructions.py index 2144a11..d73cbe3 100644 --- a/cocas/assembler/targets/cdm8/target_instructions.py +++ b/cocas/assembler/targets/cdm8/target_instructions.py @@ -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) @@ -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)] diff --git a/cocas/assembler/targets/cdm8e/code_segments.py b/cocas/assembler/targets/cdm8e/code_segments.py index 84de197..65479af 100644 --- a/cocas/assembler/targets/cdm8e/code_segments.py +++ b/cocas/assembler/targets/cdm8e/code_segments.py @@ -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 @@ -12,8 +12,6 @@ TAG = AsmExceptionTag.ASM if TYPE_CHECKING: - from cocas.object_module import ObjectSectionRecord - from ...code_block import Section diff --git a/cocas/main.py b/cocas/main.py index 5131d1a..b80c0c4 100755 --- a/cocas/main.py +++ b/cocas/main.py @@ -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), diff --git a/cocas/object_file/exceptions.py b/cocas/object_file/exceptions.py index 9296f45..0cc8c35 100644 --- a/cocas/object_file/exceptions.py +++ b/cocas/object_file/exceptions.py @@ -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): @@ -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) diff --git a/cocas/object_file/object_import.py b/cocas/object_file/object_import.py index 9bbe356..7d17aef 100644 --- a/cocas/object_file/object_import.py +++ b/cocas/object_file/object_import.py @@ -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 @@ -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(): @@ -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)