added another_parse_input
parent
9574c10600
commit
0a42a8e272
129
abacus_core.py
129
abacus_core.py
|
|
@ -14,6 +14,14 @@ class AbacusCore:
|
||||||
|
|
||||||
self._funcs['r2'] = lambda x: round(x, 2)
|
self._funcs['r2'] = lambda x: round(x, 2)
|
||||||
self._funcs['r0'] = lambda x: int(x)
|
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):
|
def _calculate(self, input_str):
|
||||||
|
|
@ -23,6 +31,8 @@ class AbacusCore:
|
||||||
res = 'Division by Zero'
|
res = 'Division by Zero'
|
||||||
except NameError as e:
|
except NameError as e:
|
||||||
res = 'Variable '+ e.name + ' not exists'
|
res = 'Variable '+ e.name + ' not exists'
|
||||||
|
except SyntaxError as e:
|
||||||
|
res = 'Syntax Error'
|
||||||
return False, res
|
return False, res
|
||||||
|
|
||||||
def get_vars(self):
|
def get_vars(self):
|
||||||
|
|
@ -32,67 +42,70 @@ class AbacusCore:
|
||||||
self._vars[name] = value
|
self._vars[name] = value
|
||||||
|
|
||||||
def parse_input(self, input_str):
|
def parse_input(self, input_str):
|
||||||
comment = None
|
'''
|
||||||
input_wo_com = None
|
versuche input_str zu parsen in comment vars und andere teile
|
||||||
success = False
|
:param input_str:
|
||||||
input_str = str(input_str).strip()
|
:return:
|
||||||
input_str = input_str.replace(',', '.')
|
'''
|
||||||
if input_str[0] == '#':
|
input_str, comment = self._get_input_wo_commentar(input_str)
|
||||||
comment = input_str[1:]
|
input_str = ''.join(input_str.split()) # entferne alle leerzeichen
|
||||||
com_index = input_str.find('#')
|
if input_str is not None:
|
||||||
if com_index > -1:
|
for chunk in self._is_new_variable(input_str): # chunks für variablenzuweisung
|
||||||
input_wo_com = input_str[:com_index]
|
self._replace_vars(chunk)
|
||||||
comment = input_str[com_index + 1:]
|
|
||||||
if input_wo_com is None:
|
|
||||||
input_wo_com = input_str
|
|
||||||
print('comment', comment)
|
|
||||||
result = self._split_input(input_wo_com)
|
|
||||||
print('result:',result)
|
|
||||||
if result[1] is not None:
|
|
||||||
success, res = self._calculate(result[1])
|
|
||||||
if success:
|
|
||||||
operators = result[0]
|
|
||||||
operators.reverse()
|
|
||||||
for op in operators:
|
|
||||||
if op[-1] == '=':
|
|
||||||
self._vars[op[:-1]] = res
|
|
||||||
elif op[-1] == ':':
|
|
||||||
res = self._funcs.get(op[:-1])(res)
|
|
||||||
print('vars:', self._vars)
|
|
||||||
else:
|
else:
|
||||||
print("Error")
|
return ('comment', comment)
|
||||||
|
|
||||||
|
|
||||||
def _split_input(self, input_str):
|
def _replace_vars(self, input_str):
|
||||||
last_position = 0
|
'''
|
||||||
varchunks = list()
|
try to find vars and replace them
|
||||||
|
:param input_str:
|
||||||
|
:return:
|
||||||
|
'''
|
||||||
|
first_position = None
|
||||||
|
new_input_str = ''
|
||||||
for i, c in enumerate(input_str):
|
for i, c in enumerate(input_str):
|
||||||
if c in [':', '=']:
|
if c.isalpha():
|
||||||
chunk = input_str[last_position:i].strip()
|
if first_position is None:
|
||||||
chunk = chunk + input_str[i]
|
first_position = i
|
||||||
varchunks.append(chunk)
|
# print('alpha'+c)
|
||||||
last_position = i+1
|
elif c == '=':
|
||||||
rest = input_str[last_position:].strip()
|
if first_position is not None: # variablenzuweisung
|
||||||
print('rest', rest)
|
self._vars[input_str[first_position:i]] = None # todo: wie machen?
|
||||||
rest = self._find_vars(rest)[1]
|
else: # keine variable angegeben todo: womöglich eine tempvariable?
|
||||||
return varchunks, rest
|
raise ValueError()
|
||||||
|
elif c.isnumeric() or c in ['+', '-', '/', '*', '(', ')', '[', ']', ';']:
|
||||||
|
if first_position is not None:
|
||||||
|
temp_var = input_str[first_position:i]
|
||||||
|
print('var', temp_var)
|
||||||
|
var = self._vars.get(temp_var)
|
||||||
|
if var is not None:
|
||||||
|
new_input_str +=str(var)
|
||||||
|
else:
|
||||||
|
raise ValueError()
|
||||||
|
first_position = None
|
||||||
|
new_input_str += input_str[i]
|
||||||
|
print(new_input_str)
|
||||||
|
|
||||||
def _find_vars(self, input_str):
|
def _get_input_wo_commentar(self, input_str):
|
||||||
chunks = list()
|
'''
|
||||||
last_position = 0
|
Zerlege input in input und kommentar
|
||||||
for i, c in enumerate(input_str):
|
:param input_str:
|
||||||
if c in ['+', '-', '/', '*', '(', ')', '[', ']', ';']:
|
:return:
|
||||||
ch = input_str[last_position:i].strip()
|
'''
|
||||||
if len(ch)>0:
|
input_str = input_str.strip()
|
||||||
chunks.append(ch)
|
comment_index = input_str.find('#')
|
||||||
chunks.append(input_str[i].strip())
|
if comment_index == 0: # die ganze zeile ist ein kommentar
|
||||||
last_position = i+1
|
return (None, input_str)
|
||||||
|
elif comment_index == -1:
|
||||||
|
return (input_str, None)
|
||||||
|
else:
|
||||||
|
comment = input_str[comment_index + 1:]
|
||||||
|
input_str = input_str[:comment_index]
|
||||||
|
return (input_str, comment)
|
||||||
|
|
||||||
if last_position<len(input_str):
|
|
||||||
chunks.append(input_str[last_position:].strip())
|
def _is_new_variable(self, input_str):
|
||||||
cp_chunks = chunks.copy()
|
chunks = input_str.split('=')
|
||||||
for i, chunk in enumerate(chunks):
|
chunks.reverse()
|
||||||
if chunk in self._vars:
|
return chunks
|
||||||
cp_chunks[i] = str(self._vars.get(chunk))
|
|
||||||
print(chunks, cp_chunks)
|
|
||||||
return len(cp_chunks)>1, ''.join(cp_chunks)
|
|
||||||
|
|
|
||||||
2
main.py
2
main.py
|
|
@ -4,4 +4,4 @@ ab = AbacusCore()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ab.parse_input('12')
|
print(ab.parse_input('alpha+ 25+15+beta+2#irgendwieso'))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue