From a75382b10c825ac08606f7b6b20b295af7647280 Mon Sep 17 00:00:00 2001 From: Gabriel Dugny Date: Thu, 11 Jan 2018 16:55:38 +0100 Subject: [PATCH] Final release --- src/GUI/tk_calculator.py | 28 ++++------------------ src/eval_global.py | 52 +++++++++++++++++++++------------------- src/parse.py | 5 ++-- 3 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/GUI/tk_calculator.py b/src/GUI/tk_calculator.py index 02d9186..4bd0918 100644 --- a/src/GUI/tk_calculator.py +++ b/src/GUI/tk_calculator.py @@ -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) @@ -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)." ) diff --git a/src/eval_global.py b/src/eval_global.py index 193f903..ea66ca7 100644 --- a/src/eval_global.py +++ b/src/eval_global.py @@ -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": @@ -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 @@ -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) @@ -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: @@ -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): @@ -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)) diff --git a/src/parse.py b/src/parse.py index 984ff7c..2e54e72 100644 --- a/src/parse.py +++ b/src/parse.py @@ -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)"))