While loop
This commit is contained in:
@@ -30,4 +30,16 @@ create var *5 = "Hello"
|
|||||||
if bool {
|
if bool {
|
||||||
set var = "BASM!"
|
set var = "BASM!"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## while bool
|
||||||
|
|
||||||
|
Runs code until a boolean is false
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```
|
||||||
|
create bool *1 = true
|
||||||
|
while bool {
|
||||||
|
# Loops forever because bool is never false
|
||||||
|
}
|
||||||
```
|
```
|
50
src/main.py
50
src/main.py
@@ -1,4 +1,4 @@
|
|||||||
from signal import Sigmasks
|
import os
|
||||||
import string
|
import string
|
||||||
import math as m
|
import math as m
|
||||||
from sys import argv
|
from sys import argv
|
||||||
@@ -6,6 +6,14 @@ file = open(argv[1]).readlines();
|
|||||||
bfcode = ""
|
bfcode = ""
|
||||||
variables = []
|
variables = []
|
||||||
varbytes = []
|
varbytes = []
|
||||||
|
braces = []
|
||||||
|
conditions = []
|
||||||
|
|
||||||
|
def find_file(filename, root_dir):
|
||||||
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
||||||
|
if filename in filenames:
|
||||||
|
return os.path.join(dirpath, filename)
|
||||||
|
return None
|
||||||
|
|
||||||
def removeChar(str, char):
|
def removeChar(str, char):
|
||||||
ans = ""
|
ans = ""
|
||||||
@@ -38,7 +46,6 @@ def filewrite():
|
|||||||
with open(argv[2], "w") as file:
|
with open(argv[2], "w") as file:
|
||||||
file.write(bfcode)
|
file.write(bfcode)
|
||||||
|
|
||||||
filewrite()
|
|
||||||
for i in file:
|
for i in file:
|
||||||
while (i[0] == " "):
|
while (i[0] == " "):
|
||||||
i = i.split(" ", 1)[1]
|
i = i.split(" ", 1)[1]
|
||||||
@@ -170,13 +177,16 @@ for i in file:
|
|||||||
bfcode += "<<"
|
bfcode += "<<"
|
||||||
# Get type
|
# Get type
|
||||||
val = i.split("=", 1)[1].split(" ", 1)[1]
|
val = i.split("=", 1)[1].split(" ", 1)[1]
|
||||||
|
type = None
|
||||||
if (val[len(val)-1] == "\n"):
|
if (val[len(val)-1] == "\n"):
|
||||||
val = removeEnd(val, 1)
|
val = removeEnd(val, 1)
|
||||||
if (val[0] == "\"") & (val[len(val)-1] == "\""):
|
if (val[0] == "\"") & (val[len(val)-1] == "\""):
|
||||||
val = val.split("\"", 1)[1]
|
val = val.split("\"", 1)[1]
|
||||||
val = removeEnd(val, 1)
|
val = removeEnd(val, 1)
|
||||||
type = "string"
|
type = "string"
|
||||||
|
elif (val == "true") | (val == "false"):
|
||||||
|
type = "bool"
|
||||||
|
|
||||||
if (type == "string"):
|
if (type == "string"):
|
||||||
for j in range(varbytes[variables.index(a)]):
|
for j in range(varbytes[variables.index(a)]):
|
||||||
bfcode += "+++>"
|
bfcode += "+++>"
|
||||||
@@ -201,37 +211,59 @@ for i in file:
|
|||||||
for j in tempint:
|
for j in tempint:
|
||||||
bfcode += "++>"
|
bfcode += "++>"
|
||||||
bfcode += "+" * j + ">"
|
bfcode += "+" * j + ">"
|
||||||
|
elif (type == "bool"):
|
||||||
|
bfcode += "++++>"
|
||||||
|
if (val == "true"):
|
||||||
|
bfcode += "+"
|
||||||
|
bfcode += ">"
|
||||||
|
else:
|
||||||
|
raise TypeError("Could not find variable type")
|
||||||
bfcode += "<<[[<<]<<]<<<<<<<<"
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
||||||
else:
|
else:
|
||||||
raise NameError(f"Could not find variable {a}")
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
elif (i.split(" ")[0] == "if"):
|
elif (i.split(" ")[0] == "if") | (i.split(" ")[0] == "while"):
|
||||||
|
braces.append(i.split(" ")[0])
|
||||||
|
|
||||||
# Argument must be boolean
|
# Argument must be boolean
|
||||||
if not ('{' in i):
|
if not ('{' in i):
|
||||||
raise SyntaxError(f"Expected {{ in if statement")
|
raise SyntaxError("Expected { in if statement")
|
||||||
a = removeChar(i.split(" ", 1)[1].split("{")[0], " ")
|
a = removeChar(i.split(" ", 1)[1].split("{")[0], " ")
|
||||||
if (a in variables):
|
if (a in variables):
|
||||||
|
bflen = len(bfcode)
|
||||||
bfcode += ">>>>>>>>>>>>"
|
bfcode += ">>>>>>>>>>>>"
|
||||||
bfcode += "[>>]>>" * (variables.index(a))
|
bfcode += "[>>]>>" * (variables.index(a))
|
||||||
|
|
||||||
|
|
||||||
bfcode += ">[>+>+<<-]>[<+>-]>[-<<<"
|
bfcode += ">[>+>+<<-]>[<+>-]>[-<<<"
|
||||||
bfcode += "[[<<]<<]"
|
bfcode += "[[<<]<<]"
|
||||||
bfcode += "+>>>>"
|
bfcode += "+>>>>"
|
||||||
bfcode += "[>>]>>" * (variables.index(a))
|
bfcode += "[>>]>>" * (variables.index(a))
|
||||||
bfcode += ">>>]"
|
bfcode += ">>>]"
|
||||||
|
|
||||||
bfcode += "<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
bfcode += "<<<" + "[<<]<<" * (variables.index(a)+1)
|
||||||
|
bfcode += "[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||||
|
|
||||||
|
conditions.append(bfcode[-(len(bfcode)-bflen):])
|
||||||
|
|
||||||
bfcode += "[-"
|
bfcode += "[-"
|
||||||
else:
|
else:
|
||||||
raise NameError(f"Could not find variable {a}")
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
elif (i == "}"):
|
elif (removeChar(removeChar(i, " "), "\n") == "}"):
|
||||||
bfcode += "]"
|
if (braces[len(braces)-1] == "if"):
|
||||||
|
bfcode += "]"
|
||||||
|
elif (braces[len(braces)-1] == "while"):
|
||||||
|
bfcode += f"{conditions[-1]}]"
|
||||||
|
else:
|
||||||
|
raise SyntaxError("How on earth did you get here")
|
||||||
|
braces.pop()
|
||||||
|
conditions.pop()
|
||||||
|
|
||||||
# No command
|
# No command
|
||||||
elif ((i != "\n") & (i != "")):
|
elif ((i != "\n") & (i != "")):
|
||||||
|
print(i)
|
||||||
raise ValueError(f"Invalid Command")
|
raise ValueError(f"Invalid Command")
|
||||||
|
|
||||||
print("DONE!")
|
print("Successfully compiled!")
|
||||||
filewrite()
|
filewrite()
|
2
test.bf
2
test.bf
@@ -1 +1 @@
|
|||||||
>>>>>>>>>>>>[[>>]>>]++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]><<<<<<<<<<+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<]
|
>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<
|
@@ -1,5 +1,14 @@
|
|||||||
create bool *1 = false
|
create bool *1 = true
|
||||||
create var *5 = "Hello"
|
create hello *5 = "Hello"
|
||||||
if bool {
|
create bool2 *1 = true
|
||||||
set var = "BASM!"
|
while bool2 {
|
||||||
}
|
set bool2 = false
|
||||||
|
if bool {
|
||||||
|
set bool2 = true
|
||||||
|
}
|
||||||
|
if bool2 {
|
||||||
|
set bool = false
|
||||||
|
print hello
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set bool2 = true
|
Reference in New Issue
Block a user