Skip to content

Instantly share code, notes, and snippets.

@jettify
Forked from ascv/calc.py
Created May 7, 2014 21:07
Show Gist options
  • Select an option

  • Save jettify/ae805d8e732be07b7b0e to your computer and use it in GitHub Desktop.

Select an option

Save jettify/ae805d8e732be07b7b0e to your computer and use it in GitHub Desktop.

Revisions

  1. @ascv ascv revised this gist Feb 21, 2014. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -37,12 +37,13 @@ def next(self):

    def term(self):
    result = self.factor()
    while self._current == '*':
    self.next()
    result *= self.term()
    while self._current == '/':
    self.next()
    result /= self.term()
    while self._current in ('*', '/'):
    if self._current == '*':
    self.next()
    result *= self.term()
    if self._current == '/':
    self.next()
    result /= self.term()
    return result

    if __name__ == '__main__':
  2. @ascv ascv revised this gist Feb 21, 2014. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,11 @@ def __init__(self, tokens):
    def exp(self):
    result = self.term()
    while self._current in ('+', '-'):
    self.next()
    if result is '+':
    if self._current == '+':
    self.next()
    result += self.term()
    if result is '-':
    if self._current == '-':
    self.next()
    result -= self.term()
    return result

    @@ -36,10 +37,10 @@ def next(self):

    def term(self):
    result = self.factor()
    while self._current is '*':
    while self._current == '*':
    self.next()
    result *= self.term()
    while self._current is '/':
    while self._current == '/':
    self.next()
    result /= self.term()
    return result
  3. @ascv ascv revised this gist Feb 21, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,12 @@ def __init__(self, tokens):

    def exp(self):
    result = self.term()
    while self._current is '+':
    while self._current in ('+', '-'):
    self.next()
    result += self.term()
    while self._current is '-':
    self.next()
    result -= self.term()
    if result is '+':
    result += self.term()
    if result is '-':
    result -= self.term()
    return result

    def factor(self):
    @@ -55,4 +55,4 @@ def term(self):
    tokens[-1] += lst[i]
    else:
    tokens.append(lst[i])
    print Calculator(tokens).exp()
    print Calculator(tokens).exp()
  4. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor * term | factor / term | factor
    term ::= factor | factor * term | factor / term
    factor ::= number | ( exp )
    """

    @@ -35,13 +35,13 @@ def next(self):
    self._current = self._tokens[0] if len(self._tokens) > 0 else None

    def term(self):
    result = self.factor()
    while self._current is '*':
    self.next()
    result *= self.term()
    while self._current is '/':
    self.next()
    result = result / self.term()
    result = self.factor()
    result /= self.term()
    return result

    if __name__ == '__main__':
  5. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor | factor * term | factor / term
    term ::= factor * term | factor / term | factor
    factor ::= number | ( exp )
    """

    @@ -35,21 +35,21 @@ def next(self):
    self._current = self._tokens[0] if len(self._tokens) > 0 else None

    def term(self):
    result = self.factor()
    while self._current is '*':
    self.next()
    result *= self.term()
    while self._current is '/':
    self.next()
    result /= self.term()
    result = result / self.term()
    result = self.factor()
    return result

    if __name__ == '__main__':
    while True:
    lst = list(raw_input('> ').replace(' ', ''))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    tokens[-1] += lst[i]
    elif lst[i] is '.' and i > 0 and tokens[-1].isdigit():
    tokens[-1] += lst[i]
  6. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion calc.py
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ def term(self):
    lst = list(raw_input('> ').replace(' ', ''))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    tokens[-1] += lst[i]
    elif lst[i] is '.' and i > 0 and tokens[-1].isdigit():
    tokens[-1] += lst[i]
  7. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -49,10 +49,10 @@ def term(self):
    lst = list(raw_input('> ').replace(' ', ''))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and len(tokens) > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    if lst[i].isdigit() and i > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    tokens[-1] += lst[i]
    elif lst[i] is '.' and len(tokens) > 0 and tokens[-1].isdigit():
    elif lst[i] is '.' and i > 0 and tokens[-1].isdigit():
    tokens[-1] += lst[i]
    else:
    tokens.append(lst[i])
    print Calculator(tokens).exp()
    print Calculator(tokens).exp()
  8. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion calc.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor * term | factor / term | factor
    term ::= factor | factor * term | factor / term
    factor ::= number | ( exp )
    """

  9. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor * term | factor / term | factor
    factor ::= number | ( exp )
    """

    class Calculator():
    def __init__(self, tokens):
  10. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,3 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor * term | factor / term | factor
    factor ::= number | ( exp )
    """

    class Calculator():
    def __init__(self, tokens):
    @@ -16,7 +11,7 @@ def exp(self):
    result += self.term()
    while self._current is '-':
    self.next()
    result -= self.term
    result -= self.term()
    return result

    def factor(self):
  11. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ def exp(self):

    def factor(self):
    result = None
    if self._current.isdigit():
    if self._current[0].isdigit() or self._current[-1].isdigit():
    result = float(self._current)
    self.next()
    elif self._current is '(':
    @@ -49,8 +49,10 @@ def term(self):
    lst = list(raw_input('> ').replace(' ', ''))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and len(tokens) > 0 and tokens[-1].isdigit():
    if lst[i].isdigit() and len(tokens) > 0 and (tokens[-1].isdigit() or tokens[-1][-1] is '.'):
    tokens[-1] += lst[i]
    elif lst[i] is '.' and len(tokens) > 0 and tokens[-1].isdigit():
    tokens[-1] += lst[i]
    else:
    tokens.append(lst[i])
    print Calculator(tokens).exp()
    print Calculator(tokens).exp()
  12. @ascv ascv revised this gist Feb 24, 2013. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    """
    exp ::= term | exp + term | exp - term
    term ::= factor * term | factor / term | factor
    factor ::= number | ( exp )
    """

    class Calculator():
    def __init__(self, tokens):
    self._tokens = tokens
    @@ -32,15 +38,15 @@ def term(self):
    result = self.factor()
    while self._current is '*':
    self.next()
    result *= self.factor()
    result *= self.term()
    while self._current is '/':
    self.next()
    result /= self.factor()
    result /= self.term()
    return result

    if __name__ == '__main__':
    while True:
    lst = list(raw_input('> '))
    lst = list(raw_input('> ').replace(' ', ''))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and len(tokens) > 0 and tokens[-1].isdigit():
  13. @ascv ascv created this gist Feb 24, 2013.
    50 changes: 50 additions & 0 deletions calc.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    class Calculator():
    def __init__(self, tokens):
    self._tokens = tokens
    self._current = tokens[0]

    def exp(self):
    result = self.term()
    while self._current is '+':
    self.next()
    result += self.term()
    while self._current is '-':
    self.next()
    result -= self.term
    return result

    def factor(self):
    result = None
    if self._current.isdigit():
    result = float(self._current)
    self.next()
    elif self._current is '(':
    self.next()
    result = self.exp()
    self.next()
    return result

    def next(self):
    self._tokens = self._tokens[1:]
    self._current = self._tokens[0] if len(self._tokens) > 0 else None

    def term(self):
    result = self.factor()
    while self._current is '*':
    self.next()
    result *= self.factor()
    while self._current is '/':
    self.next()
    result /= self.factor()
    return result

    if __name__ == '__main__':
    while True:
    lst = list(raw_input('> '))
    tokens = []
    for i in range(len(lst)):
    if lst[i].isdigit() and len(tokens) > 0 and tokens[-1].isdigit():
    tokens[-1] += lst[i]
    else:
    tokens.append(lst[i])
    print Calculator(tokens).exp()