Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Simple FreeBASIC Brainfuck Interpreter

Uploader:Mitglieddarkinsanity
Datum/Zeit:30.06.2011 22:27:33

const sfbi_sample_hello_world as string = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
const sfbi_sample_rot13 as string = ",[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>++++++++++++++<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>>+++++[<----->-]<<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>++++++++++++++<-[>+<-[>+<-[>+<-[>+<-[>+<-[>++++++++++++++<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>>+++++[<----->-]<<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>++++++++++++++<-[>+<-]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]>.[-]<,]"
const sfbi_sample_fibonacci as string = "+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]"

const sfbi_error_memory_overrun as integer = 2
const sfbi_error_memory_underrun as integer = 4
const sfbi_error_code_overrun as integer = 8
const sfbi_error_code_underrun as integer = 16
declare function sfbi_run (code as string, memsize as uinteger) as integer


dim code as string = sfbi_sample_hello_world
dim return_code as integer

return_code = sfbi_run(code, 1024)
if return_code then
    print "execution finished"
else
    print "return_code: " & return_code
end if
sleep
end


function sfbi_run (code as string, memsize as uinteger) as integer
    dim memory as string = string(memsize,0)
    dim instruction_pointer as uinteger
    dim data_pointer as uinteger
    dim scope_level as integer

    for instruction_pointer = 0 to len(code)
        select case chr(code[instruction_pointer])
            case ">"
                data_pointer += 1
                if (data_pointer>memsize-1) then return sfbi_error_memory_overrun
            case "<"
                data_pointer -= 1
                if (data_pointer>memsize-1) then return sfbi_error_memory_underrun
            case "+"
                memory[data_pointer] += 1
            case "-"
                memory[data_pointer] -= 1
            case "."
                print chr(memory[data_pointer]);
            case ","
                memory[data_pointer] = asc(input(1))
            case "["
                if (memory[data_pointer] = 0) then
                    dim oldlevel as uinteger = scope_level
                    scope_level += 1
                    do until (scope_level = oldlevel)
                        instruction_pointer += 1
                        if (instruction_pointer>len(code)-1) then return sfbi_error_code_overrun
                        select case chr(code[instruction_pointer])
                            case "["
                                scope_level += 1
                            case "]"
                                scope_level -= 1
                        end select
                    loop
                else
                    scope_level += 1
                end if
                continue for
            case "]"
                if (memory[data_pointer] = 0) then
                    scope_level -= 1
                    continue for
                else
                    dim oldlevel as integer = scope_level
                    scope_level -= 1
                    do until (scope_level = oldlevel)
                        instruction_pointer -= 1
                        if (instruction_pointer>len(code)-1) then return sfbi_error_code_underrun
                        select case chr(code[instruction_pointer])
                            case "["
                                scope_level += 1
                            case "]"
                                scope_level -= 1
                        end select
                    loop
                end if
                continue for
            case else
                continue for
        end select
    next
    return -1
end function