brainfuck

Creating a Brainfuck parser, whats the best method of parsing loop operators?

半世苍凉 提交于 2019-12-05 09:59:11
I'm creating a Brainfuck parser (in a BASIC dialect) ultimately to create an interpreter but i've realise it's not as straight forward as i first thought. My problem is that i need a way to accurately parse the matching loop operators within a Brainfuck program. This is an example program: ,>,>++++++++[<------<------>>-] <<[>[>+>+<<-]>>[<<+>>-]<<<-] >>>++++++[<++++++++>-],<.>. '[' = start of loop ']' = end of loop I need to record the start and end point of each matching loop operator so i can jump around the source as needed. Some loops are alone, some are nested. What would be the best way

How does the Brainfuck Hello World actually work?

喜夏-厌秋 提交于 2019-12-04 07:21:04
问题 Someone sent this to me and claimed it is a hello world in Brainfuck (and I hope so...) ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. I know the basics that it works by moving a pointer and increment and decrementing stuff... Yet I still want to know, how does it actually work? How does it print anything on the screen in the first place? How does it encode the text? I do not understand at all... 回答1: 1. Basics To understand

Bugku-CTF加密篇之 .!? (..... ..... ..... ..... !?!!. ?.... ..... ..... ..... .?.?! .?... .!........ ..... !.?.. ..... !?!!. ?!!!! !!?.? !.?!! !!!.. ..... ..... .!.?.....)

假如想象 提交于 2019-12-03 13:24:48
.!? ..... ..... ..... ..... !?!!. ?.... ..... ..... ..... .?.?! .?... .!........ ..... !.?.. ..... !?!!. ?!!!! !!?.? !.?!! !!!.. ..... ..... .!.?...... ...!? !!.?. ..... ..?.? !.?.. ..... .!.?. ..... ..... !?!!. ?!!!!!!!!! !?.?! .?!.? ..... ....! ?!!.? ..... ...?. ?!.?. ..... !.?.. .....!?!!. ?!!!! !!?.? !.?!! !!!!! !!!!. ..... ...!. ?.... ...!? !!.?. .....?.?!. ?..!. ?.... ..... !?!!. ?!!!! !!!!? .?!.? !!!!! !!!!! !!!.? .......!?! !.?.. ....? .?!.? ....! .!!!. !!!!! !!!!! !!!!! !!.?. ..... .!?!!.?... ...?. ?!.?. ..... !.!!! !!!!! !.?.. ..... ..!?! !.?.. ..... .?.?!.?... ..... !.?. 本题要点:

Printing a number in brainfuck?

此生再无相见时 提交于 2019-12-03 10:29:33
I've searched for a while, but i couldn't find anything that could help me. Let's say the first cell(or value, etc.) equals 165. How do i print "165"? My idea was to cut the number into seperate pieces: 1,6 and 5. It would than be no problem to print them. Note: I don't just want to print "165". I want to print the value the first cell has. No matter if it's 165, 255, 0, 1 or anything else. use a famous modulo procedure ( http://esolangs.org/wiki/brainfuck_algorithms will help you) >+++++++++++[-<+++++++++++++++>] # initialize 165 at first cell >++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[

Detecting infinite loop in brainfuck program

夙愿已清 提交于 2019-12-03 08:29:28
问题 I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of cases, and hence the GA gets stuck at the point. So, I need a mechanism to detect infinite loops and avoid executing that code in bf. One obvious (trivial) case is when I have [] I can detect this and refuse to run that program. For the non-trivial

Detecting infinite loop in brainfuck program

给你一囗甜甜゛ 提交于 2019-12-02 23:54:54
I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of cases, and hence the GA gets stuck at the point. So, I need a mechanism to detect infinite loops and avoid executing that code in bf. One obvious (trivial) case is when I have [] I can detect this and refuse to run that program. For the non-trivial cases, I figured out that the basic idea is: to determine how one iteration of the loop changes the

what does the '~' mean in python? [duplicate]

孤者浪人 提交于 2019-11-30 18:26:20
This question already has an answer here: bit-wise operation unary ~ (invert) 5 answers what does the '~' mean in python? i found this BF interpreter in python a while ago. import sys #c,i,r,p=0,0,[0]*255,raw_input() c=0 i=0 p=raw_input() r=[0]*255 while c<len(p): m,n,u=p[c],0,r[i] if m==">":i+=1 if m=="<":i-=1 if m=="+":r[i]+=1 if m=="-":r[i]-=1 if m==".":sys.stdout.write(chr(u)) if m=="[": if ~u: while 1: m=p[c] if m=="]":n-=1 if m=="[":n+=1 if ~n:break c+=1 if m=="]": if u: while 1: m=p[c] if m=="]":n-=1 if m=="[":n+=1 if ~n:break c-=1 c+=1 and i want to know what it does because i want to

what does the '~' mean in python? [duplicate]

爷,独闯天下 提交于 2019-11-30 01:24:44
问题 This question already has answers here : bit-wise operation unary ~ (invert) (5 answers) Closed 3 years ago . what does the '~' mean in python? i found this BF interpreter in python a while ago. import sys #c,i,r,p=0,0,[0]*255,raw_input() c=0 i=0 p=raw_input() r=[0]*255 while c<len(p): m,n,u=p[c],0,r[i] if m==">":i+=1 if m=="<":i-=1 if m=="+":r[i]+=1 if m=="-":r[i]-=1 if m==".":sys.stdout.write(chr(u)) if m=="[": if ~u: while 1: m=p[c] if m=="]":n-=1 if m=="[":n+=1 if ~n:break c+=1 if m=="]":

Brainfuck compare 2 numbers as greater than or less than

人走茶凉 提交于 2019-11-29 09:14:42
How can I compare two numbers with an inequality? (greater than or less than) I want to compare single digits For example 1 2 5 3 9 2 etc. This is the best way to compare two numbers.Why because, if you are intelligent enough, you can use the same code in bigger programs.It's highly portable. Assume we have two numbers a,b. we have two blocks : if( a>=b ) and else , Hope its enough. 0 1 0 a b 0 Make the array like this. And point to the (4) i.e. point to the a +>+< This is for managing if a=0 and b=0 [->-[>]<<] This is a magic loop. if a is the one which reaches 0 first (a<b),then pointer will

How to calculate the sum of 2 numbers with BrainFuck

别等时光非礼了梦想. 提交于 2019-11-28 07:47:23
I'm trying to write a program with BrainFuck that can read two numbers up to 9, calculate the sum of them and then print the result out, e.g. 3 & 5 give the result 8 . I'm just trying to understand the BF language but it looks much harder than I thought it would be. dirkgently Think of the language as a huge tape (30K bytes long) where you can read, write and move forward or backward and increment/decrement one cell at a time (each cell being 1 byte, so you effectively have 30K cells). Optionally, you can read in and write out stuff that the byte stream holds(in ASCII form). Assuming that you