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 Brainfuck To FreeBASIC Compiler

Uploader:Mitglieddarkinsanity
Datum/Zeit:02.07.2011 01:41:44

declare function sbtfc_compile (code as string, memsize as uinteger) as string

dim code as string = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
open "test.bas" for output as #1
print #1, sbtfc_compile(code, 1024)
close #1
end

function sbtfc_compile (code as string, memsize as uinteger) as string
    dim fbcode as string
    dim codepointer as uinteger
    dim loop_c as uinteger = 0

    fbcode = "dim mem as ubyte ptr = callocate(" & memsize & ")" & chr(10)
    fbcode += "dim memptr as uinteger = 0" & chr(10,10)

    for codepointer = 0 to len(code)
        select case chr(code[codepointer])
            case ">"
                fbcode += string(loop_c,9) & "memptr += 1" & chr(10)
            case "<"
                fbcode += string(loop_c,9) & "memptr -= 1" & chr(10)
            case "+"
                fbcode += string(loop_c,9) & "mem[memptr] += 1" & chr(10)
            case "-"
                fbcode += string(loop_c,9) & "mem[memptr] -= 1" & chr(10)
            case "."
                fbcode += string(loop_c,9) & "print chr(mem[memptr]);" & chr(10)
            case ","
                fbcode += string(loop_c,9) & "mem[memptr] = asc(input(1))" & chr(10)
            case "["
                fbcode += string(loop_c,9) & "while not (mem[memptr]=0)" & chr(10)
                loop_c += 1
            case "]"
                loop_c -= 1
                fbcode += string(loop_c,9) & "wend" & chr(10)
            case else

        end select
    next
    fbcode += "deallocate(mem)" & chr(10)
    fbcode += "end" & chr(10)
    return fbcode
end function