it works
parent
0a42a8e272
commit
7f30a44f6d
104
abacus_core.py
104
abacus_core.py
|
|
@ -8,39 +8,29 @@ class AbacusCore:
|
|||
self._vars = dict()
|
||||
self._funcs = dict()
|
||||
|
||||
self._vars['result'] = 0
|
||||
self._vars['alphabet'] = 28
|
||||
self._vars['alpha'] = 1
|
||||
self._vars['beta'] = 2
|
||||
|
||||
self._funcs['r2'] = lambda x: round(x, 2)
|
||||
self._funcs['r0'] = lambda x: int(x)
|
||||
self._funcs['summe'] = self.summe
|
||||
|
||||
|
||||
def summe(self, l):
|
||||
s = 0
|
||||
for ss in l:
|
||||
s = s + ss
|
||||
return s
|
||||
|
||||
|
||||
def _calculate(self, input_str):
|
||||
try:
|
||||
return True, eval(input_str)
|
||||
except ZeroDivisionError:
|
||||
res = 'Division by Zero'
|
||||
except NameError as e:
|
||||
res = 'Variable '+ e.name + ' not exists'
|
||||
except SyntaxError as e:
|
||||
res = 'Syntax Error'
|
||||
return False, res
|
||||
|
||||
def get_vars(self):
|
||||
return self._vars
|
||||
|
||||
def add_var(self, name, value):
|
||||
self._vars[name] = value
|
||||
|
||||
def _calculate(self, input_str):
|
||||
print('input:', input_str)
|
||||
try:
|
||||
return eval(input_str)
|
||||
except ZeroDivisionError:
|
||||
res = 'Division by Zero'
|
||||
except NameError as e:
|
||||
res = 'Variable '+ e.name + ' not exists'
|
||||
except SyntaxError as e:
|
||||
res = 'Syntax Error'
|
||||
return res
|
||||
|
||||
def parse_input(self, input_str):
|
||||
'''
|
||||
versuche input_str zu parsen in comment vars und andere teile
|
||||
|
|
@ -49,12 +39,20 @@ class AbacusCore:
|
|||
'''
|
||||
input_str, comment = self._get_input_wo_commentar(input_str)
|
||||
input_str = ''.join(input_str.split()) # entferne alle leerzeichen
|
||||
input_str = input_str.replace(',', '.')
|
||||
if input_str is not None:
|
||||
for chunk in self._is_new_variable(input_str): # chunks für variablenzuweisung
|
||||
self._replace_vars(chunk)
|
||||
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
|
||||
else:
|
||||
return ('comment', comment)
|
||||
|
||||
return 'comment', comment
|
||||
|
||||
def _replace_vars(self, input_str):
|
||||
'''
|
||||
|
|
@ -64,28 +62,35 @@ class AbacusCore:
|
|||
'''
|
||||
first_position = None
|
||||
new_input_str = ''
|
||||
operators = ['+', '-', '/', '*']
|
||||
delimiters = ['(', ')', '[', ']', ';']
|
||||
op_del = operators + delimiters
|
||||
if input_str[0] in operators:
|
||||
input_str = 'result' + input_str
|
||||
input_str_len = len(input_str)
|
||||
for i, c in enumerate(input_str):
|
||||
if c.isalpha():
|
||||
if first_position is None:
|
||||
first_position = i
|
||||
# print('alpha'+c)
|
||||
elif c == '=':
|
||||
if first_position is not None: # variablenzuweisung
|
||||
self._vars[input_str[first_position:i]] = None # todo: wie machen?
|
||||
else: # keine variable angegeben todo: womöglich eine tempvariable?
|
||||
raise ValueError()
|
||||
elif c.isnumeric() or c in ['+', '-', '/', '*', '(', ')', '[', ']', ';']:
|
||||
if c in op_del or i == input_str_len-1: # and first_position is not None:
|
||||
if first_position is not None:
|
||||
temp_var = input_str[first_position:i]
|
||||
last_position = i
|
||||
if i == input_str_len-1:
|
||||
last_position += 1
|
||||
temp_var = input_str[first_position:last_position]
|
||||
print('var', temp_var)
|
||||
var = self._vars.get(temp_var)
|
||||
if var is not None:
|
||||
new_input_str +=str(var)
|
||||
new_input_str += str(var)
|
||||
else:
|
||||
raise ValueError()
|
||||
first_position = None
|
||||
if i == input_str_len - 1:
|
||||
continue
|
||||
new_input_str += input_str[i]
|
||||
print(new_input_str)
|
||||
elif c.isalpha():
|
||||
if first_position is None:
|
||||
first_position = i
|
||||
else:
|
||||
new_input_str += input_str[i]
|
||||
return new_input_str
|
||||
|
||||
def _get_input_wo_commentar(self, input_str):
|
||||
'''
|
||||
|
|
@ -96,16 +101,25 @@ class AbacusCore:
|
|||
input_str = input_str.strip()
|
||||
comment_index = input_str.find('#')
|
||||
if comment_index == 0: # die ganze zeile ist ein kommentar
|
||||
return (None, input_str)
|
||||
return None, input_str
|
||||
elif comment_index == -1:
|
||||
return (input_str, None)
|
||||
return input_str, None
|
||||
else:
|
||||
comment = input_str[comment_index + 1:]
|
||||
input_str = input_str[:comment_index]
|
||||
return (input_str, comment)
|
||||
return input_str, comment
|
||||
|
||||
|
||||
def _is_new_variable(self, input_str):
|
||||
'''
|
||||
|
||||
'''
|
||||
chunks = input_str.split('=')
|
||||
chunks.reverse()
|
||||
return chunks
|
||||
chunks_len = len(chunks)
|
||||
if chunks_len > 2:
|
||||
raise ValueError()
|
||||
# chunks.reverse()
|
||||
elif chunks_len == 2:
|
||||
return chunks[1], chunks[0]
|
||||
else:
|
||||
return chunks[0], None
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
from abacus_core import AbacusCore
|
||||
|
||||
ab = AbacusCore()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
step = 0
|
||||
res = 0
|
||||
while True:
|
||||
var = input('gib was ein:')
|
||||
if var == 'vars':
|
||||
print(ab.get_vars())
|
||||
elif var == 'res':
|
||||
print(res)
|
||||
else:
|
||||
step += 1
|
||||
res = ab.parse_input(str(var))
|
||||
print('step', step, 'result', res)
|
||||
Loading…
Reference in New Issue