Brainfuck: Handle segmentation faults properly.
parent
b5afe0f832
commit
1dd8522392
|
@ -39,13 +39,19 @@ from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
||||||
|
|
||||||
_ = PluginInternationalization('Brainfuck')
|
_ = PluginInternationalization('Brainfuck')
|
||||||
|
|
||||||
class BrainfuckSyntaxError(Exception):
|
class BrainfuckException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class BrainfuckTimeout(Exception):
|
class BrainfuckSyntaxError(BrainfuckException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class NotEnoughInput(Exception):
|
class BrainfuckTimeout(BrainfuckException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NotEnoughInput(BrainfuckException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class SegmentationFault(BrainfuckException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class BrainfuckProcessor:
|
class BrainfuckProcessor:
|
||||||
|
@ -90,6 +96,8 @@ class BrainfuckProcessor:
|
||||||
self.memory.append(0)
|
self.memory.append(0)
|
||||||
elif char == '<': # Decrement pointer
|
elif char == '<': # Decrement pointer
|
||||||
self.memoryPointer -= 1
|
self.memoryPointer -= 1
|
||||||
|
if self.memoryPointer < 0:
|
||||||
|
raise SegmentationFault(_('Negative memory pointer.'))
|
||||||
elif char == '+': # Increment data
|
elif char == '+': # Increment data
|
||||||
self.memory[self.memoryPointer] += 1
|
self.memory[self.memoryPointer] += 1
|
||||||
elif char == '-': # Decrement data
|
elif char == '-': # Decrement data
|
||||||
|
@ -177,6 +185,9 @@ class Brainfuck(callbacks.Plugin):
|
||||||
except NotEnoughInput:
|
except NotEnoughInput:
|
||||||
irc.error(_('Input too short.'))
|
irc.error(_('Input too short.'))
|
||||||
return
|
return
|
||||||
|
except SegmentationFault as e:
|
||||||
|
irc.error(_('Segmentation fault: %s') % e.args[0])
|
||||||
|
return
|
||||||
irc.reply(output)
|
irc.reply(output)
|
||||||
brainfuck = wrap(brainfuck, [getopts({'recover': '',
|
brainfuck = wrap(brainfuck, [getopts({'recover': '',
|
||||||
'input': 'something'}),
|
'input': 'something'}),
|
||||||
|
|
|
@ -68,5 +68,9 @@ class BrainfuckTestCase(PluginTestCase):
|
||||||
self.assertNotError('brainfuck --input a ,.')
|
self.assertNotError('brainfuck --input a ,.')
|
||||||
self.assertResponse('brainfuck --recover .', 'a')
|
self.assertResponse('brainfuck --recover .', 'a')
|
||||||
|
|
||||||
|
def testMemory(self):
|
||||||
|
self.assertResponse('brainfuck <',
|
||||||
|
'Error: Segmentation fault: Negative memory pointer.')
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
|
Loading…
Reference in New Issue