Mapytex/mapytex/calculus/str2tokens.py

278 lines
7.8 KiB
Python
Raw Normal View History

2014-11-08 15:43:04 +00:00
#!/usr/bin/env python
# encoding: utf-8
2016-03-06 15:18:01 +00:00
from .generic import Stack, isNumber, isPolynom
2016-01-07 16:34:23 +00:00
from .operator import op
2016-02-15 12:20:24 +00:00
from decimal import Decimal
2014-11-08 15:43:04 +00:00
2016-02-27 10:21:56 +00:00
import logging
2016-03-06 15:18:01 +00:00
# logging.basicConfig(filename='str2tokens_debug.log',level=logging.DEBUG)
2016-02-27 10:21:56 +00:00
2016-02-13 04:04:08 +00:00
2014-11-08 16:40:02 +00:00
def str2tokens(exp):
""" Parse the string into tokens then turn it into postfix form
2016-02-13 03:29:26 +00:00
2014-11-08 16:40:02 +00:00
>>> str2tokens('2+3*4')
2016-02-27 10:21:56 +00:00
[2, 3, 4, *, +]
2014-11-08 16:40:02 +00:00
>>> str2tokens('2*3+4')
2016-02-27 10:21:56 +00:00
[2, 3, *, 4, +]
2014-12-22 09:57:18 +00:00
>>> str2tokens('2x+4')
2016-03-07 11:29:08 +00:00
[2, < Polynom x [0, 1]>, *, 4, +]
2014-11-08 16:40:02 +00:00
"""
in_tokens = str2in_tokens(exp)
post_tokens = in2post_fix(in_tokens)
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
return post_tokens
2016-02-13 04:04:08 +00:00
2014-11-08 16:40:02 +00:00
def str2in_tokens(exp):
2014-11-08 15:43:04 +00:00
""" Parse the expression, ie tranform a string into a list of tokens
:param exp: The expression (a string)
:returns: list of token
2014-11-11 08:33:31 +00:00
>>> str2in_tokens('2+3*4')
[2, '+', 3, '*', 4]
>>> str2in_tokens('2*3+4')
[2, '*', 3, '+', 4]
>>> str2in_tokens('12*3+4')
[12, '*', 3, '+', 4]
>>> str2in_tokens('2.3*3+4')
[Decimal('2.3'), '*', 3, '+', 4]
>>> str2in_tokens('a*3+4')
2016-03-07 11:29:08 +00:00
[< Polynom a [0, 1]>, '*', 3, '+', 4]
2014-11-08 15:43:04 +00:00
"""
tokens = ['', '']
2014-11-08 15:43:04 +00:00
for character in exp:
if character.isdigit():
2016-03-06 15:18:01 +00:00
tokens += feed_digit(character, tokens.pop(), tokens[-1])
2014-11-08 15:43:04 +00:00
elif character == "(":
tokens += hidden_meaning_time(tokens[-1])
tokens.append("(")
elif character in op.available_op():
tokens.append(character)
elif character == ")":
2014-12-02 11:37:23 +00:00
tokens.append(character)
2014-11-08 15:43:04 +00:00
2014-12-22 09:57:18 +00:00
elif character.isalpha():
tokens += hidden_meaning_time(tokens[-1])
tokens.append(feed_alpha(character))
2014-12-22 09:57:18 +00:00
2014-11-08 15:43:04 +00:00
elif character == ".":
tokens.append(feed_dot(tokens.pop()))
2014-11-08 15:43:04 +00:00
elif character != " ":
raise ValueError("{} is an unvalid character".format(character))
return tokens[2:]
2016-03-06 15:18:01 +00:00
def feed_digit(character, tok_b, tok_bb):
""" Feed token when a digit is detected
:param character: the character
:param tok_b: the token before
:param tok_bb: the token before before
:returns: list of token to replace
>>> feed_digit(1, '-', 2)
['-', 1]
>>> feed_digit(1, '', '')
['', 1]
>>> feed_digit(1, 2, '')
[21]
>>> feed_digit(1, Decimal(2), '')
[Decimal('2.1')]
>>> feed_digit(1, Decimal('2.3'), '')
[Decimal('2.31')]
>>> feed_digit(1, -2, '')
[-21]
>>> feed_digit(1, Decimal('-2'), '')
[Decimal('-2.1')]
>>> feed_digit(1, '-', '')
[-1]
>>> feed_digit(1, '-', '+')
[-1]
>>> feed_digit(1, '-', '(')
[-1]
>>> feed_digit(1, '-', 2)
['-', 1]
2017-04-17 13:48:52 +00:00
>>> from mapytex.calculus.polynom import Polynom
>>> feed_digit(1, '-', Polynom([0,1]))
['-', 1]
"""
if isinstance(tok_b, int):
return [tok_b * 10 + int(tok_b/abs(tok_b)) * int(character)]
elif isinstance(tok_b, Decimal):
return [tok_b + int(tok_b/abs(tok_b)) * int(character) * Decimal('10') ** (tok_b.as_tuple().exponent - 1)]
# TODO: WTF!!! |sam. févr. 27 17:11:53 EAT 2016
elif tok_b == "-" and (str(tok_bb) in op.available_op() or str(tok_bb) == ""):
return [- int(character)]
else:
return [tok_b, int(character)]
2016-03-06 15:18:01 +00:00
def hidden_meaning_time(tok_b):
""" Return a "*" character if it is hidden meaning
:param tok_b: the token before
>>> hidden_meaning_time(4)
['*']
>>> hidden_meaning_time(')')
['*']
2017-04-17 13:48:52 +00:00
>>> from mapytex.calculus.polynom import Polynom
>>> hidden_meaning_time(Polynom([0,1]))
['*']
>>> hidden_meaning_time("+")
[]
>>> hidden_meaning_time("*")
[]
"""
if isNumber(tok_b) \
or tok_b == ")" \
or isPolynom(tok_b):
return ["*"]
return []
2016-03-06 15:18:01 +00:00
def feed_alpha(character):
""" Feed token when an alpha character is detected
:param character: the alpha character
:param tok_b: the token before
:returns: tokens to add
"""
2017-04-17 13:48:52 +00:00
from mapytex.calculus.polynom import Polynom
return Polynom([0, 1], letter=character)
2016-03-06 15:18:01 +00:00
def feed_dot(tok_b):
r""" Build Decimal with the previous token
:param tok_b: the previous token
:returns: the Decimal
>>> feed_dot(2)
Decimal('2')
>>> feed_dot(Decimal('2.3'))
Traceback (most recent call last):
...
ValueError: A number has 2 points...! We have 2.3 before the dot
>>> feed_dot('+')
Traceback (most recent call last):
...
ValueError: Try to make decimal but + before the dot
"""
if isinstance(tok_b, int):
return Decimal(tok_b)
elif isinstance(tok_b, Decimal):
raise ValueError("A number has 2 points...! We have {} before the dot".format(tok_b))
else:
raise ValueError("Try to make decimal but {} before the dot".format(tok_b))
2014-11-08 15:43:04 +00:00
2014-11-08 16:40:02 +00:00
def in2post_fix(infix_tokens):
2014-11-08 15:43:04 +00:00
""" From the infix_tokens list compute the corresponding postfix_tokens list
2016-02-13 03:29:26 +00:00
2014-11-08 15:43:04 +00:00
@param infix_tokens: the infix list of tokens to transform into postfix form.
@return: the corresponding postfix list of tokens.
2016-02-14 17:59:04 +00:00
>>> in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
2016-02-27 10:21:56 +00:00
[2, 5, 1, -, +, 3, 4, *, /]
2016-02-14 17:59:04 +00:00
>>> in2post_fix(['-', '(', '-', 2, ')'])
2016-02-27 10:21:56 +00:00
[2, -, -]
2016-02-14 17:59:04 +00:00
>>> in2post_fix(['-', '(', '-', 2, '+', 3, '*', 4, ')'])
2016-02-27 10:21:56 +00:00
[2, -, 3, 4, *, +, -]
2014-11-08 15:43:04 +00:00
"""
2016-02-27 10:21:56 +00:00
logging.debug("New start with {}".format(infix_tokens))
2014-11-08 16:40:02 +00:00
# Stack where operator will be stocked
2014-11-08 15:43:04 +00:00
opStack = Stack()
2014-11-08 16:40:02 +00:00
# final postfix list of tokens
postfix_tokens = []
# stack with the nbr of tokens still to compute in postfix_tokens
arity_Stack = Stack()
arity_Stack.push(0)
2016-02-13 04:04:08 +00:00
for (pos_token, token) in enumerate(infix_tokens):
2014-11-08 15:43:04 +00:00
2016-03-06 15:18:01 +00:00
logging.debug(str(postfix_tokens) +
" | " + str(opStack) +
" | " + str(infix_tokens[(pos_token+1):]) +
" | " + str(arity_Stack)
)
2014-11-11 08:33:31 +00:00
if token == ")":
2014-12-02 11:37:23 +00:00
next_op = opStack.pop()
2016-02-27 10:21:56 +00:00
while next_op != op.par:
2014-12-02 11:37:23 +00:00
postfix_tokens.append(next_op)
next_op = opStack.pop()
2014-11-08 16:40:02 +00:00
2016-02-13 03:29:26 +00:00
# Go back to old arity
2014-11-08 16:40:02 +00:00
arity_Stack.pop()
# Raise the arity
arity = arity_Stack.pop()
arity_Stack.push(arity + 1)
2014-12-02 11:37:23 +00:00
elif op.can_be_operator(token):
2014-11-11 08:33:31 +00:00
if token == "(":
2016-03-06 15:18:01 +00:00
opStack.push(op.get_op(token, 0))
2014-11-11 08:33:31 +00:00
# Set next arity counter
arity_Stack.push(0)
else:
2016-02-13 03:29:26 +00:00
arity = arity_Stack.pop()
2014-12-02 11:37:23 +00:00
token_op = op.get_op(token, arity + 1)
2016-02-13 04:04:08 +00:00
# Reset arity to 0 in case there is other operators (the real
# operation would be "-op.arity + 1")
2014-11-11 08:33:31 +00:00
arity_Stack.push(0)
2016-02-13 04:04:08 +00:00
while (not opStack.isEmpty()) and opStack.peek(
).priority >= token_op.priority:
2014-12-02 11:37:23 +00:00
next_op = opStack.pop()
postfix_tokens.append(next_op)
opStack.push(token_op)
2016-03-06 15:18:01 +00:00
logging.debug("--" + token + " -> " + str(arity + 1))
2014-11-08 15:43:04 +00:00
else:
2014-11-08 16:40:02 +00:00
postfix_tokens.append(token)
arity = arity_Stack.pop()
arity_Stack.push(arity + 1)
2014-11-08 15:43:04 +00:00
2016-03-06 15:18:01 +00:00
logging.debug(str(postfix_tokens) +
" | " + str(opStack) +
" | " + str(infix_tokens[(pos_token+1):]) +
" | " + str(arity_Stack)
)
2014-11-11 08:33:31 +00:00
2014-11-08 15:43:04 +00:00
while not opStack.isEmpty():
2014-12-02 11:37:23 +00:00
next_op = opStack.pop()
postfix_tokens.append(next_op)
2014-11-08 16:40:02 +00:00
2016-03-06 15:18:01 +00:00
logging.debug(str(postfix_tokens) +
" | " + str(opStack) +
" | " + str(infix_tokens[(pos_token+1):]) +
" | " + str(arity_Stack)
)
2014-11-08 16:40:02 +00:00
if arity_Stack.peek() != 1:
2016-02-13 04:04:08 +00:00
raise ValueError(
"Unvalid expression. The arity Stack is ",
str(arity_Stack))
2014-11-08 15:43:04 +00:00
2016-02-27 10:21:56 +00:00
logging.debug("Fini!")
2014-11-08 16:40:02 +00:00
return postfix_tokens
2014-11-08 15:43:04 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2016-02-13 03:29:26 +00:00
# cursor: 16 del