Skip to content

Commit

Permalink
[cocas] Add syntax and AST support for section attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kapkekes committed Oct 19, 2024
1 parent 4b6b1df commit da25c05
Show file tree
Hide file tree
Showing 14 changed files with 861 additions and 665 deletions.
30 changes: 21 additions & 9 deletions cocas/assembler/ast_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import warnings
from base64 import b64decode
from pathlib import Path
from typing import Literal, overload

from antlr4 import CommonTokenStream, InputStream

from cocas.object_module import CodeLocation

from .ast_nodes import (
from cocas.assembler.ast_nodes import (
AbsoluteSectionNode,
BreakStatementNode,
ConditionalStatementNode,
Expand All @@ -26,8 +25,9 @@
UntilLoopNode,
WhileLoopNode,
)
from .exceptions import AntlrErrorListener, AssemblerException, AssemblerExceptionTag
from .generated import AsmLexer, AsmParser, AsmParserVisitor
from cocas.assembler.exceptions import AntlrErrorListener, AssemblerException, AssemblerExceptionTag
from cocas.assembler.generated import AsmLexer, AsmParser, AsmParserVisitor
from cocas.object_module import CodeLocation


# noinspection PyPep8Naming
Expand Down Expand Up @@ -96,17 +96,25 @@ def check_label_is_ext(label: LabelDeclarationNode):
raise AssemblerException(AssemblerExceptionTag.ASM, label.location.file, label.location.line,
"Only external labels are allowed at the top of a file")

def visitSection_attr(self, ctx: AsmParser.Section_attrContext) -> str:
return ctx.WORD().getText()

def visitSection_attrs(self, ctx: AsmParser.Section_attrsContext) -> list[str]:
return [self.visitSection_attr(section_attr) for section_attr in ctx.section_attr()]

def visitAbsoluteSection(self, ctx: AsmParser.AbsoluteSectionContext) -> AbsoluteSectionNode:
header = ctx.asect_header()
lines = self.visitSection_body(ctx.section_body())
attributes = self.visitSection_attrs(header.section_attrs())
address = self.visitNumber(header.number())
return AbsoluteSectionNode(lines, address)
return AbsoluteSectionNode(lines, attributes, address)

def visitRelocatableSection(self, ctx: AsmParser.RelocatableSectionContext) -> RelocatableSectionNode:
header = ctx.rsect_header()
lines = self.visitSection_body(ctx.section_body())
attributes = self.visitSection_attrs(header.section_attrs())
name = header.name().getText()
return RelocatableSectionNode(lines, name)
return RelocatableSectionNode(lines, attributes, name)

def visitTemplateSection(self, ctx: AsmParser.TemplateSectionContext) -> TemplateSectionNode:
header = ctx.tplate_header()
Expand Down Expand Up @@ -185,7 +193,11 @@ def visitUntil_loop(self, ctx: AsmParser.Until_loopContext):
mnemonic = ctx.branch_mnemonic()
return UntilLoopNode(lines, mnemonic.getText(), self._ctx_location(mnemonic))

def visitCode_block(self, ctx: AsmParser.Code_blockContext, return_locations=False):
@overload
def visitCode_block(self, ctx: AsmParser.Code_blockContext, return_locations: Literal[True]) -> tuple[list, list]: ...
@overload
def visitCode_block(self, ctx: AsmParser.Code_blockContext, return_locations: Literal[False] = False) -> list: ...
def visitCode_block(self, ctx: AsmParser.Code_blockContext, return_locations: bool = False):
if ctx.children is None:
if return_locations:
return [], []
Expand Down Expand Up @@ -313,7 +325,7 @@ def visitArguments(self, ctx: AsmParser.ArgumentsContext):
return [self.visitArgument(i) for i in ctx.children if isinstance(i, AsmParser.ArgumentContext)]


def build_ast(input_stream: InputStream, filepath: Path):
def build_ast(input_stream: InputStream, filepath: Path) -> Node:
str_path = filepath.absolute().as_posix()
lexer = AsmLexer(input_stream)
lexer.removeErrorListeners()
Expand Down
11 changes: 8 additions & 3 deletions cocas/assembler/ast_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,21 @@ class ContinueStatementNode(ExportLocationNode):

@dataclass
class SectionNode(Node):
lines: list
lines: list[Node]


@dataclass
class AttributeMixin:
attributes: list[str]


@dataclass
class AbsoluteSectionNode(SectionNode):
class AbsoluteSectionNode(AttributeMixin, SectionNode):
address: int


@dataclass
class RelocatableSectionNode(SectionNode):
class RelocatableSectionNode(AttributeMixin, SectionNode):
name: str


Expand Down
262 changes: 134 additions & 128 deletions cocas/assembler/generated/AsmLexer.py

Large diffs are not rendered by default.

1,104 changes: 631 additions & 473 deletions cocas/assembler/generated/AsmParser.py

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion cocas/assembler/generated/AsmParserVisitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from assembler/grammar/AsmParser.g4 by ANTLR 4.13.1
# Generated from assembler/grammar/AsmParser.g4 by ANTLR 4.13.2
from antlr4 import *
if "." in __name__:
from .AsmParser import AsmParser
Expand Down Expand Up @@ -37,6 +37,16 @@ def visitTemplateSection(self, ctx:AsmParser.TemplateSectionContext):
return self.visitChildren(ctx)


# Visit a parse tree produced by AsmParser#section_attr.
def visitSection_attr(self, ctx:AsmParser.Section_attrContext):
return self.visitChildren(ctx)


# Visit a parse tree produced by AsmParser#section_attrs.
def visitSection_attrs(self, ctx:AsmParser.Section_attrsContext):
return self.visitChildren(ctx)


# Visit a parse tree produced by AsmParser#asect_header.
def visitAsect_header(self, ctx:AsmParser.Asect_headerContext):
return self.visitChildren(ctx)
Expand Down
79 changes: 40 additions & 39 deletions cocas/assembler/generated/MacroLexer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.1
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.2
from antlr4 import *
from io import StringIO
import sys
Expand All @@ -24,43 +24,44 @@ def serializedATN():
138,0,0,17,1,1,3,2,5,3,7,4,9,5,11,6,13,7,15,8,17,9,19,10,21,11,23,
12,25,13,27,14,29,15,31,16,33,17,1,0,9,2,0,9,9,32,32,1,0,10,10,2,
0,58,58,62,62,3,0,10,10,34,34,92,92,3,0,10,10,39,39,92,92,3,0,65,
90,95,95,97,122,5,0,46,46,48,57,65,90,95,95,97,122,1,0,48,57,3,0,
40,41,43,43,45,45,153,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,0,7,1,
0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,0,13,1,0,0,0,0,15,1,0,0,0,0,17,1,
0,0,0,0,19,1,0,0,0,0,21,1,0,0,0,0,23,1,0,0,0,0,25,1,0,0,0,0,27,1,
0,0,0,0,29,1,0,0,0,0,31,1,0,0,0,0,33,1,0,0,0,1,35,1,0,0,0,3,44,1,
0,0,0,5,64,1,0,0,0,7,69,1,0,0,0,9,73,1,0,0,0,11,82,1,0,0,0,13,84,
1,0,0,0,15,86,1,0,0,0,17,88,1,0,0,0,19,90,1,0,0,0,21,105,1,0,0,0,
23,120,1,0,0,0,25,127,1,0,0,0,27,129,1,0,0,0,29,131,1,0,0,0,31,133,
1,0,0,0,33,136,1,0,0,0,35,36,5,109,0,0,36,37,5,97,0,0,37,38,5,99,
0,0,38,39,5,114,0,0,39,40,5,111,0,0,40,2,1,0,0,0,41,43,7,0,0,0,42,
41,1,0,0,0,43,46,1,0,0,0,44,42,1,0,0,0,44,45,1,0,0,0,45,47,1,0,0,
0,46,44,1,0,0,0,47,48,5,109,0,0,48,49,5,101,0,0,49,50,5,110,0,0,
50,51,5,100,0,0,51,55,1,0,0,0,52,54,7,0,0,0,53,52,1,0,0,0,54,57,
1,0,0,0,55,53,1,0,0,0,55,56,1,0,0,0,56,59,1,0,0,0,57,55,1,0,0,0,
58,60,5,13,0,0,59,58,1,0,0,0,59,60,1,0,0,0,60,61,1,0,0,0,61,62,5,
10,0,0,62,4,1,0,0,0,63,65,7,0,0,0,64,63,1,0,0,0,65,66,1,0,0,0,66,
64,1,0,0,0,66,67,1,0,0,0,67,6,1,0,0,0,68,70,5,13,0,0,69,68,1,0,0,
0,69,70,1,0,0,0,70,71,1,0,0,0,71,72,5,10,0,0,72,8,1,0,0,0,73,77,
5,35,0,0,74,76,8,1,0,0,75,74,1,0,0,0,76,79,1,0,0,0,77,75,1,0,0,0,
77,78,1,0,0,0,78,80,1,0,0,0,79,77,1,0,0,0,80,81,6,4,0,0,81,10,1,
0,0,0,82,83,5,42,0,0,83,12,1,0,0,0,84,85,5,44,0,0,85,14,1,0,0,0,
86,87,7,2,0,0,87,16,1,0,0,0,88,89,5,63,0,0,89,18,1,0,0,0,90,100,
5,34,0,0,91,93,8,3,0,0,92,91,1,0,0,0,93,94,1,0,0,0,94,92,1,0,0,0,
94,95,1,0,0,0,95,99,1,0,0,0,96,97,5,92,0,0,97,99,9,0,0,0,98,92,1,
0,0,0,98,96,1,0,0,0,99,102,1,0,0,0,100,98,1,0,0,0,100,101,1,0,0,
0,101,103,1,0,0,0,102,100,1,0,0,0,103,104,5,34,0,0,104,20,1,0,0,
0,105,115,5,39,0,0,106,108,8,4,0,0,107,106,1,0,0,0,108,109,1,0,0,
0,109,107,1,0,0,0,109,110,1,0,0,0,110,114,1,0,0,0,111,112,5,92,0,
0,112,114,9,0,0,0,113,107,1,0,0,0,113,111,1,0,0,0,114,117,1,0,0,
0,115,113,1,0,0,0,115,116,1,0,0,0,116,118,1,0,0,0,117,115,1,0,0,
0,118,119,5,39,0,0,119,22,1,0,0,0,120,124,7,5,0,0,121,123,7,6,0,
0,122,121,1,0,0,0,123,126,1,0,0,0,124,122,1,0,0,0,124,125,1,0,0,
0,125,24,1,0,0,0,126,124,1,0,0,0,127,128,7,7,0,0,128,26,1,0,0,0,
129,130,5,47,0,0,130,28,1,0,0,0,131,132,5,39,0,0,132,30,1,0,0,0,
133,134,5,36,0,0,134,32,1,0,0,0,135,137,7,8,0,0,136,135,1,0,0,0,
137,138,1,0,0,0,138,136,1,0,0,0,138,139,1,0,0,0,139,34,1,0,0,0,15,
0,44,55,59,66,69,77,94,98,100,109,113,115,124,138,1,6,0,0
90,95,95,97,122,5,0,46,46,48,57,65,90,95,95,97,122,1,0,48,57,5,0,
40,41,43,43,45,45,91,91,93,93,153,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,
0,0,0,0,7,1,0,0,0,0,9,1,0,0,0,0,11,1,0,0,0,0,13,1,0,0,0,0,15,1,0,
0,0,0,17,1,0,0,0,0,19,1,0,0,0,0,21,1,0,0,0,0,23,1,0,0,0,0,25,1,0,
0,0,0,27,1,0,0,0,0,29,1,0,0,0,0,31,1,0,0,0,0,33,1,0,0,0,1,35,1,0,
0,0,3,44,1,0,0,0,5,64,1,0,0,0,7,69,1,0,0,0,9,73,1,0,0,0,11,82,1,
0,0,0,13,84,1,0,0,0,15,86,1,0,0,0,17,88,1,0,0,0,19,90,1,0,0,0,21,
105,1,0,0,0,23,120,1,0,0,0,25,127,1,0,0,0,27,129,1,0,0,0,29,131,
1,0,0,0,31,133,1,0,0,0,33,136,1,0,0,0,35,36,5,109,0,0,36,37,5,97,
0,0,37,38,5,99,0,0,38,39,5,114,0,0,39,40,5,111,0,0,40,2,1,0,0,0,
41,43,7,0,0,0,42,41,1,0,0,0,43,46,1,0,0,0,44,42,1,0,0,0,44,45,1,
0,0,0,45,47,1,0,0,0,46,44,1,0,0,0,47,48,5,109,0,0,48,49,5,101,0,
0,49,50,5,110,0,0,50,51,5,100,0,0,51,55,1,0,0,0,52,54,7,0,0,0,53,
52,1,0,0,0,54,57,1,0,0,0,55,53,1,0,0,0,55,56,1,0,0,0,56,59,1,0,0,
0,57,55,1,0,0,0,58,60,5,13,0,0,59,58,1,0,0,0,59,60,1,0,0,0,60,61,
1,0,0,0,61,62,5,10,0,0,62,4,1,0,0,0,63,65,7,0,0,0,64,63,1,0,0,0,
65,66,1,0,0,0,66,64,1,0,0,0,66,67,1,0,0,0,67,6,1,0,0,0,68,70,5,13,
0,0,69,68,1,0,0,0,69,70,1,0,0,0,70,71,1,0,0,0,71,72,5,10,0,0,72,
8,1,0,0,0,73,77,5,35,0,0,74,76,8,1,0,0,75,74,1,0,0,0,76,79,1,0,0,
0,77,75,1,0,0,0,77,78,1,0,0,0,78,80,1,0,0,0,79,77,1,0,0,0,80,81,
6,4,0,0,81,10,1,0,0,0,82,83,5,42,0,0,83,12,1,0,0,0,84,85,5,44,0,
0,85,14,1,0,0,0,86,87,7,2,0,0,87,16,1,0,0,0,88,89,5,63,0,0,89,18,
1,0,0,0,90,100,5,34,0,0,91,93,8,3,0,0,92,91,1,0,0,0,93,94,1,0,0,
0,94,92,1,0,0,0,94,95,1,0,0,0,95,99,1,0,0,0,96,97,5,92,0,0,97,99,
9,0,0,0,98,92,1,0,0,0,98,96,1,0,0,0,99,102,1,0,0,0,100,98,1,0,0,
0,100,101,1,0,0,0,101,103,1,0,0,0,102,100,1,0,0,0,103,104,5,34,0,
0,104,20,1,0,0,0,105,115,5,39,0,0,106,108,8,4,0,0,107,106,1,0,0,
0,108,109,1,0,0,0,109,107,1,0,0,0,109,110,1,0,0,0,110,114,1,0,0,
0,111,112,5,92,0,0,112,114,9,0,0,0,113,107,1,0,0,0,113,111,1,0,0,
0,114,117,1,0,0,0,115,113,1,0,0,0,115,116,1,0,0,0,116,118,1,0,0,
0,117,115,1,0,0,0,118,119,5,39,0,0,119,22,1,0,0,0,120,124,7,5,0,
0,121,123,7,6,0,0,122,121,1,0,0,0,123,126,1,0,0,0,124,122,1,0,0,
0,124,125,1,0,0,0,125,24,1,0,0,0,126,124,1,0,0,0,127,128,7,7,0,0,
128,26,1,0,0,0,129,130,5,47,0,0,130,28,1,0,0,0,131,132,5,39,0,0,
132,30,1,0,0,0,133,134,5,36,0,0,134,32,1,0,0,0,135,137,7,8,0,0,136,
135,1,0,0,0,137,138,1,0,0,0,138,136,1,0,0,0,138,139,1,0,0,0,139,
34,1,0,0,0,15,0,44,55,59,66,69,77,94,98,100,109,113,115,124,138,
1,6,0,0
]

class MacroLexer(Lexer):
Expand Down Expand Up @@ -108,7 +109,7 @@ class MacroLexer(Lexer):

def __init__(self, input=None, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.13.1")
self.checkVersion("4.13.2")
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
self._predicates = None
Expand Down
4 changes: 2 additions & 2 deletions cocas/assembler/generated/MacroParser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.1
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.2
# encoding: utf-8
from antlr4 import *
from io import StringIO
Expand Down Expand Up @@ -167,7 +167,7 @@ class MacroParser ( Parser ):

def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.13.1")
self.checkVersion("4.13.2")
self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
self._predicates = None

Expand Down
2 changes: 1 addition & 1 deletion cocas/assembler/generated/MacroVisitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.1
# Generated from assembler/grammar/Macro.g4 by ANTLR 4.13.2
from antlr4 import *
if "." in __name__:
from .MacroParser import MacroParser
Expand Down
3 changes: 2 additions & 1 deletion cocas/assembler/grammar/AsmLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ ANGLE_BRACKET : '>' ;
OPEN_PAREN : '(' ;
CLOSE_PAREN : ')' ;
LINE_MARK_MARKER: '-|';
OPEN_SQUARE_BRACKET : '[';
CLOSE_SQUARE_BRACKET : ']';

REGISTER : 'r'DECIMAL_NUMBER ;
WORD : [a-zA-Z_][a-zA-Z_0-9]* ;
Expand All @@ -48,4 +50,3 @@ BASE64 : 'fp-' [a-zA-Z0-9/+=]+;


UNEXPECTED_TOKEN: [\u0000-\uFFFE];

7 changes: 5 additions & 2 deletions cocas/assembler/grammar/AsmParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ section
| tplate_header section_body # templateSection
;

asect_header : Asect number NEWLINE+ ;
rsect_header : Rsect name NEWLINE+ ;
section_attr : WORD ;
section_attrs : OPEN_SQUARE_BRACKET (section_attr (COMMA section_attr)*)? CLOSE_SQUARE_BRACKET;

asect_header : Asect section_attrs? number NEWLINE+ ;
rsect_header : Rsect section_attrs? name NEWLINE+ ;
tplate_header : Tplate name NEWLINE+ ;

section_body : code_block ;
Expand Down
2 changes: 1 addition & 1 deletion cocas/assembler/grammar/Macro.g4
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ SLASH : '/' ;
APOSTROPHE : '\'' ;
DOLLAR_SIGN : '$' ;

OTHER : [-+()]+ ;
OTHER : [-+()[\]]+ ;
4 changes: 2 additions & 2 deletions cocas/object_file/generated/ObjectFileLexer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from object_file/grammar/ObjectFileLexer.g4 by ANTLR 4.13.1
# Generated from object_file/grammar/ObjectFileLexer.g4 by ANTLR 4.13.2
from antlr4 import *
from io import StringIO
import sys
Expand Down Expand Up @@ -148,7 +148,7 @@ class ObjectFileLexer(Lexer):

def __init__(self, input=None, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.13.1")
self.checkVersion("4.13.2")
self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
self._predicates = None
Expand Down
4 changes: 2 additions & 2 deletions cocas/object_file/generated/ObjectFileParser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from object_file/grammar/ObjectFileParser.g4 by ANTLR 4.13.1
# Generated from object_file/grammar/ObjectFileParser.g4 by ANTLR 4.13.2
# encoding: utf-8
from antlr4 import *
from io import StringIO
Expand Down Expand Up @@ -175,7 +175,7 @@ class ObjectFileParser ( Parser ):

def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.13.1")
self.checkVersion("4.13.2")
self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
self._predicates = None

Expand Down
2 changes: 1 addition & 1 deletion cocas/object_file/generated/ObjectFileParserVisitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from object_file/grammar/ObjectFileParser.g4 by ANTLR 4.13.1
# Generated from object_file/grammar/ObjectFileParser.g4 by ANTLR 4.13.2
from antlr4 import *
if "." in __name__:
from .ObjectFileParser import ObjectFileParser
Expand Down

0 comments on commit da25c05

Please sign in to comment.