Created
June 17, 2013 00:04
-
-
Save boxmein/5793938 to your computer and use it in GitHub Desktop.
HexChat/xchat plugin for inline reverse polish notation. The RPN part is unfinished.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xchat, re, threading | |
__module_name__ = "RPN" | |
__module_description = "Reverse Polish Notation (/disablerpn, /enablerpn, {{rpn}})" | |
__module_version__ = "1.3.37" | |
class TimeoutError(Exception): | |
pass | |
def timelimit(timeout): | |
def internal(function): | |
def internal2(*args, **kw): | |
class Calculator(threading.Thread): | |
def __init__(self): | |
threading.Thread.__init__(self) | |
self.result = None | |
self.error = None | |
def run(self): | |
try: | |
self.result = function(*args, **kw) | |
except: | |
self.error = sys.exc_info()[0] | |
c = Calculator() | |
c.start() | |
c.join(timeout) | |
if c.isAlive(): | |
raise TimeoutError | |
if c.error: | |
raise c.error | |
return c.result | |
return internal2 | |
return internal | |
# ---------------------------------------------------------------------------- # | |
# My code | |
# Regular expressions | |
# Integers | |
intx = re.compile(r"\d+") | |
floatx = re.compile(r"[\d.]+f") | |
operatorx = re.compile(r"[*+-/%^]") | |
expressionx = re.compile(r"{{.+?}}") | |
@timelimit (1) | |
def rpn (text): | |
words = text.split(" ") | |
stack = [] | |
tmpint = 0 | |
## TODO finish | |
for each in words: | |
# For every integer | |
if intx.match(each): | |
try: | |
tmpint = int(each) | |
except ValueError: | |
print "ValueError while converting %s to int! :C" % each | |
# For floating points | |
elif floatx.match(each): | |
try: | |
tmpint = float(each) | |
except ValueError: | |
print "ValueError while converting %s to int! :C" % each | |
def hookhandler (word, word_eol): | |
text = ' '.join(word) | |
matches = expressionx.finditer(text) | |
if matches: | |
for match in matches: | |
matchstr = match.group(0).strip("{} ") | |
try: | |
text = text[:match.start()] + rpn(matchstr) + text[match.end():] | |
except Exception: | |
print "Errored on RPN parsing" | |
else: | |
# okay I didn't find none, don't do anything | |
return xchat.EAT_NONE | |
xchat.command("say " + text) | |
# make sure it isn't unruly and sends the data itself | |
return xchat.EAT_XCHAT | |
al = xchat.hook_command('', hookhandler) | |
def disablerpn (word, word_eol): | |
xchat.unhook(al) | |
def enablerpn (word, word_eol): | |
global al | |
al = xchat.hook_command("", hookhandler) | |
bl = xchat.hook_command("disablerpn", disablerpn) | |
cl = xchat.hook_command("enablerpn", enablerpn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment