summaryrefslogtreecommitdiff
path: root/src/examples/SimpleCalc.py
blob: 577c8e3687815bb501883294a5c80ffe93fcd388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# SimpleCalc.py
#
# Demonstration of the parsing module, 
# Sample usage
#
#     $ python SimpleCalc.py 
#     Type in the string to be parse or 'quit' to exit the program
#     > g=67.89 + 7/5 
#     69.29
#     > g
#     69.29
#     > h=(6*g+8.8)-g 
#     355.25
#     > h + 1 
#     356.25
#     > 87.89 + 7/5 
#     89.29
#     > ans+10
#     99.29
#     > quit
#     Good bye!
#
# 

from __future__ import division

# Uncomment the line below for readline support on interactive terminal
# import readline  
from pyparsing import ParseException, Word, alphas, alphanums
import math

# Debugging flag can be set to either "debug_flag=True" or "debug_flag=False"
debug_flag=False

variables = {}

from fourFn import BNF, exprStack, fn, opn
def evaluateStack( s ):
    op = s.pop()
    if op == 'unary -':
        return -evaluateStack( s )
    if op in "+-*/^":
        op2 = evaluateStack( s )
        op1 = evaluateStack( s )
        return opn[op]( op1, op2 )
    elif op == "PI":
        return math.pi # 3.1415926535
    elif op == "E":
        return math.e  # 2.718281828
    elif op in fn:
        return fn[op]( evaluateStack( s ) )
    elif op[0].isalpha():
        if op in variables:
            return variables[op]
        raise Exception("invalid identifier '%s'" % op)
    else:
        return float( op )

arithExpr = BNF()
ident = Word(alphas, alphanums).setName("identifier")
assignment = ident("varname") + '=' + arithExpr
pattern = assignment | arithExpr

if __name__ == '__main__':
  # input_string
  input_string=''
  
  # Display instructions on how to quit the program
  print "Type in the string to be parsed or 'quit' to exit the program"
  input_string = raw_input("> ")
  
  while input_string != 'quit':
    if input_string.lower() == 'debug':
        debug_flag=True
        input_string = raw_input("> ")
        continue

    # Reset to an empty exprStack
    del exprStack[:]
  
    if input_string != '':
      # try parsing the input string
      try:
        L=pattern.parseString( input_string, parseAll=True )
      except ParseException,err:
        L=['Parse Failure',input_string]
      
      # show result of parsing the input string
      if debug_flag: print input_string, "->", L
      if len(L)==0 or L[0] != 'Parse Failure':
        if debug_flag: print "exprStack=", exprStack
  
        # calculate result , store a copy in ans , display the result to user
        try:
          result=evaluateStack(exprStack)
        except Exception,e:
          print str(e)
        else:
          variables['ans']=result
          print result
      
          # Assign result to a variable if required
          if L.varname:
            variables[L.varname] = result
          if debug_flag: print "variables=",variables
      else:
        print 'Parse Failure'
        print err.line
        print " "*(err.column-1) + "^"
        print err
  
    # obtain new input string
    input_string = raw_input("> ")
  
  # if user type 'quit' then say goodbye
  print "Good bye!"