2025-10-06 10:50:14 +11:00
|
|
|
import string
|
|
|
|
|
from sys import argv
|
|
|
|
|
file = open(argv[1]).readlines();
|
|
|
|
|
bfcode = ""
|
2025-10-06 12:52:38 +11:00
|
|
|
variables = []
|
2025-10-06 18:33:53 +11:00
|
|
|
varbytes = []
|
2025-10-06 10:50:14 +11:00
|
|
|
def removeChar(str, char):
|
2025-10-06 11:11:27 +11:00
|
|
|
ans = ""
|
2025-10-06 10:50:14 +11:00
|
|
|
for i in str:
|
|
|
|
|
if (i != char):
|
|
|
|
|
ans += i
|
|
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
def removeEnd(string, num):
|
|
|
|
|
ans = ""
|
|
|
|
|
for i in range(len(string)-1):
|
|
|
|
|
ans += string[i]
|
|
|
|
|
return ans
|
|
|
|
|
|
|
|
|
|
def ord2(char):
|
|
|
|
|
if (len(char) == 1):
|
|
|
|
|
return ord(char)
|
|
|
|
|
elif (len(char == 0)):
|
|
|
|
|
return(0)
|
|
|
|
|
|
|
|
|
|
for i in file:
|
|
|
|
|
if (i.split(" ")[0] == "create"):
|
2025-10-06 11:11:27 +11:00
|
|
|
if (i[len(i)-1] == "\n"):
|
|
|
|
|
i = removeEnd(i, 1)
|
2025-10-06 10:50:14 +11:00
|
|
|
bfcode += ">>>>>>>>>>>>[[>>]>>]"
|
|
|
|
|
|
2025-10-06 11:11:27 +11:00
|
|
|
bytes = int(removeChar(i.split("*", 1)[1].split("=", 1)[0], " "))
|
2025-10-06 18:33:53 +11:00
|
|
|
varbytes.append(bytes)
|
2025-10-06 10:50:14 +11:00
|
|
|
|
|
|
|
|
a = i.split("=", 1)[1]
|
|
|
|
|
if (a[0] == " "):
|
|
|
|
|
a = a.split(" ", 1)[1]
|
|
|
|
|
|
|
|
|
|
# Get type
|
|
|
|
|
if ((a[0] == "\"") & (a[len(a)-1] == "\"")):
|
|
|
|
|
type = "string"
|
|
|
|
|
else:
|
|
|
|
|
type = "undefined"
|
|
|
|
|
|
|
|
|
|
if (type == "string"):
|
|
|
|
|
a = a.split("\"", 1)[1]
|
|
|
|
|
a = removeEnd(a, 1)
|
|
|
|
|
for j in range(bytes):
|
|
|
|
|
bfcode += "+++>"
|
|
|
|
|
if (j < len(a)):
|
|
|
|
|
bfcode += "+" * ord(a[j]) + ">"
|
2025-10-06 18:33:53 +11:00
|
|
|
else:
|
|
|
|
|
bfcode += ">"
|
|
|
|
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
2025-10-06 12:52:38 +11:00
|
|
|
|
|
|
|
|
variables.append(i.split(" ")[1].split("*")[0])
|
|
|
|
|
|
|
|
|
|
elif (i.split(" ")[0] == "print"):
|
|
|
|
|
a = i.split(" ", 1)[1]
|
|
|
|
|
if a[len(a)-1] == "\n":
|
|
|
|
|
a = removeEnd(a, 1)
|
|
|
|
|
if (a in variables):
|
|
|
|
|
bfcode += ">>>>>>>>>>>>"
|
2025-10-06 18:33:53 +11:00
|
|
|
for j in range(variables.index(a)):
|
2025-10-06 12:52:38 +11:00
|
|
|
bfcode += "[>>]>>"
|
|
|
|
|
bfcode += "[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]"
|
|
|
|
|
else:
|
|
|
|
|
raise NameError(f"Could not find variable {a}")
|
2025-10-06 18:33:53 +11:00
|
|
|
|
|
|
|
|
elif (i.split(" ")[0] == "set"):
|
|
|
|
|
a = i.split(" ")[1]
|
|
|
|
|
if a[len(a)-1] == "\n":
|
|
|
|
|
a = removeEnd(a, 1)
|
|
|
|
|
if (a in variables):
|
|
|
|
|
bfcode += ">>>>>>>>>>>>"
|
|
|
|
|
for j in range(variables.index(a)):
|
|
|
|
|
bfcode += "[>>]>>"
|
|
|
|
|
for j in range(varbytes[variables.index(a)]):
|
|
|
|
|
bfcode += "[-]>[-]>"
|
|
|
|
|
for j in range(varbytes[variables.index(a)]):
|
|
|
|
|
bfcode += "<<"
|
|
|
|
|
# Get type
|
|
|
|
|
val = i.split("=", 1)[1].split(" ", 1)[1]
|
|
|
|
|
if (val[len(val)-1] == "\n"):
|
|
|
|
|
val = removeEnd(val, 1)
|
|
|
|
|
if (val[0] == "\"") & (val[len(val)-1] == "\""):
|
|
|
|
|
val = val.split("\"", 1)[1]
|
|
|
|
|
val = removeEnd(val, 1)
|
|
|
|
|
type = "string"
|
|
|
|
|
|
|
|
|
|
if (type == "string"):
|
|
|
|
|
for j in range(varbytes[variables.index(a)]):
|
|
|
|
|
bfcode += "+++>"
|
|
|
|
|
if (j >= len(val)):
|
|
|
|
|
bfcode += ">"
|
|
|
|
|
else:
|
|
|
|
|
bfcode += "+" * ord(val[j]) + ">"
|
|
|
|
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
|
|
|
|
else:
|
|
|
|
|
raise NameError(f"Could not find variable {a}")
|
2025-10-06 12:52:38 +11:00
|
|
|
|
|
|
|
|
elif ((i != "\n") & (i != "")):
|
|
|
|
|
raise ValueError(f"Invalid Command")
|
2025-10-06 10:50:14 +11:00
|
|
|
|
|
|
|
|
with open(argv[2], "w") as file:
|
|
|
|
|
file.write(bfcode)
|