prog8/il65/preprocess.py

71 lines
2.6 KiB
Python
Raw Normal View History

2017-12-21 13:52:30 +00:00
"""
2017-12-25 15:00:25 +00:00
Programming Language for 6502/6510 microprocessors
2017-12-21 13:52:30 +00:00
This is the preprocessing parser of the IL65 code, that only generates a symbol table.
Written by Irmen de Jong (irmen@razorvine.net)
License: GNU GPL 3.0, see LICENSE
"""
2017-12-30 12:34:52 +00:00
from typing import List, Tuple, Set
2017-12-21 13:52:30 +00:00
from .parse import Parser, ParseResult, SymbolTable, SymbolDefinition
2017-12-28 18:08:33 +00:00
from .symbols import SourceRef
2017-12-31 03:45:27 +00:00
from .astdefs import _AstNode, InlineAsm
2017-12-21 13:52:30 +00:00
class PreprocessingParser(Parser):
2017-12-30 12:34:52 +00:00
def __init__(self, filename: str, existing_imports: Set[str], parsing_import: bool=False) -> None:
super().__init__(filename, "", existing_imports=existing_imports, parsing_import=parsing_import)
self.print_block_parsing = False
2017-12-21 13:52:30 +00:00
def preprocess(self) -> Tuple[List[Tuple[int, str]], SymbolTable]:
def cleanup_table(symbols: SymbolTable):
symbols.owning_block = None # not needed here
for name, symbol in list(symbols.symbols.items()):
if isinstance(symbol, SymbolTable):
cleanup_table(symbol)
elif not isinstance(symbol, SymbolDefinition):
del symbols.symbols[name]
self.parse()
cleanup_table(self.root_scope)
return self.lines, self.root_scope
2017-12-28 18:08:33 +00:00
def print_warning(self, text: str, sourceref: SourceRef=None) -> None:
pass
2017-12-21 13:52:30 +00:00
def load_source(self, filename: str) -> List[Tuple[int, str]]:
lines = super().load_source(filename)
# can do some additional source-level preprocessing here
return lines
def parse_file(self) -> ParseResult:
2017-12-30 12:34:52 +00:00
print("preprocessing", self.sourceref.file)
2017-12-21 13:52:30 +00:00
self._parse_1()
return self.result
2017-12-31 03:45:27 +00:00
def parse_asminclude(self, line: str) -> InlineAsm:
return InlineAsm([], self.sourceref)
2017-12-21 13:52:30 +00:00
2017-12-31 03:45:27 +00:00
def parse_statement(self, line: str) -> _AstNode:
2017-12-21 13:52:30 +00:00
return None # type: ignore
def parse_var_def(self, line: str) -> None:
super().parse_var_def(line)
def parse_const_def(self, line: str) -> None:
super().parse_const_def(line)
def parse_memory_def(self, line: str, is_zeropage: bool=False) -> None:
super().parse_memory_def(line, is_zeropage)
def parse_label(self, line: str) -> None:
super().parse_label(line)
2017-12-25 18:09:10 +00:00
def parse_subroutine_def(self, line: str) -> None:
super().parse_subroutine_def(line)
2017-12-21 13:52:30 +00:00
2017-12-31 03:45:27 +00:00
def create_import_parser(self, filename: str, outputdir: str) -> Parser:
2017-12-30 12:34:52 +00:00
return PreprocessingParser(filename, parsing_import=True, existing_imports=self.existing_imports)
def print_import_progress(self, message: str, *args: str) -> None:
pass