Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Final release
Browse files Browse the repository at this point in the history
  • Loading branch information
GabDug committed Jan 11, 2018
1 parent bed1fac commit a75382b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
28 changes: 4 additions & 24 deletions src/GUI/tk_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,13 @@ def __init__(self, master=None):
self.menubar = tk.Menu(self)

menu = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Calculator App", menu=menu)
self.menubar.add_cascade(label="Project Calculator", menu=menu)
menu.add_command(label="About", command=self.about)
menu.add_command(label="Exit", command=self.exit)

menu = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Edit", menu=menu)
menu.add_command(label="Cut", command=self.cut)
menu.add_command(label="Copy", command=self.copy)
menu.add_command(label="Paste", command=self.paste)
menu.add_command(label="Clear ALL", command=self.clear)
self.menubar.add_cascade(label="Clear", menu=menu)
menu.add_command(label="Are you sure to clear?", command=self.clear)

self.master.config(menu=self.menubar)
self.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
Expand All @@ -281,29 +278,12 @@ def exit(self):
def clear(self):
self.tkcon.clear()

def cut(self):
messagebox.showwarning(
"Not implemented",
"The 'cut' method is not implemented yet"
)

def copy(self):
messagebox.showwarning(
"Not implemented",
"The 'copy' method is not implemented yet"
)

def paste(self):
messagebox.showwarning(
"Not implemented",
"The 'paste' method is not implemented yet"
)

def about(self):
messagebox.showinfo(
"About Project Calculator",
"This project was created by Gabriel Dugny and Thibault Lepez.\n"
"Tkinter module is created using pytkcon by samyzaf."
"For the GUI we used pytkcon by samyzaf (Open source library)."
)


Expand Down
52 changes: 28 additions & 24 deletions src/eval_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _clear():
system('cls' if name == 'nt' else 'clear')


def _single_element(element: tuple, variable_dic):
def _check_variable(element: tuple, variable_dic):
"""Evaluate a single token. Used when there is only one token."""
if element[1] == "variable":
if element[0] == "exit":
Expand All @@ -28,11 +28,7 @@ def _single_element(element: tuple, variable_dic):
return variable_dic[element[0]]
else:
raise Exception(f"unknown variable ('{element[0]}')")
elif element[1] == "integer":
return element
elif element[1] == "string":
return element
elif element[1] == "boolean":
else:
return element


Expand All @@ -49,7 +45,7 @@ def _eval_global(expression: list, var_dic: dict):
# If expression is single-operand (exit condition of recursivity)
if length == 1:
logger.debug(" Return:" + str(expression))
return _single_element(expression[0], var_dic)
return _check_variable(expression[0], var_dic)

# Expression Shape: left-expression main-operator right-expression
operator_index = find_operator(expression)
Expand All @@ -71,18 +67,22 @@ def _eval_global(expression: list, var_dic: dict):

# Processing the prefix unary operator first, right side of expression
if expression[operator_index][0] == "not":
logger.debug(" Processing not")
if left_expression is None or left_expression == []:
if _eval_global(right_expression, var_dic)[0] == "true":
return ("false", "boolean")
right = _eval_global(right_expression, var_dic)
if right[1] == "boolean":
if left_expression is None or left_expression == []:
if right[0] == "true":
return ("false", "boolean")
else:
return ("true", "boolean")
else:
return ("true", "boolean")
if right[0] == "true":
left_expression.append(("false", "boolean"))
else:
left_expression.append(("true", "boolean"))
return _eval_global(left_expression, var_dic)
else:
if _eval_global(right_expression, var_dic)[0] == "true":
left_expression.append(("false", "boolean"))
else:
left_expression.append(("true", "boolean"))
return _eval_global(left_expression, var_dic)
raise Exception(f"type mismatch ({main_operator[0]} {right[1]})")


# Binary infix operators
else:
Expand Down Expand Up @@ -193,14 +193,18 @@ def _first_eval(expression: list, variable_dic):
if size == 0:
raise Exception('Expression is empty.')
elif size == 1:
return _single_element(expression[0], variable_dic)
return _check_variable(expression[0], variable_dic)
else:
return _eval_global(expression, variable_dic)


def ext_eval_global(expression_str: str, variable_dic=None):
"""Evaluates an expression (boolean, integer or string), where the input is a string."""
return _first_eval(parse(expression_str), variable_dic)[0]
eval = _first_eval(parse(expression_str), variable_dic)
if eval is None:
return ""
else:
return eval[0]


def find_operator(expression):
Expand Down Expand Up @@ -228,14 +232,14 @@ def find_operator(expression):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(stdout)
ch.setLevel(logging.DEBUG)
ch.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s [%(levelname)s] : %(message)s")
formatter.datefmt = "%H:%M:%S"
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info("Starting logger from module.")

# dict_var = {}
# print(ext_eval_global("a = 10", dict_var))
# print(ext_eval_global("a + 1", dict_var))
print(ext_eval_global("'e'+1"))
dict_var = {}
print(ext_eval_global("a = 10", dict_var))
print(ext_eval_global("a + 1", dict_var))
print(ext_eval_global("true and false and not true or false", dict_var))
5 changes: 2 additions & 3 deletions src/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,5 @@ def remove_parenthesis(expression):
logger.addHandler(ch)
logger.info("Starting logger from module.")

logger.info(parse("1 === 1"))
# logger.info(parse("'Salut'+'toi'"))
# logger.info(parse("'Salut+'toi'"))
logger.info(parse("1 + 3 * 4 - 5"))
logger.info(parse("(1 + 3 * 5 - 6) + (4 / 7)"))

0 comments on commit a75382b

Please sign in to comment.