Last active
April 23, 2016 05:00
-
-
Save AnastasiaDunbar/16ce9d2073641cce623e5fcb64bb986d to your computer and use it in GitHub Desktop.
Took 2 days to create.
This file contains 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
''' | |
/*Print example*/ | |
//[0][97] | |
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++< //'a' in ASCII char is 97 | |
//[6][97] | |
++++++[>.<-] //Loop 6 times. Subtract first cell. | |
/*Input*/ | |
++++++++++++++++++++++++++++++[>,.<-] //This has 30 character limit | |
+[,.] //We could just do this. | |
>+[>,] //Add a value to move for loop. Add characters to each cell until there's a 0. | |
<[<] //Go back to the beginning until we get 0. | |
>>[.>] //Read all characters. | |
/*Print ASCII*/ | |
++++++++++++++++++++++++++++++++[+.] //Prints ASCII characters | |
/*Algorithms*/ | |
[>+>+<<-]>>[<<+>>-]<< //Duplicates 0 to 1 but be warned about 2 | |
#>$< //Brainfuck$ version, same thing | |
''' | |
import math | |
import os | |
import time | |
#SETTINGS HERE------------------------------------------------------------------------ | |
size = 150 | |
dynamic_size = True | |
limit = 256 #256 number limit by default. | |
buffers = True #Brainfuck$ | |
#Display | |
debug = True | |
display_memory = True | |
display_sleep = 0.01 | |
display_code = True | |
display_zeros = False | |
display_out = True | |
#------------------------------------------------------------------------------------- | |
characters = '<>+-[].,:;#$' | |
if(display_memory): | |
size = min(size,20) | |
at_startposition = 0 #(0 = 0, 1 = int(math.floor(size/2))) | |
at = at_startposition | |
array = [0]*size #list(range(0,size)) OOPS not zeros [1,2,3,4,5] | |
def only_include(inp,chars): | |
valids = "" | |
for character in inp: | |
if character in chars: | |
valids += character | |
return valids | |
def parse(string): | |
#maps the "["s to the corresponding "]"s | |
opening = [] | |
loop = {} | |
for i,c in enumerate(string): | |
if c == "[": | |
opening.append(i) | |
elif c == "]": | |
try: | |
begin = opening.pop() | |
loop[begin] = i | |
except IndexError: | |
print("ERROR: Supplied string isn't balanced, too many ]s!") | |
# if the stack isn't empty, the string cannot be balanced | |
if opening != []: | |
print("ERROR: Supplied string isn't balanced, too many [s!") | |
else: | |
return loop | |
def restart_brainfuck(): | |
global at; global array; global at_startposition; global size | |
if (at_startposition == 1): | |
at = int(math.floor(size/2)) | |
else: | |
at = 0 | |
array = [0]*size | |
def eval_brainfuck(string,*custominput): | |
global characters | |
string = only_include(string,characters) | |
restart_brainfuck() | |
global at; global size | |
global buffers; global debug | |
global display_memory; global display_sleep; global display_out; | |
if (len(custominput)>0): | |
inp = custominput[0] | |
elif (',' in string): | |
inp = str(raw_input("Write your input: ")) | |
output = "" #Printing out. | |
loop = parse(string) #Check for parenthesis error. | |
stack = [] | |
buffer_stack = [] | |
i = 0 | |
input_index = 0 | |
#for i in range(0,len(string)): | |
sleep_or_clear = display_memory or display_out | |
while i < len(string): | |
if (sleep_or_clear): | |
if not ((not display_memory) and output==""): | |
os.system('cls') | |
if (display_memory): | |
dms = '' | |
for dmi in range(0,len(array)): | |
dma = ('*' if (dmi == at) else '') | |
if ((not display_zeros) and array[dmi] == 0): | |
dms += '.' | |
else: | |
dms += '['+dma+str(array[dmi])+dma+']' | |
print(dms) | |
if string[i] == '>': | |
if debug: | |
print("Move "+str(at)+" one step to the right.") | |
at += 1 | |
if (dynamic_size): | |
if (at>=size): | |
array.append(0) | |
size += 1 | |
elif (at>=size or at<0): | |
print("ERROR: Outside the memory"); break | |
if string[i] == '<': | |
if debug: | |
print("Move "+str(at)+" one step to the left.") | |
at -= 1 | |
if (dynamic_size): | |
if (at<0): | |
array.insert(0,0) | |
at += 1 | |
size += 1 | |
elif (at>=size or at<0): | |
print("ERROR: Outside the memory"); break | |
if string[i] == '+': | |
if debug: | |
print("Add 1 to index "+str(at)+" with value "+str(array[at]%limit)+".") | |
array[at] = (array[at]+1)%limit | |
if string[i] == '-': | |
if debug: | |
print("Subtract 1 to index "+str(at)+" with value "+str(array[at]%limit)+".") | |
array[at] = (array[at]-1)%limit | |
if string[i] == '.': | |
output += chr(array[at]) | |
if debug: | |
print("Print out value "+str(array[at])+" at index "+str(at)+' into ASCII "'+chr(array[at])+'".') | |
if string[i] == ',': | |
if (input_index<len(inp)): | |
if debug: | |
print("Replace index "+str(at)+" with value "+str(array[at])+' with input character "'+str(inp[input_index])+'" with value '+str(ord(inp[input_index]))+".") | |
array[at] = ord(inp[input_index]) | |
input_index += 1 | |
else: | |
array[at] = 0 | |
if buffers: | |
if string[i] == '#': | |
if debug: | |
print("Push "+str(array[at])+" into buffer-stack.") | |
buffer_stack.append(array[at]) | |
if string[i] == '$': | |
if len(buffer_stack) > 0: | |
if debug: | |
print("Pop and replace "+str(array[at])+" with "+str(buffer_stack[-1])+".") | |
array[at] = buffer_stack.pop() | |
else: | |
print("ERROR: Empty buffer-stack"); break | |
if string[i] == ';': | |
'''if (inp[input_index].isdigit()): | |
array[at] = int(inp[input_index]) | |
#array[at] = int(str(array[at])+inp[input_index]) | |
else: | |
print("ERROR: Input is not a digit"); break | |
input_index += 1''' | |
while(True): | |
try: | |
array[at] = int(raw_input("Write an integer: "))%limit | |
break | |
except ValueError: | |
pass | |
if string[i] == ':': | |
output += str(array[at]) | |
if string[i] == '[': | |
if array[at] > 0: | |
stack.append(i) | |
if debug: | |
print("Enter loop with value "+str(array[at])+".") | |
else: #Go to the end of block. | |
i = loop[i] | |
if debug: | |
print("Stop looping.") | |
elif string[i] == ']': | |
#Jump back where you came from. | |
i = stack.pop()-1 | |
i += 1 | |
if (display_memory): | |
dcs = '' | |
for dci in range(i,len(string)): | |
dcs += string[dci] | |
print (dcs) | |
if (display_out and output!=""): | |
print(output) | |
if (sleep_or_clear and (output!=""or display_memory)): | |
time.sleep(display_sleep) | |
if (display_memory): | |
dms = '' | |
for dmi in range(0,len(array)): | |
dma = ('*' if (dmi == at) else '') | |
if ((not display_zeros) and array[dmi] == 0): | |
dms += '.' | |
else: | |
dms += '['+dma+str(array[dmi])+dma+']' | |
print(dms) | |
print(output) | |
if (display_memory): | |
time.sleep(2) | |
'''eval_brainfuck("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.") #Hello World! | |
eval_brainfuck(">"+('+'*ord('a'))+"<++++++[>.<-]") #Print 6 'a' | |
eval_brainfuck(">+[>,]<[<]>>[.>]") | |
eval_brainfuck('+'*13)+'[>+<-]') | |
eval_brainfuck('+'*13)+'>[-]>[-]<<[->+>+<<]>>[-<<+>>]<<') | |
eval_brainfuck("-,+[-[>>++++[>++++++++<-]<+<-[>+>+>-[>>>]<[[>+<-]>>+>]<<<<<-]]>>>[-]+>--[-[<->[-]]]<[++++++++++++<[>-[>+>>]>[+[<+>-]>+>>]<<<<<-]>>[<+>-]>[-[-<<[-]>>]<<[<<->>-]>>]<<[<<+>>-]]<[-]<.[-]<-,+]") | |
''' | |
eval_brainfuck("+[,.]","Welcome to Brainfuck.") | |
#USER INPUT EVALUATION AND CODE STORING | |
your_codes = ["++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",">+[>,]<[<]>>[.>]",('+'*6)+"[>"+('+'*3)+"<-]>:", | |
'''Multiplying | |
;> | |
;> | |
"<<#>>$<#>>$>" | |
<< Go back | |
[>[>+>+<<-]>>[<<+>>-]<<<-]>> Multiply | |
#<$#<$>>>#<$#<$<<< Move multiplication and go to start | |
A B Result * = | |
:>>> | |
++++++[>+++++++<-]>#<$>>#<$<. print "*" 42 | |
<<:>>> | |
++++++[>++++++++++<-]>#<$>>#<$<+. print "=" 61 | |
<<: | |
''', | |
'''Divide | |
>> Go twice to avoid out of memory error | |
;[-<+<+>>]> Load first value and duplicate it | |
;<< Load second value for dividing with | |
[ | |
>+>[<<->>->+<] | |
>[-<+>]< | |
<< | |
]< : > '''+('+'*ord('/'))+'''. >> : > '''+('+'*ord('='))+'''. << : | |
'''] | |
is_new = True | |
is_on = 0 | |
current_code = "" | |
while(True): | |
is_loading = False | |
is_a_command = False | |
usr_in = str(raw_input("Load or Create or Settings? (L/C/S):")) | |
if (usr_in.upper() == "L"): | |
is_a_command = True | |
if (len(your_codes)>0): | |
for i in range(0,len(your_codes)): | |
print(str(i)+": "+your_codes[i]) | |
while(True): | |
usr_open = str(raw_input("Open (INDEX):")) | |
try: | |
val = int(usr_open) | |
if (val >= 0 and val < len(your_codes)): | |
is_on = val | |
is_new = False | |
is_loading = True | |
current_code = your_codes[val] | |
break | |
except ValueError: | |
print(str(usr_open)+" is not a number.") | |
continue | |
else: | |
print("You don't haven't done any code.") | |
if (usr_in.upper() == "C" or is_loading): | |
is_a_command = True | |
is_new = not is_loading | |
while(True): | |
usr_in = str(raw_input("Write Brainfuck (S to save/Q to quit/D to delete"+("" if is_new else "/E to execute")+"/R to read): ")) | |
if (usr_in.upper() == "S"): | |
is_new = False | |
is_on = len(your_codes) | |
your_codes.append(current_code) | |
elif (usr_in.upper() == "Q"): | |
break | |
elif (usr_in.upper() == "D"): | |
if (is_new): | |
print("You haven't saved this code yet.") | |
else: | |
usr_in = str(raw_input("Are you sure? (Y/N)")) | |
if (usr_in.upper() == "Y"): | |
your_codes.pop(is_on) | |
print("Deleted.") | |
break | |
elif (usr_in.upper() == "N"): | |
print("Cancelled.") | |
elif (usr_in.upper() == "E" and not is_new): | |
eval_brainfuck(current_code) | |
elif (usr_in.upper() == "R"): | |
if (not is_new): | |
print("Saved: "+your_codes[is_on]) | |
print("Unsaved: "+current_code) | |
else: | |
current_code = usr_in | |
eval_brainfuck(usr_in) | |
if (usr_in.upper() == "S"): | |
is_a_command = True | |
while(True): | |
usr_set = str(raw_input("Debug? (Y/N): ")) | |
if (usr_set.upper() == 'Y'): | |
debug = True; break | |
elif (usr_set.upper() == 'N'): | |
debug = False; break | |
else: | |
print("That's not a setting.") | |
while(True): | |
try: | |
usr_set = str(raw_input("Sleep each instruction (in seconds): ")) | |
if (usr_set == ''): | |
break | |
else: | |
clamp = lambda x,a,b: min(max(x,a),b) | |
display_sleep = clamp(float(usr_set),0,1.5) | |
break | |
except ValueError: | |
print("That's not a setting.") | |
elif (not is_a_command): | |
print("I don't understand.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment