def main(): if 'posix' == os.name: readline.parse_and_bind('set editing-mode vi') while True: try: eqn_str = raw_input("Roll it! ('q' to quit): ") except EOFError: break if '' == eqn_str: continue if 'q' == eqn_str[0]: break try: const_strings = [] ans_str = [] answers = None is_separated, const_strings, ( ans_str, answers) = rollparse.solve_roll(eqn_str) except Exception as e: print e #print sys.exc_traceback.tb_lineno for frame in traceback.extract_tb(sys.exc_info()[2]): fname, lineno, fn, text = frame logging.debug("Error in %s on line %d" % (fname, lineno)) continue if 0 < len(const_strings) and not is_separated: print "Calculated constants:" i = 1 for c in const_strings: print " {0}: {1}".format(i, c) i += 1 if is_separated: print "Rolls:" for i in range(0, len(ans_str)): const_counter = 1 for const_str in const_strings[i]: print " [{0}: {1}]".format(const_counter, const_str) const_counter += 1 print " {0}: {1} = {2}".format(i + 1, ans_str[i], answers[i]) else: print "{0} = {1}".format(ans_str, answers) return 0
def main(): if 'posix' == os.name: readline.parse_and_bind('set editing-mode vi') while True: try: eqn_str = raw_input("Roll it! ('q' to quit): ") except EOFError: break if '' == eqn_str: continue if 'q' == eqn_str[0]: break try: const_strings = [] ans_str = [] answers = None is_separated, const_strings, (ans_str, answers) = rollparse.solve_roll(eqn_str) except Exception as e: print e #print sys.exc_traceback.tb_lineno for frame in traceback.extract_tb(sys.exc_info()[2]): fname,lineno,fn,text = frame logging.debug("Error in %s on line %d" % (fname, lineno)) continue if 0 < len(const_strings) and not is_separated: print "Calculated constants:" i = 1 for c in const_strings: print " {0}: {1}".format(i, c) i += 1 if is_separated: print "Rolls:" for i in range(0,len(ans_str)): const_counter = 1 for const_str in const_strings[i]: print " [{0}: {1}]".format(const_counter, const_str) const_counter += 1 print " {0}: {1} = {2}".format(i+1, ans_str[i], answers[i]) else: print "{0} = {1}".format(ans_str, answers) return 0
def roll_it(self, *args): """This performs the actual roll. Arguments: """ self.dice_eqn_input.clear_start_text() eqn_text = self.dice_eqn_input.text if eqn_text == "": if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return if self.help_match.match(eqn_text): self.help_is_on = True self.dh = dice_help() help_str, self.dice_eqn_input.text = self.dh.next() self.stash_print(help_str) self.complete_stash_print() if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return if self.help_done.search(eqn_text): self.help_is_on = False self.stash_print("Just type help again if you need it.") self.complete_stash_print() del self.dh self.dice_eqn_input.text = "" if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return try: if self.help_is_on: try: help_str, self.dice_eqn_input.text = self.dh.next() self.stash_print(help_str) self.complete_stash_print() except StopIteration: self.help_is_on = False var_array = [eqn.strip() for eqn in eqn_text.split(":", 2)] var_name = None is_var = False if 1 < len(var_array): var_name, eqn_text = var_array is_var = True eqn_text_save = eqn_text eqns = [eqn.strip() for eqn in eqn_text.split(";")] for eqn_text in reversed(eqns): is_separated, const_strings, (ans_str, answers) = rollparse.solve_roll(eqn_text) self.stash_print(eqn_text) if 0 < len(const_strings) and not is_separated: self.stash_print("\tCalculated constants:") i = 1 for c in const_strings: self.stash_print("\t {0}: {1}".format(i, c)) i += 1 if is_separated: self.stash_print("\tRolls:") for i in range(0, len(ans_str)): const_counter = 1 for c_str in const_strings[i]: self.stash_print("\t [{0}: {1}]".format(const_counter, c_str)) const_counter += 1 self.stash_print("\t {0}: {1} = {2}".format(i + 1, ans_str[i], answers[i])) else: self.stash_print("\t{0} = {1}".format(ans_str, answers)) self.add_to_history(eqn_text_save) if is_var: self.add_var(var_name, eqn_text_save) except Exception as e: print e self.stash_print(str(e)) self.complete_stash_print() if not self.help_is_on: self.dice_eqn_input.text = "" if not MOBILE: Clock.schedule_once(self.set_eqn_focus) if MOBILE: self.dice_eqn_input.focus = False self.hide_vkbd()
import rollparse import logging logging.basicConfig(level=logging.DEBUG) production = "roll" rollparser = rollparse.RollParser( rollparse.declaration ) rollparse.solve_roll("2x2d8*2+3") rollparse.solve_roll("d8") rollparser.parse("[d6+d8+5]+3", processor=rollparse.lexer.Lexer() ) rollparser.parse("{2xd6}+3+3xd8", processor=rollparse.lexer.Lexer() ) rollparser.parse("{2xd6]+3+3xd8", processor=rollparse.lexer.Lexer() ) #should give error
def roll_it(self, *args): """This performs the actual roll. Arguments: """ self.dice_eqn_input.clear_start_text() eqn_text = self.dice_eqn_input.text if eqn_text == '': if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return if self.help_match.match(eqn_text): self.help_is_on = True self.dh = dice_help() help_str, self.dice_eqn_input.text = self.dh.next() self.stash_print(help_str) self.complete_stash_print() if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return if self.help_done.search(eqn_text): self.help_is_on = False self.stash_print("Just type help again if you need it.") self.complete_stash_print() del self.dh self.dice_eqn_input.text = "" if not MOBILE: Clock.schedule_once(self.set_eqn_focus) return try: if self.help_is_on: try: help_str, self.dice_eqn_input.text = self.dh.next() self.stash_print(help_str) self.complete_stash_print() except StopIteration: self.help_is_on = False var_array = [eqn.strip() for eqn in eqn_text.split(':', 2)] var_name = None is_var = False if 1 < len(var_array): var_name, eqn_text = var_array is_var = True eqn_text_save = eqn_text eqns = [eqn.strip() for eqn in eqn_text.split(';')] for eqn_text in reversed(eqns): is_separated, const_strings, (ans_str, answers) = \ rollparse.solve_roll(eqn_text) self.stash_print(eqn_text) if 0 < len(const_strings) and not is_separated: self.stash_print("\tCalculated constants:") i = 1 for c in const_strings: self.stash_print("\t {0}: {1}".format(i, c)) i += 1 if is_separated: self.stash_print("\tRolls:") for i in range(0, len(ans_str)): const_counter = 1 for c_str in const_strings[i]: self.stash_print("\t [{0}: {1}]".format( \ const_counter, c_str)) const_counter += 1 self.stash_print("\t {0}: {1} = {2}".format( \ i+1, ans_str[i], answers[i])) else: self.stash_print("\t{0} = {1}".format(ans_str, answers)) self.add_to_history(eqn_text_save) if is_var: self.add_var(var_name, eqn_text_save) except Exception as e: print e self.stash_print(str(e)) self.complete_stash_print() if not self.help_is_on: self.dice_eqn_input.text = "" if not MOBILE: Clock.schedule_once(self.set_eqn_focus) if MOBILE: self.dice_eqn_input.focus = False self.hide_vkbd()
import rollparse import logging logging.basicConfig(level=logging.DEBUG) production = "roll" rollparser = rollparse.RollParser(rollparse.declaration) rollparse.solve_roll("2x2d8*2+3") rollparse.solve_roll("d8") rollparser.parse("[d6+d8+5]+3", processor=rollparse.lexer.Lexer()) rollparser.parse("{2xd6}+3+3xd8", processor=rollparse.lexer.Lexer()) rollparser.parse("{2xd6]+3+3xd8", processor=rollparse.lexer.Lexer()) #should give error