changed logic

another_parse_input
a.jurcenko 2025-09-17 21:29:14 +02:00
parent 70d02e0c1b
commit b5584bf316
2 changed files with 60 additions and 34 deletions

View File

@ -13,6 +13,9 @@ class AbacusCore:
self._vars['alpha'] = 1 self._vars['alpha'] = 1
self._vars['beta'] = 2 self._vars['beta'] = 2
self._operators = ['+', '-', '/', '*']
self._delimiters = ['(', ')', '[', ']', ';', ' ']
def get_vars(self): def get_vars(self):
return self._vars return self._vars
@ -40,41 +43,65 @@ class AbacusCore:
:param input_str: :param input_str:
:return: :return:
''' '''
if len(input_str) < 1:
return
if input_str.find(':')>-1:
chunks = input_str.split('#')
comment = None comment = None
orig_input_str = input_str
if len(input_str) < 1: # leere eingabe wird nicht akzeptiert
return
chunks = input_str.split('#')
if len(chunks) > 1: if len(chunks) > 1:
comment = chunks[1]
input_str = chunks[0] input_str = chunks[0]
inp_chunks = input_str.split(':') comment = ' '.join(chunks[1:])
if len(inp_chunks) == 2: chunks = input_str.split('=') # gibt es variablenzuweisung?
self._funcs[inp_chunks[0]] = inp_chunks[1] if len(chunks) > 2:
return inp_chunks[0], inp_chunks[1], 0, comment raise ValueError('Mehrfachzuweisung wird nicht unterstützt')
else: elif len(chunks) > 1:
return self._with_variable(input_str) 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) 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 = ''.join(input_str.split()) # entferne alle leerzeichen
input_str = input_str.replace(',', '.') input_str = input_str.replace(',', '.')
if input_str is not None: opdel = self._operators + self._delimiters
expression, new_var = self._is_new_variable(input_str) # chunks für variablenzuweisung chunks = list()
# print('expression', expression) last_index = 0
expression_wo_vars = self._replace_vars(expression) for i, v in enumerate(input_str):
# print('expression wo vars', expression_wo_vars) if v in opdel:
expression_calculated = self._calculate(expression_wo_vars) chunk = input_str[last_index:i]
# print('eval', expression_calculated) chunk = self._is_a_variable_or_func(chunk)
if new_var is not None: chunks.append(chunk)
self._vars[new_var] = expression_calculated chunks.append(input_str[i])
self._vars['result'] = expression_calculated last_index = i+1
return input_str, expression_wo_vars, expression_calculated, comment 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: else:
return 'comment', comment return chunk
def _replace_vars(self, input_str): def _replace_vars(self, input_str):
''' '''
@ -84,10 +111,8 @@ class AbacusCore:
''' '''
first_position = None first_position = None
new_input_str = '' new_input_str = ''
operators = ['+', '-', '/', '*'] op_del = self._operators + self._delimiters
delimiters = ['(', ')', '[', ']', ';', ' '] if input_str[0] in self._operators:
op_del = operators + delimiters
if input_str[0] in operators:
input_str = 'result' + input_str input_str = 'result' + input_str
input_str_len = len(input_str) input_str_len = len(input_str)
for i, c in enumerate(input_str): for i, c in enumerate(input_str):

View File

@ -48,7 +48,8 @@ class AbacusGUI:
input_wo_var = str(result[1]) input_wo_var = str(result[1])
ergebnis = str(result[2]) ergebnis = str(result[2])
comment = result[3] 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: if comment:
res_str += ' ' + comment res_str += ' ' + comment
item = QStandardItem(res_str) item = QStandardItem(res_str)