Author Topic: brainfsck in python by willrandship  (Read 3697 times)

0 Members and 1 Guest are viewing this topic.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
brainfsck in python by willrandship
« on: August 17, 2012, 12:43:18 am »
Code: [Select]
Just a little project I'm working on. It's not working yet, but it should be  :banghead:

[code]#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
#  untitled.py

#  Copyright 2012 Unknown <william@willdesktop>

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.


import sys

try:
prog = open(sys.argv[1],'r')
except IndexError:
print ("Usage: python script program")
exit()
except IOError:
print("File Not Found")
exit()

BYTE_MAX = 255 #These values are the default for an 8-bit implementation.
ARRAY_MAX = 29999

loops = {}
ptr = 0

array = []
for a in range(30000): #Give array the proper dimensions
array += [0]


#These are the executing functions for the commands.

def inc_ptr():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
ptr += 1
if ptr > ARRAY_MAX:
ptr = 0

def dec_ptr():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
ptr += -1
if ptr == -1:
ptr = ARRAY_MAX

def inc_byte():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
array[ptr] += 1
if array[ptr] > BYTE_MAX:
array[ptr] = 0

def dec_byte():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
array[ptr] += -1
if array[ptr] == -1:
array[ptr] = BYTE_MAX

def out_byte():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
print (chr(array[ptr]))

def in_byte():
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
array[ptr] = ord(input("In: ")[0:1]) #Python3 uses input, python2 uses raw_input. change if it nameErrors. (2 because of unrecognized chars, 3 because raw_input is meaningless.)

def in_loop(run_line):
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
if array[ptr] == 0:
run_line = loops[run_line]
print ("Jumped to "+str(run_line))

def out_loop(run_line):
global ptr,ARRAY_MAX,BYTE_MAX,loops,array
if array[ptr] != 0:
run_line = loops[run_line]
print ("Returned to "+str(run_line))

#These are other functions, mostly for startup prep.

def find_loops():
global loops
global prog
op_num = 0
in_loops = []
out_loops = []
for line in prog:
for char in line:
if char == "[":
in_loops += [op_num]
if char == "]":
out_loops += [op_num]
op_num += 1

if len(in_loops) != len(out_loops):
print ("Error: Nonmatching bracket count.")
exit()

for a in range(len(in_loops)):
loops[in_loops[a]]  = out_loops[a]
loops[out_loops[a]] =  in_loops[a]
print ("Found "+str(len(in_loops))+" loops")

def filter_prog(): #Currently unused
global prog
op_str = ""
for line in prog:
for char in line:
if char in "><+-.,[]": #Eliminate useless characters
op_str += char
op_str += "!" #Signals an EOF. Don't worry, you can still have ! in the file elsewhere.
return op_str

def exec_op_str(op_str):
op = ''
op_num = 0
while op != '!':
op = op_str[op_num]

#Contains the if statements for all the commands, executing them.
if True: #For grouping purposes
if op == ">":
inc_ptr()

if op == "<":
dec_ptr()

if op == "+":
inc_byte()

if op == "-":
dec_byte()

if op == ".":
print ("Printed a char")
out_byte()

if op == ",":
in_byte()

if op == "[":
in_loop(op_num)

if op == "]":
out_loop(op_num)
op_num += 1


def main():
global array, prog
find_loops()
prog.seek(0)
op_str = prog.read()
exec_op_str(op_str)
print (array[0:100])
return 0

if __name__ == '__main__':
main()

Oh btw {code} turned my tabs into spaces, but the code should still work fine.[/code]
« Last Edit: August 17, 2012, 12:44:24 am by willrandship »