Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

bell.bf

// Sounds the system bell

   1  
   2  URL: http://snippets.dzone.com/posts/show/3539
   3  
   4  add seven
   5  while not zero
   6  	putchar
   7  	minus one
   8  
   9  +++++++[.-]

rot13.bf

// ROT13 in Brainfuck

   1  
   2  URL: http://snippets.dzone.com/posts/show/3538
   3  
   4  +[,+[-[>+>+<<-]>[<+>-]+>>++++++++[<-------->-]<-[<[-]>>>+[<+<+>>-]<[>+<-]<[<++>
   5  >>+[<+<->>-]<[>+<-]]>[<]<]>>[-]<<<[[-]<[>>+>+<<<-]>>[<<+>>-]>>++++++++[<-------
   6  ->-]<->>++++[<++++++++>-]<-<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<<<<+>>>>++++[<++++++++
   7  >-]>-]<<-<-]>[<<<<[-]>>>>[<<<<->>>>-]]<<++++[<<++++++++>>-]<<-[>>+>+<<<-]>>[<<+
   8  >>-]+>>+++++[<----->-]<-[<[-]>>>+[<+<->>-]<[>+<-]<[<++>>>+[<+<+>>-]<[>+<-]]>[<]
   9  <]>>[-]<<<[[-]<<[>>+>+<<<-]>>[<<+>>-]+>------------[<[-]>>>+[<+<->>-]<[>+<-]<[<
  10  ++>>>+[<+<+>>-]<[>+<-]]>[<]<]>>[-]<<<<<------------->>[[-]+++++[<<+++++>>-]<<+>
  11  >]<[>++++[<<++++++++>>-]<-]>]<[-]++++++++[<++++++++>-]<+>]<.[-]+>>+<]>[[-]<]<]

bf.py

// Brainfuck Interpreter

   1  
   2  #!/usr/bin/env python
   3  
   4  """A Brainfuck interpreter"""
   5  
   6  __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
   7  __date__="18 Nov 2005 - 27 Feb 2006"
   8  __copyright__="Copyright 2006 Andrew Pennebaker"
   9  __license__="GPL"
  10  __version__="0.5"
  11  __URL__="http://snippets.dzone.com/posts/show/3536"
  12  
  13  from aio import chomp
  14  
  15  import sys
  16  from getopt import getopt
  17  
  18  tape=[0]*100
  19  address=0
  20  
  21  def sublevel(toplevel):
  22  	i=0
  23  
  24  	# until a balanced-bracket code block is found, add a character
  25  
  26  	while toplevel[0:i+1].count("[")!=toplevel[0:i+1].count("]"): i+=1
  27  
  28  	return toplevel[1:i]
  29  
  30  def run(instructions):
  31  	global tape
  32  	global address
  33  
  34  	position=0
  35  	while position<len(instructions):
  36  		cmd=instructions[position]
  37  
  38  		if cmd=="<": address-=1
  39  		elif cmd==">": address+=1
  40  		elif cmd=="+": tape[address]+=1
  41  		elif cmd=="-": tape[address]-=1
  42  		elif cmd==".": sys.stdout.write(chr(tape[address]))
  43  		elif cmd==",":
  44  			try: tape[address]=ord(sys.stdin.read(1))
  45  			except: tape[address]=-1
  46  		elif cmd=="[":
  47  			level=sublevel(instructions[position:])
  48  			while tape[address]!=0: run(level)
  49  
  50  			position+=len(level)+1
  51  
  52  		position+=1
  53  
  54  def usage():
  55  	print "Usage: %s [options] <sourcefile>" % (sys.argv[0])
  56  	print "--help (usage)"
  57  
  58  	sys.exit()
  59  
  60  def main():
  61  	systemArgs=sys.argv[1:] # ignore program name
  62  
  63  	live=False
  64  
  65  	optlist=[]
  66  	args=[]
  67  
  68  	try:
  69  		optlist, args=getopt(systemArgs, None, ["help"])
  70  	except Exception, e:
  71  		usage()
  72  
  73  	live=len(args)<1
  74  
  75  	for option, value in optlist:
  76  		if option=="--help":
  77  			usage()
  78  
  79  	if live:
  80  		print "--BF Interpreter 0.5--"
  81  		print "  Type exit to exit."
  82  
  83  		line="not exit"
  84  		while line!="exit":
  85  			sys.stdout.write("% ")
  86  			line=chomp(sys.stdin.readline())
  87  
  88  			if line.count("[")!=line.count("]"):
  89  				raise "Unbalanced brackets"
  90  			else:
  91  				run(line)
  92  	else:
  93  		src=args[0]
  94  
  95  		srcfile=open(src, "r")
  96  		code="".join(srcfile.readlines())
  97  		srcfile.close()
  98  
  99  		if code.count("[")!=code.count("]"):
 100  			raise "Unbalanced brackets"
 101  		else:
 102  			run(code)
 103  
 104  if __name__=="__main__":
 105  	try:
 106  		main()
 107  	except KeyboardInterrupt, e:
 108  		pass
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS