Skip to content

Commit

Permalink
add lib support
Browse files Browse the repository at this point in the history
  • Loading branch information
ruskaof committed Dec 17, 2023
1 parent b8c1092 commit a58e319
Show file tree
Hide file tree
Showing 4 changed files with 3,220 additions and 2,805 deletions.
15 changes: 14 additions & 1 deletion computer_simulator/translator/translator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import sys
from pathlib import Path

Expand All @@ -19,10 +20,22 @@ def read_file(filename: str) -> str:
return Path(filename).read_text(encoding="utf-8")


# find all expressions like (include "file") and replace them with the contents of the file
def preprocess(source: str) -> str:
regex = re.compile(r'\(include\s+"(.+)"\)')
match = regex.search(source)
while match:
file = match.group(1)
source = source.replace(match.group(), read_file(file))
match = regex.search(source)
return source


def main(source: str, target: str) -> None:
with open(target, "w", encoding="utf-8") as f:
source_code: str = read_file(source)
tokenized_code: list[Token] = tokenize(source_code)
preprocessed_code: str = preprocess(source_code)
tokenized_code: list[Token] = tokenize(preprocessed_code)
program: Program = run_translator(tokenized_code)

print(
Expand Down
Loading

0 comments on commit a58e319

Please sign in to comment.