diff --git a/abacus_core.py b/abacus_core.py index 47a3429..f108d3f 100644 --- a/abacus_core.py +++ b/abacus_core.py @@ -13,6 +13,9 @@ class AbacusCore: self._vars['alpha'] = 1 self._vars['beta'] = 2 + self._operators = ['+', '-', '/', '*'] + self._delimiters = ['(', ')', '[', ']', ';', ' '] + def get_vars(self): return self._vars @@ -40,41 +43,65 @@ class AbacusCore: :param input_str: :return: ''' - if len(input_str) < 1: + comment = None + orig_input_str = input_str + if len(input_str) < 1: # leere eingabe wird nicht akzeptiert return - if input_str.find(':')>-1: - chunks = input_str.split('#') - comment = None - if len(chunks) > 1: - comment = chunks[1] - input_str = chunks[0] - inp_chunks = input_str.split(':') - if len(inp_chunks) == 2: - self._funcs[inp_chunks[0]] = inp_chunks[1] - return inp_chunks[0], inp_chunks[1], 0, comment - else: - return self._with_variable(input_str) + chunks = input_str.split('#') + if len(chunks) > 1: + input_str = chunks[0] + comment = ' '.join(chunks[1:]) + chunks = input_str.split('=') # gibt es variablenzuweisung? + if len(chunks) > 2: + raise ValueError('Mehrfachzuweisung wird nicht unterstützt') + elif len(chunks) > 1: + expression = self._with_variable_another_way(chunks[1]) + self._vars[chunks[0]] = expression + return + chunks = input_str.split(':') # gibt es funktionsdefinition? + if len(chunks) > 1: + self._funcs[chunks[0]] = chunks[1] + return + res = self._with_variable_another_way(input_str) + self._vars['result'] = res + return orig_input_str, None, res, comment - def _with_variable(self, input_str): + + + + def _with_variable_another_way(self, input_str): input_str, comment = self._get_input_wo_commentar(input_str) - func = self._funcs.get(input_str) - if func: - input_str = func input_str = ''.join(input_str.split()) # entferne alle leerzeichen input_str = input_str.replace(',', '.') - if input_str is not None: - expression, new_var = self._is_new_variable(input_str) # chunks für variablenzuweisung - # print('expression', expression) - expression_wo_vars = self._replace_vars(expression) - # print('expression wo vars', expression_wo_vars) - expression_calculated = self._calculate(expression_wo_vars) - # print('eval', expression_calculated) - if new_var is not None: - self._vars[new_var] = expression_calculated - self._vars['result'] = expression_calculated - return input_str, expression_wo_vars, expression_calculated, comment + opdel = self._operators + self._delimiters + chunks = list() + last_index = 0 + for i, v in enumerate(input_str): + if v in opdel: + chunk = input_str[last_index:i] + chunk = self._is_a_variable_or_func(chunk) + chunks.append(chunk) + chunks.append(input_str[i]) + last_index = i+1 + elif i == len(input_str)-1: + chunk = input_str[last_index:i+1] + chunk = self._is_a_variable_or_func(chunk) + chunks.append(chunk) + return eval(''.join(chunks)) + + def _is_a_variable_or_func(self, chunk): + var = self._vars.get(chunk) + if not var: + var = self._funcs.get(chunk) + if var: + var = self._with_variable_another_way(var) + if var: + return str(var) else: - return 'comment', comment + return chunk + + + def _replace_vars(self, input_str): ''' @@ -84,10 +111,8 @@ class AbacusCore: ''' first_position = None new_input_str = '' - operators = ['+', '-', '/', '*'] - delimiters = ['(', ')', '[', ']', ';', ' '] - op_del = operators + delimiters - if input_str[0] in operators: + op_del = self._operators + self._delimiters + if input_str[0] in self._operators: input_str = 'result' + input_str input_str_len = len(input_str) for i, c in enumerate(input_str): diff --git a/main.py b/main.py index afb1efd..14b34de 100644 --- a/main.py +++ b/main.py @@ -48,7 +48,8 @@ class AbacusGUI: input_wo_var = str(result[1]) ergebnis = str(result[2]) comment = result[3] - res_str = str(self._step) + '. ' + input_str + ' => ' + input_wo_var + ' = ' + ergebnis + res_str = str(self._step) + '. ' + input_str + ' = ' + ergebnis + # res_str = str(self._step) + '. ' + input_str + ' => ' + input_wo_var + ' = ' + ergebnis if comment: res_str += ' ' + comment item = QStandardItem(res_str)