-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessor.py
More file actions
49 lines (36 loc) · 1.55 KB
/
Preprocessor.py
File metadata and controls
49 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from Definitions import *
from Exceptions import PreprocessorError
from Program import *
from typing import List, Optional, Union
import re
class Preprocessor():
"""
Processes IMPORT instructions and adds indicators with filename and linenumber :
Like this:
__source__ file.iasm
__line__ 10
The assembler uses these lines to keep track of the linenumber/sourcefile
"""
def __init__(self):
pass
def preprocess(self, program: Program) -> None:
# print(f"PREPROCESSING { program.sourceFile }")
lines = program.source.replace("\t", " ").split("\n")
program.preprocessed.clear()
program.preprocessed.append(f"__source__ { program.sourceFile }")
program.preprocessed.append(f"__line__ {1}")
for ix, line in enumerate(lines):
parts = line.split(" ")
if parts[0] == "IMPORT":
if len(parts) == 2:
sourceFile = os.path.join(program.sourceFolder, parts[1])
include = Program(sourceFile)
preproc = Preprocessor()
preproc.preprocess(include)
program.preprocessed = program.preprocessed + include.preprocessed
program.preprocessed.append(f"__source__ { program.sourceFile }")
program.preprocessed.append(f"__line__ {ix + 2}")
else:
raise PreprocessorError("SYNTAX ERROR", ix, program.sourceFile)
else:
program.preprocessed.append(line)