diff --git a/examples/test_include/anarcho.ptx b/examples/test_include/anarcho.ptx new file mode 100644 index 0000000..fd72efb --- /dev/null +++ b/examples/test_include/anarcho.ptx @@ -0,0 +1,2 @@ +stuff = 'Anarcho' +print stuff diff --git a/examples/test_include/body.rpy b/examples/test_include/body.rpy deleted file mode 100644 index 553fa3e..0000000 --- a/examples/test_include/body.rpy +++ /dev/null @@ -1,16 +0,0 @@ -# The game starts here. -label start: - - # Start by playing some music. - play music "illurock.opus" - - scene bg lecturehall - with fade - - "It's only when I hear the sounds of shuffling feet and supplies being put away that I realize that the lecture's over." - - "Professor Eileen's lectures are usually interesting, but today I just couldn't concentrate on it." - - "I've had a lot of other thoughts on my mind...thoughts that culminate in a question." - - "It's a question that I've been meaning to ask a certain someone." \ No newline at end of file diff --git a/examples/test_include/header.rpy b/examples/test_include/header.rpy deleted file mode 100644 index 8b03abc..0000000 --- a/examples/test_include/header.rpy +++ /dev/null @@ -1,7 +0,0 @@ -# Declare characters used by this game. -define s = Character(_("Sylvie"), color="#c8ffc8") -define m = Character(_("Me"), color="#c8c8ff") - -# This is a variable that is True if you've compared a VN to a book, and False -# otherwise. -default book = False \ No newline at end of file diff --git a/examples/test_include/taoism.ptx b/examples/test_include/taoism.ptx new file mode 100644 index 0000000..08b6ede --- /dev/null +++ b/examples/test_include/taoism.ptx @@ -0,0 +1,3 @@ +include 'anarcho.ptx' +stuff = 'Taoism' +print stuff diff --git a/examples/test_include/test.ptx b/examples/test_include/test.ptx index 728ecc2..97b94bb 100644 --- a/examples/test_include/test.ptx +++ b/examples/test_include/test.ptx @@ -1,4 +1,4 @@ # Testing 'include' macro -include 'header.rpy' -include 'body.rpy' +include 'anarcho.ptx' +include 'taoism.ptx' diff --git a/pytux/build/lexa.py b/pytux/build/lexa.py index 9fef050..69551d0 100644 --- a/pytux/build/lexa.py +++ b/pytux/build/lexa.py @@ -44,7 +44,7 @@ def t_STRING(t): def t_NEWLINE(t): r'\n+' t.lexer.lineno += len(t.value) - t.value = '\n' * (len(t.value) + 1) + t.value = '\n' return t diff --git a/pytux/build/quiz.py b/pytux/build/quiz.py index 615efe1..8f0dd28 100644 --- a/pytux/build/quiz.py +++ b/pytux/build/quiz.py @@ -22,22 +22,22 @@ def __init__(self, question, lineno): self.__lineno = lineno Quiz.__counter += 1 - """ - Add answer to quiz. - - :param marker: '+' if answer is correct, '-' if answer is incorrect - :param text: text of the answer - """ def add_answer(self, marker, text): + """ + Add answer to quiz. + + :param marker: '+' if answer is correct, '-' if answer is incorrect + :param text: text of the answer + """ self.__answers.append({'marker': marker, 'text': text}) - """ - Quiz validation function: when there is more than one correct answer or no correct answers, or - there is no incorrect answers, than some error will be raised. - - :return: None. - """ def __validate_quiz(self): + """ + Quiz validation function: when there is more than one correct answer or no correct answers, or + there is no incorrect answers, than some error will be raised. + + :return: None. + """ n_correct = 0 n_incorrect = 0 for answer in self.__answers: @@ -51,19 +51,19 @@ def __validate_quiz(self): if n_incorrect == 0: raise QuizError(f"There must be at least one answer marked with - in quiz on line {self.__lineno}") - """ - Quiz generation. - - :return: Ren'Py code for quiz. - """ def generate_quiz(self): + """ + Quiz generation. + + :return: Ren'Py code for quiz. + """ # Checking answers and shuffling them randomly self.__validate_quiz() shuffle(self.__answers) result = '' - result += f"menu quiz_{self.__counter}:\n" # Ren'Py menu statement - result += f"{Quiz.TAB}\"{self.__question}\"\n\n" # question is placed right after + result += f"menu quiz_{self.__counter}:\n" # Ren'Py menu statement + result += f"{Quiz.TAB}\"{self.__question}\"\n\n" # question is placed right after for answer in self.__answers: result += f"{Quiz.TAB}\"{answer['text']}\":\n" if answer['marker'] == '+': diff --git a/pytux/build/semen.py b/pytux/build/semen.py index 34e4081..197dfea 100644 --- a/pytux/build/semen.py +++ b/pytux/build/semen.py @@ -11,6 +11,23 @@ def __str__(self): parsed_file_dir = '' +dependencies_stack = [] + + +def process_dependencies(file_path): + """ + Process file dependencies. + + :param file_path: file relative path + :return: None. + """ + if file_path in dependencies_stack: + dependencies = "" + for name in dependencies_stack: + dependencies += f"{name} -> " + dependencies += file_path + raise SemenError(f"Recursive include: {dependencies}") + dependencies_stack.append(file_path) # Program @@ -27,7 +44,11 @@ def p_program(p): def p_include_string(p): 'sentence : INCLUDE STRING NEWLINE' with open(path.join(parsed_file_dir, p[2]), "r") as include_file: - p[0] = include_file.read() + p[3] + data = include_file.read() + process_dependencies(p[2]) + result = Semen.parse(data, lexer=Lexa.clone()) + dependencies_stack.pop() + p[0] = result # Variable initialization or assignment sentence @@ -40,7 +61,7 @@ def p_assign_string_variable(p): # Print variable sentence def p_print(p): 'sentence : PRINT VARNAME NEWLINE' - p[0] = Varya.get_value(p[2]) + '\n' + p[0] = Varya.get_value(p[2]) + p[3] # Quiz sentence @@ -113,7 +134,14 @@ def p_error(p): def parse(file): + """ + Parses file, collects some important metainformation. + + :param file: source file (S) + :return: result (R) of parsing. + """ global parsed_file_dir parsed_file_dir = path.dirname(path.abspath(file.name)) + dependencies_stack.append(file.name) data = file.read() return Semen.parse(data) diff --git a/pytux/build/varya.py b/pytux/build/varya.py index 59cdff1..61dc5a2 100644 --- a/pytux/build/varya.py +++ b/pytux/build/varya.py @@ -12,44 +12,44 @@ class VarSystem: __variables = {} - """ - Initializes variable or assigns value to existing variable. - - :param name: variable identifier - :param value: variable value - :param type: variable type - :return: None. - """ def init_or_assign(self, name: str, value, type: VarType): + """ + Initializes variable or assigns value to existing variable. + + :param name: variable identifier + :param value: variable value + :param type: variable type + :return: None. + """ if name in self.__variables: var_type = self.__variables[name][1] if var_type != type: raise ValueError(f"trying to assign value of type {type} to the variable {name} of type {var_type}") self.__variables[name] = (value, type) - """ - Gets variable value if variable with such identifier exists. - - :param name: variable identifier - :return: variable value. - """ def get_value(self, name: str): + """ + Gets variable value if variable with such identifier exists. + + :param name: variable identifier + :return: variable value. + """ if name in self.__variables: return self.__variables[name][0] else: return None - """ - Gets variable type if variable with such identifier exists. - - :param name: variable identifier - :return: variable type. - """ def get_type(self, name: str): + """ + Gets variable type if variable with such identifier exists. + + :param name: variable identifier + :return: variable type. + """ if name in self.__variables: return self.__variables[name][1] else: return None -Varya = VarSystem() \ No newline at end of file +Varya = VarSystem()