Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
dartixus committed Dec 18, 2022
1 parent 1499719 commit ebe6bf1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
10 changes: 2 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ School Project: **Not so Swift Language** compiler
-------
todo

[//]: # (- for defined in (https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html))
[comment]: <> (- for defined in https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html)

[//]: # ()
[//]: # (for index in 1...5 { )

[//]: # ()
[//]: # (some code )

[//]: # ()
[//]: # (})
[comment]: <> ( for index in 1...5 { some code })

- boolean
- closures (lambda expressions)
Expand Down
77 changes: 49 additions & 28 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

import ply.lex
import ply.yacc as yy
# from ete3 import TreeStyle

import src.syntax_analyzer as syntax
import src.lex_analyzer as lexical
import src.pl0_code_generator as gen
from src.syntax_analyzer.utils import generate_table_of_symbols
from src.semantics_analyzer.analyzer import Analyzer


def generate_output_files(dst, generated_code):
"""
It generates output files
:param dst: syntax tree
:param generated_code: a list of strings, each of which is a line of generated code
"""
if "output" not in os.listdir("../"):
os.mkdir("../output")
with open("../output/full_tree.txt", mode="w") as tree:
Expand All @@ -25,20 +31,49 @@ def generate_output_files(dst, generated_code):
generated_code.print_symbol_table(table.writelines)


def main(input_file_name: str):
def visualize_dst(dst, show_tree_with_pyqt5):
# # ###### Showing the tree. with pyqt5 ##################
if show_tree_with_pyqt5:
from ete3 import TreeStyle
tree_style = TreeStyle()
tree_style.show_leaf_name = True
tree_style.mode = "c"
tree_style.arc_start = -180 # 0 degrees = 3 o'clock
dst.show(
tree_style=tree_style
)


def save_generated_code(generated_code, formatted_input_code):
"""
"This function takes a file name as input and returns a list of the words in the file."
It saves the generated code to a file
:param input_file_name: The name of the file that contains the input data
:param generated_code: The code that was generated by the model
:param formatted_input_code: The input code, formatted with the correct indentation
"""
if generated_code.return_code() != "":
# Writing the generated code to a file.
with open("../output/generated_code.txt", mode="w") as txt:
txt.writelines("----------input code----------------\n")
txt.writelines(formatted_input_code)
txt.writelines("\n")
txt.writelines("----------generated code------------\n")
txt.writelines(generated_code.return_code())
txt.writelines("------------------------------------")


def main(input_file_name: str, show_tree_with_pyqt5=False):
"""
> This function takes a file name as input, and returns a list of lists of strings
:param input_file_name: The name of the file to be parsed
:type input_file_name: str
:param show_tree_with_pyqt5: If True, the tree will be displayed using PyQt5, defaults to False (optional)
"""

with open(input_file_name) as f:
code = f.read()
# print("input_code:")
# print(code)
formatted_input_code = copy(code)
code = code.replace("\n", " ")
# Parsing the code_input.
lexer = \
ply.lex.lex(module=lexical)
Expand All @@ -55,38 +90,24 @@ def main(input_file_name: str):
return
'''



generated_code = gen.Pl0(dst, table_of_symbols)

# Generating the output files.
generate_output_files(dst, generated_code)

# Showing the tree. with pyqt5
# tree_style = TreeStyle()
# tree_style.show_leaf_name = True
# tree_style.mode = "c"
# tree_style.arc_start = -180 # 0 degrees = 3 o'clock
# dst.show(
# tree_style=tree_style
# )
# Showing the tree.
visualize_dst(dst, show_tree_with_pyqt5)

# Generating the instructions for the PL/0 compiler.
generated_code.generate_instructions()

if generated_code.return_code() != "":
# Writing the generated code to a file.
with open("../output/generated_code.txt", mode="w") as txt:
txt.writelines("----------input code----------------\n")
txt.writelines(formatted_input_code)
txt.writelines("\n")
txt.writelines("----------generated code------------\n")
txt.writelines(generated_code.return_code())
txt.writelines("------------------------------------")
# Saving the generated code to a file.
save_generated_code(generated_code, formatted_input_code)

return generated_code.return_code()


if __name__ == '__main__':
main("../sample_input/not_tested/func.swift")
#main("../sample_input/program.swift")
# main("../sample_input/not_tested/func.swift")
main("../sample_input/program.swift", show_tree_with_pyqt5=True)
# main("../sample_input/not_tested/for_in_func.swift")

0 comments on commit ebe6bf1

Please sign in to comment.