Skip to content

Commit

Permalink
Merge pull request #16 from via8/15-update-include
Browse files Browse the repository at this point in the history
15 update include
  • Loading branch information
via8 authored Mar 7, 2021
2 parents baf883e + f978de0 commit fd2a537
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 68 deletions.
2 changes: 2 additions & 0 deletions examples/test_include/anarcho.ptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
stuff = 'Anarcho'
print stuff
16 changes: 0 additions & 16 deletions examples/test_include/body.rpy

This file was deleted.

7 changes: 0 additions & 7 deletions examples/test_include/header.rpy

This file was deleted.

3 changes: 3 additions & 0 deletions examples/test_include/taoism.ptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include 'anarcho.ptx'
stuff = 'Taoism'
print stuff
4 changes: 2 additions & 2 deletions examples/test_include/test.ptx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Testing 'include' macro

include 'header.rpy'
include 'body.rpy'
include 'anarcho.ptx'
include 'taoism.ptx'
2 changes: 1 addition & 1 deletion pytux/build/lexa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
38 changes: 19 additions & 19 deletions pytux/build/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'] == '+':
Expand Down
32 changes: 30 additions & 2 deletions pytux/build/semen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
42 changes: 21 additions & 21 deletions pytux/build/varya.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Varya = VarSystem()

0 comments on commit fd2a537

Please sign in to comment.