Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Brainfuck Generator - String
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Degensquared




PostPosted: Thu Jul 10, 2008 9:10 pm   Post subject: Brainfuck Generator - String

This is just a program I wrote for fun recently. If you give it a string, it will output the Brainfuck code required to output that string. Just make sure that the file exists if you choose to output to file.

Enjoy!

code:

var menuinput : string
var reply : string (1)
var filename : string
var inputstring : string := ""
var tofile : boolean := false

put "Brainfuck Generator - Output String"
put "By: Degensquared"
put "Press any key to continue."
getch(reply)
cls
put "Enter the string that you would like to be converted to bf code:"
get inputstring : *
put "output to file? (y/n)"
get menuinput
if menuinput = "y" then
    tofile := true
    put "What is the filename of the output file? (out.txt is default)"
    get filename
end if
put "Press any key to begin parsing"
getch (reply)
cls


var input : array 1 .. length (inputstring) of int
var output : string := "++++++++++[>+++>+++++++>++++++++++>+<<<<-]"
var pointerloc : int := 0
var cells : array 1 .. 3 of int := init (30, 70, 100)
var curcell : int := 0
var stream : int
var outputarray : flexible array 1 .. 0 of string

if tofile then
    open : stream, filename, put
end if

% Fill the input array with the ascii values for each of
for i : 1 .. upper (input)
    input (i) := ord (inputstring (i))
end for

%Fixes the compiler throwing an exception for strings > 255 char
%If output string gets to large, it transfers the string to an array so it can be output later.
proc checklength
    if length (output) >= 254 then
        new outputarray, upper (outputarray) + 1
        outputarray (upper (outputarray)) := output
        output := ""
    end if
end checklength

%For each letter in input
for i : 1 .. upper (input)
    curcell := 0
    %Check if it should use the first cell
    if input (i) < 57 then
        curcell := 1
        % Checks current pointer location, and moves it to cell 1 if it isn't already
        if pointerloc > 1 then
            for j : 1 .. pointerloc - 1
                output += "<"
                checklength
                pointerloc -= 1
            end for
        else
            output += ">"
            checklength
            pointerloc += 1
        end if
        %Checks wether or not to use third cell
    elsif input (i) > 90 then
        curcell := 3
        % Moves pointer to 3rd location if it isn't already there
        if pointerloc < 3 then
            for j : 1 .. abs (3 - pointerloc)
                output += ">"
                checklength
                pointerloc += 1
            end for
        end if
    else
        curcell := 2
        if pointerloc < 2 then
            for j : 1 .. abs (2 - pointerloc)
                output += ">"
                checklength
                pointerloc += 1
            end for
        elsif pointerloc > 2 then
            output += "<"
            checklength
            pointerloc -= 1
        end if
    end if
    %Increments or Decrements the cell to match input value
    if input (i) > cells (curcell) then
        for j : 1 .. abs (input (i) - cells (curcell))
            output += "+"
            checklength
            cells (curcell) += 1
        end for
    elsif input (i) < cells (curcell) then
        for j : 1 .. cells (curcell) - input (i)
            output += "-"
            checklength
            cells (curcell) -= 1
        end for
    end if
    % Output character
    output += "."
end for

% Output Code.
put "Brain Fuck code to output '", inputstring, "':"
for i : 1 .. upper (outputarray)
    put outputarray (i) ..
end for
put output ..

if tofile then
put ""
put ""
    put "Written to ", filename
    for i : 1 .. upper (outputarray)
        put : stream, outputarray (i) ..
    end for
    put : stream, output
    close : stream
end if
Sponsor
Sponsor
Sponsor
sponsor
SNIPERDUDE




PostPosted: Thu Jul 10, 2008 9:55 pm   Post subject: RE:Brainfuck Generator - String

man, BF get annoying, lol.

Good job Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: