# Copyright 2010, 2011 G24 # # This file is part of gpy. # # gpy is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # gpy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with gpy. If not, see <http://www.gnu.org/licenses/>. import main class mod_main(main.clisten): """Calc module.""" def addcmds(self): self.name = "calc" self.cmds = {"rpn":"rpn"} def rpn(self): '''Reverse Polish Notation Calc function.''' a = self.conn.recvinfo["message.params"].split(" ") self.stack = [] for char in a: try: char = float(char)# if it succeeds, then char is a number self.stack.append(char) except Exception,e: #float(char) failed, it's nonsense or an operator try: a = float(self.stack.pop()) #last b = float(self.stack.pop()) #pre-last if(char == "+"): self.stack.append(a + b) elif(char == "-"): self.stack.append(b - a) elif(char == "^"): self.stack.append(pow(b,a)) elif(char == "*"): self.stack.append(a * b) elif(char == "/"): self.stack.append(b / a) except Exception, e: self.conn.reply(str(e)) return print self.stack try: self.conn.reply(str(self.stack[0])) except Exception, e: return