Dashboard Temp Share Shortlinks Frames API

HTMLify

BrainF#ck Interpreter
Views: 500 | Author: abh
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python

from sys import argv
from os import system






def help():
    print(
        "Usages: ./bf <options> <file>",
        "Available Options:",
        "-i: Intractive/REPL mode",
        "-i <file>: Interprete the file",
        "-c <file>: Compile the file",
        "-o <code> -o <compiled>: Write compiled file in <compiled>",
        "-h, --help: Shows this menu",
    )


def getch():
    return ord(input()[0])

def putch(char):
    print(chr(char), end="", flush=True)

def interpete(code):
    global array
    global p
    i = 0
    while i < len(code):
        c = code[i]
        if not c in "+-<>,.[]":
            i += 1
            continue
        if c == "+":
            array[p] += 1
        if c == "-":
            array[p] -= 1
        if c == "<":
            p -= 1
        if c == ">":
            p += 1
        if c == ",":
            array[p] = getch()
        if c == ".":
            putch(array[p])
        if c == "]":
            if array[p] > 0:
                b = 1
                while b:
                    i -= 1
                    if code[i] == "[":
                        b -= 1
                    if code[i] == "]":
                        b += 1
        i += 1

def intractive():
    array = [0]*30000
    p = 0
    output = ""
    while True:
        code = input(">>> ")
        while code.count("[") != code.count("]"):
            code += input("... ")
        interpete(code)
        if "." in code:
            print("")


def bf2c(code):
    c_code = """
    #include<stdio.h>
    int main(){
    char a[30000];
    char *p = a;
    """
    i = 0
    while i < len(code):
        c = code[i]
        if not c in "+-<>,.[]":
            i += 1
            continue
        if c == "+":
            c_code += "*p++;"
            #array[p] += 1
        if c == "-":
            c_code += "*p--;"
            array[p] -= 1
        if c == "<":
            c_code += "p--;"
            #p -= 1
        if c == ">":
            c_code += "p++;"
            #p += 1
        if c == ",":
            c_code += "*p=getchar();"
            #array[p] = getch()
        if c == ".":
            c_code += "putchar(*p);"
        if c == "[":
            c_code += "while(*p){"
        if c == "]":
            c_code += "}"
        i += 1
    c_code += "return 0;}"
    return c_code


global array, p
array = [0]*30000
p = 0

args = len(argv)

if args == 1:
    print("No input files run with -h or --help for help menu")
    quit()
elif args == 2:
    if argv[1][0] != "-":
        code_file = argv[1]
    if argv[1] == "-h":
        help()
    if argv[1] == "-i":
        intractive()
elif args == 3:
    mode = argv[1]
    code_file = argv[2]
    if mode == "-c":
        output_file = code_file[:code_file.find(".")]
        c_code = bf2c(open(code_file).read())
        with open(output_file + ".c", "w") as f:
            f.write(c_code)
        system("gcc " + output_file + ".c -o " + output_file)
        #system("rm "+output_file + ".c")
    if mode == "-i":
        interpete(open(code_file).read())
elif args == 5:
    mode = argv[1]
    code_file = argv[2]
    if argv[3] == "-o":
        output_file = argv[4]


if __name__ == "__main__":
    pass