From ffc57460b1321e9b04e3fda82e8758eae7a1ab91 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 15 Mar 2009 01:14:45 -0600 Subject: [PATCH] more cleanup of bf --- plugins/bf.py | 94 +++++++++++++------------------------------- plugins/bf.pyc | Bin 2578 -> 0 bytes plugins/dice.pyc | Bin 2148 -> 0 bytes plugins/filter.pyc | Bin 708 -> 0 bytes plugins/hash.pyc | Bin 866 -> 0 bytes plugins/misc.pyc | Bin 426 -> 0 bytes plugins/pyexec.pyc | Bin 689 -> 0 bytes plugins/twitter.pyc | Bin 1097 -> 0 bytes plugins/weather.pyc | Bin 1450 -> 0 bytes 9 files changed, 28 insertions(+), 66 deletions(-) delete mode 100644 plugins/bf.pyc delete mode 100644 plugins/dice.pyc delete mode 100644 plugins/filter.pyc delete mode 100644 plugins/hash.pyc delete mode 100644 plugins/misc.pyc delete mode 100644 plugins/pyexec.pyc delete mode 100644 plugins/twitter.pyc delete mode 100644 plugins/weather.pyc diff --git a/plugins/bf.py b/plugins/bf.py index b83ab68..153ae3f 100644 --- a/plugins/bf.py +++ b/plugins/bf.py @@ -5,109 +5,71 @@ import re import random BUFFER_SIZE = 5000 +MAX_STEPS = 1000000 #command -def bf(bot, inp, input=None, max_steps=1000000, no_input=0): - """Runs a Brainfuck program given as a string. +def bf(bot, input): + """Runs a Brainfuck program.""" - The string must contain nothing but "<>[]+-.,", i.e. - be already filtered. - - If 'input' is None, stdin is used. 'input' can be - a string or a tuple; tuples are only used if - extended_interpretation is non-zero, and should - be more or less obvious in that case. - - if max_steps is < 0, there is no restriction.""" - program = re.sub('[^][<>+-.,]', '', inp.inp) + program = re.sub('[^][<>+-.,]', '', input.inp) # create a dict of brackets pairs, for speed later on - parens={} - open_parens=[] + brackets={} + open_brackets=[] for pos in range(len(program)): if program[pos] == '[': - open_parens.append(pos) + open_brackets.append(pos) elif program[pos] == ']': - if len(open_parens) > 0: - parens[pos] = open_parens[-1] - parens[open_parens[-1]] = pos - open_parens.pop() + if len(open_brackets) > 0: + brackets[pos] = open_brackets[-1] + brackets[open_brackets[-1]] = pos + open_brackets.pop() else: return 'unbalanced brackets' -# if len(open_parens) != 0: -# return 'unbalanced brackets' + if len(open_brackets) != 0: + return 'unbalanced brackets' + # now we can start interpreting - pc = 0 # program counter + ip = 0 # instruction pointer mp = 0 # memory pointer steps = 0 memory = [0] * BUFFER_SIZE #initial memory area rightmost = 0 - if input != None: - if type(input) == type(()): - # we'll only be using input[0] right now - inputs, input = input, input[0] - input_pointer = 0 output = "" #we'll save the output here - if no_input: - eof_reached = 1 - else: - eof_reached = 0 - # the main program loop: - while pc < len(program): - c = program[pc] + while ip < len(program): + c = program[ip] if c == '+': memory[mp] = memory[mp] + 1 % 256 elif c == '-': memory[mp] = memory[mp] - 1 % 256 elif c == '>': - mp = mp + 1 + mp += 1 if mp > rightmost: rightmost = mp if mp >= len(memory): - memory = memory + [0]*BUFFER_SIZE # no restriction on memory growth! + memory.extend([0]*BUFFER_SIZE) # no restriction on memory growth! elif c == '<': mp = mp - 1 % len(memory) elif c == '.': output += chr(memory[mp]) if len(output) > 500: break - - elif program[pc] == ',': - if eof_reached: - raise Exception, "Program tries reading past EOF" - if input == None: - #char = sys.stdin.read(1) - char = chr(random.randint(1,255)) - if char == '': # EOF - memory[mp] = 0 - eof_reached = 1 - else: - memory[mp] = ord(char) - else: - if input_pointer == len(input): # EOF - memory[mp] = 0 - eof_reached = 1 - else: - memory[mp] = ord(input[input_pointer]) - input_pointer = input_pointer + 1 - - elif program[pc] == '[': + elif c == ',': + memory[mp] = random.randint(1,255) + elif c == '[': if memory[mp] == 0: - pc = parens[pc] - - elif program[pc] == ']': + ip = brackets[ip] + elif c == ']': if memory[mp] != 0: - pc = parens[pc] + ip = brackets[ip] - pc += 1 + ip += 1 steps += 1 - if max_steps >= 0 and steps > max_steps: + if steps > MAX_STEPS: output += "Maximum number of steps exceeded" break - - # end of while loop - return unicode(re.sub('[\r\n\x00]', '/', output), 'iso-8859-1')[:400] + return unicode(re.sub('[\r\n\x00]', '/', output), 'iso-8859-1')[:430] diff --git a/plugins/bf.pyc b/plugins/bf.pyc deleted file mode 100644 index 0a07a4306cee2f224fe70a51fea7afe283b50fa9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2578 zcmZ`*-ESL35T83IcH-FiZazvut0K`r6Q@P08j2NFEveK8N|k^rB?{L0Zf##`-(K$a zss!0Q5F~g&2+E&=5HI{oyr4cJmEX)=5&`NtJ2yK!-!nIxU;nCCw|@BJu}{TQ#``{2 zdJiBV+6QzfaOt^2`#^HsrG1xz6784xC>Wz)oPsia3kndI_SPz_1M<{PyfEry{&N*Z zRwr?y0hJecvDJa*EE{o{cwU-5%`fyW{#X;g+if$$2q}%|)#|X!%9>b=RXBgEO;%W@GlI6?$O$U6$6B$TL4GvJT1HiYQw+Nf*$OR= zYC@{h5XD%bqY{}dn4aW4Oalp48DX!lzyVhn$7$6aP1DGw2;Q^inH+OzG)qG`TLpQ7 zMspNZa)voN`Pm&#UgqYA$c$VGdy-a1^E8~NU{VY%!BCBcRpjd`E$3)6ngughrvA(F z7OkAK{OJfwO;|pK76d#&%ZMA;ur6u*P{<=hToq?(65eKYT_lG^W~Hh#;?6iZPsPG*2%`m% zi(pn1{0;?kWa=mfDmF*oC?1vd~N)Y}?`bq+I_r!y(qXn}?c{4KGsi<15#L3v4z z-zzzGnt9+;al$SPXT&?v@g`BQu_)SLhE^}dv6Doj4MMnG#F01qyC4+RL?gqcX%Nu2 zT=hwk*za7*86G3!)lisl|ATEasGojO~F_wKJ=(H@6QCZ*DZ!udl1Hp>yfBR$f2R zUT~^9VIN~p2l>H;>c<^*BaGtA-cVtxo|s6l!&Cs}*k&pCFV1~0Qf(~`o%^T^2C1@H z+}9uHFZiEmWupG6NUN|T0{X=2D9}OcVg!3OG-4z&(Z*LgG3&~U0+sem)(=Dm&JPSL zOH^N{sWR;sp~+HAab7V&ep?RFnYzTiLW(Mney#&P_NRV*x5|0*oUdv*xI`D(Z=oYCzyS*1{z;C1z4K}W?cYWf+f7t ztSxV}fMtD&7j^TKzK$$k#a5jw7$bz}!55Dn?d-Pp9`Em948aPzMi6@%=fZw3 z!N@1tfv+(qTvS0hE?8U_Vh3ScQEQSwXjv5Uy}QOYrW~}5-L3J4J(DICr59DU6QD;1 zJ%#=i6~)IS)?zebl#0U!A+(A;M;)6~k`Vjy8sxD{dPQ`g(Rim~ED5_kI{?2Nh^5rl z&;4n|MjBcUO=j5-&X6{p7AouabRfm?B|U!6OQawIQq~gpvu+rro9zz%i4bSn9j;^g zBLLM_N@b_)EKu2*liz|f%|Ev`#`LmNm$m9%ajI_FtuQQ=Xu+xCWT{w;Lx=M=-YZy( zz$e`~rvkk0GNlT+MVF=UhIY0!UqH~Dt9&xH#@!%UZnXmAw_3aG=aD* diff --git a/plugins/dice.pyc b/plugins/dice.pyc deleted file mode 100644 index fd8f62fccc987e75b70b51ccf7070b7c7ff67b20..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2148 zcma)7&2Ah;5U!s6vp3n;;0=yI34JhV{EIP(2qChAa1Oa7VvGoiZLIP3c+<}8%w)RP zb|hOu3L<$QuDk$89smh$oOuEGs%BS7Bu8TGuBqy(ssF0|<)2Rb#qqEAdNg|Kc;CTs z1%M%X1gOXa^hD7kAQc|aqks}cwnnB#&%vsX$-0n@G1(BZIVPJzw#c;TCn%pFGa<+% zo7f5`+45b0wz0irrFf=~OY41->HTMVs~0Cnc{+4@ZFTkLk{%W&_K9KYi?*|aVG7b| z{<6%|)V1xuc<#Fz^zFDs7vlZH_TuvHMz~`ZPj}3M!FxXBt2b`u7v|hM5Uyl5Zn5P2 z24vkdZo_S0%exEnhV?AW_X1q#ZU?x$kHi!Bl04H0=na-A-HK>QNiSFgY%xiB4 ziL;y=`*Mb;r+_iVP3HJ~1aNYVBix;(GeyXaMI4elg!`R(+N6il8Jt*)!+fIqA*=CC zfHKZZKJc7;Ms4OR9t*6jBFC^00+0YV1*}MieVe&YMm^@Z>u&dxEIBF4^>;xy{&j@UinL~JtRC_}yzzq9}8@+>|Wxhw`o z?q0cbKWwlrY0BY1(shC)^%J>5s>BK3fl!v0gE&2kON$|@GRWUb9V4z+l|%MT$nz2@ z1~QqwOOTUkp`fEW>RRwI-gChfCDSqiE}ySi>PzT6#VV1~K1X#fNV{USY-I{)oos{m zP9=k!o?%3qWLtD915r$n;uc|H4AxnV`~=y_5%;W4C%@Baoi=xWqq7G2wtOjT$zCAX z+kwU=ok1PelA^|26dkrHM?&ymy0_Si?*ShAX@ZBm zG(zrtw+pbs{j6_~_usl+*5_=+C7rDFS9B%3X4Y1h&5gzDT84N{Z?3JnCGCd$zKnZb zf4X^NNyozK?@Mek)tacX(Yt34}IDN9x_E=&K(rv3JDoIP>+ATM=(l#~Z)er&V8 zjy15@FX_Xie4^KHZHK&RNBvlM?m6d6TcGUN0x%(yy|-ub-rk;c>hWWXHDAQNWUs6m zncZbA;@95TFXpnkLF{{vMT@ijjBhz(2R1Wtnxa);ut=?E2Je%8nGXvWu}s7(PF#!k zo!EXbisWut=4CZkz6@E15c|0olQt`cULtTs(u>%Oh=)|JIMWfYoJs&IGX(MW^%U_e zUBz(=ELWRW7lO-bT1}{?no=!lst+(>-9W30ss?N}xT-FxP9O_9;(dqT6fDQ28g>q> zmk$b?CK3OfMZEB$E`#?NaLi)zl~;nDH}H-lzdgv!FiqB_bgZ!nM=l0aa1MXL00W^IFP!d_B z5m@^R&n)W!#yAo>#ql>VTTM0;qpVQ0)1F#PU_BSdH~i9jJ+Zq{b@Ik98`8&t(Ji b3CzDH0sX&|m$kp#xV$aRE7`Iakx}#;aEXB> diff --git a/plugins/hash.pyc b/plugins/hash.pyc deleted file mode 100644 index 3a5685e589167df296da4033b8cabf7711fdd805..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 866 zcmbVKO-lnY5S{GSYDEzQ^;+;!Q0f;7iin^WFFmZFAb4oIYa6ZG)ud2CPZjky_&2=z ztNa7{-c+fSg5Z)&W|NsWllSuWF+Dl=`hHZG?=Qf*kLi|RVhkf!B4=;{d4yvm4%G=H z&Z!eJ=D0M5c>9U3QYXgGJRsIQjK{H&dm}cG9&9K*L7Kr)rIcg9I83wZnk`%NA^huj zQ&*d2zQD7j8{0NITXRtQ5C+vjeuqSc|1~HD6 zxQe1I7wfzs8)5?+m~I=UyDs-4=S%XS?8hPtkizRvFOV0=@=K6~6LXW7Q&p5u3cXYO zrBEaBRqt<=hoS3xW|UWfjk2ojfQwO*II6naNYbR6-R@2Qlq(nwhWtebj%n66xB8-! zpv$&RRdtuOZ2vI~6VDE`_NcO|9$&>2D-u!6tWhfax6*7%6ft5TrFx?f9ktxOZpWQg Pvd3)4?5VJjE9AZap}2$0 diff --git a/plugins/misc.pyc b/plugins/misc.pyc deleted file mode 100644 index 9e5b463ae1a4b954e707b736c2909c229ee487da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 426 zcmZ`#yH3ME5S+Cg9zhW)CL5K;t^70E?{DCmqGp$qoeBQU|V ziqKv_v=>wnCL6#+Ffmj)roui6e(EtMZvuI_g;<{3lq7hM{(1*cpL)2_A{}sK?i2DX zo6b0ip#=O6Da-G(yPH%?ze_6HrYUR{w60heP3?8}$JU1gYs%+PtXgA(Vr&U#AHq7>eLl5Xe z4L^o?-P+PEZFxB7HtuAZ3upeHWF+i)P3MDT_a9( zbiR&>VQGC7^=qB2g0CH?_W+OEiG8o6t1I5>WAIXFgd|%wl4ykMBr&by0iC@EW)`oO@V4{olt&hNtf aOY@&xJcROi)l^+@&kZo^c*e+bFaHC~8jV^2 diff --git a/plugins/twitter.pyc b/plugins/twitter.pyc deleted file mode 100644 index 56b1b59070a6a164da07bc842f8276672487e6df..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1097 zcmaJ=&2H2%5FRJn?f$e-fz*}@@}*)gY+B#|RVf_Gfy*9t5lEHLDoxzhZ4#&Uq}`R; zQxQLpzzgsKTzM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