Last active
June 21, 2020 08:07
-
-
Save wgaylord/46442e813919a4b1f1a3136e1fbb54df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re,sys | |
start = """ | |
#include <stdio.h> | |
unsigned char memory[256]; | |
unsigned char X; | |
""" | |
charInput = """ | |
char Input(){ | |
unsigned char t; | |
scanf("%s",&t); | |
return t; | |
} | |
""" | |
charOutput = """ | |
void Output(unsigned char t){ | |
printf("%c",t); | |
} | |
""" | |
numberInput = """ | |
unsigned char Input(){ | |
unsigned int temp = 0; | |
scanf("%d",&temp); | |
return (unsigned char)temp; | |
} | |
""" | |
numberOutput = """ | |
void Output(unsigned char t){ | |
printf("%d",(int)t); | |
} | |
""" | |
mainStart = """int main() { | |
""" | |
end = """ | |
return 0; | |
} | |
""" | |
instructionNumber = 0 | |
insturctionRegex = "([G,O,R,B,I,T,S,A,g,o,r,b,i,t,s,a])(\d*)" | |
if len(sys.argv) < 4: | |
print("Assemble2C.py <Input.gbt> <Input type> <Output type>") | |
print(" Input.gbt MUST end in .gbt to process correctly.") | |
print(" Input and Output type can be either C for Character or N for Number.") | |
print(" Generates a C source file based off the .gbt file.\n") | |
exit() | |
inputFile = open(sys.argv[1],"r") | |
inputFileText = inputFile.read() | |
inputFile.close() | |
outputFile = open(sys.argv[1].replace(".gbt",".c"),"w+") | |
Instructions = re.findall(insturctionRegex,inputFileText) | |
if len(Instructions) > 256: | |
print("You can only have 256 instructions for proper GORBITSA. Compiling anyways.") | |
outputFile.write(start) | |
if sys.argv[2] == "C": | |
outputFile.write(charInput) | |
else: | |
outputFile.write(numberInput) | |
if sys.argv[3] == "C": | |
outputFile.write(charOutput) | |
else: | |
outputFile.write(numberOutput) | |
outputFile.write(mainStart) | |
for x in Instructions: | |
inst = x[0] | |
N = x[1] | |
#print(inst,N) | |
outputFile.write("IC"+str(instructionNumber)+":") | |
if inst == "G": | |
outputFile.write("X = memory["+str(N)+"];\n") | |
instructionNumber+=1 | |
continue | |
if inst == "O": | |
outputFile.write("memory["+str(N)+"] = X;\n") | |
instructionNumber+=1 | |
continue | |
if inst == "R": | |
outputFile.write("X = Input();\n") | |
instructionNumber+=1 | |
continue | |
if inst == "B": | |
outputFile.write("if(X == 0){goto IC"+str(N)+";}\n") | |
instructionNumber+=1 | |
continue | |
if inst == "I": | |
outputFile.write("X+="+str(N)+";\n"); | |
instructionNumber+=1 | |
continue | |
if inst == "T": | |
outputFile.write("Output(X);\n") | |
instructionNumber+=1 | |
continue | |
if inst == "S": | |
outputFile.write("X = "+str(N)+";\n") | |
instructionNumber+=1 | |
continue | |
if inst == "A": | |
outputFile.write("X += memory["+str(N)+"];\n") | |
instructionNumber+=1 | |
continue | |
if inst == "g": | |
outputFile.write("X = memory[memory["+str(N)+"]];\n") | |
instructionNumber+=1 | |
continue | |
if inst == "o": | |
outputFile.write("memory[memory["+str(N)+"]] = X;\n") | |
instructionNumber+=1 | |
continue | |
if inst == "r": | |
outputFile.write("memory["+str(N)+"]=Input();\n") | |
instructionNumber+=1 | |
continue | |
if inst == "b": | |
outputFile.write("if(X == 0){") | |
for x in range(256): | |
outputFile.write("if(memory["+str(N)+"] =="+str(x)+"){goto IC"+str(x)+";}") | |
outputFile.write("}") | |
instructionNumber+=1 | |
continue | |
if inst == "i": | |
outputFile.write("memory["+str(N)+"]+=X;\n"); | |
instructionNumber+=1 | |
continue | |
if inst == "t": | |
outputFile.write("Output(memory["+str(N)+"]);\n") | |
instructionNumber+=1 | |
continue | |
if inst == "s": | |
outputFile.write("X = X^memory["+str(N)+"];\n") | |
instructionNumber+=1 | |
continue | |
if inst == "a": | |
outputFile.write("X += memory[memory["+str(N)+"]];\n") | |
instructionNumber+=1 | |
continue | |
while instructionNumber < 256: | |
outputFile.write("IC"+str(instructionNumber)+":X = X;\n") | |
instructionNumber+=1 | |
outputFile.write(end) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment