While loop
This commit is contained in:
@@ -31,3 +31,15 @@ if bool {
|
||||
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
|
||||
}
|
||||
```
|
46
src/main.py
46
src/main.py
@@ -1,4 +1,4 @@
|
||||
from signal import Sigmasks
|
||||
import os
|
||||
import string
|
||||
import math as m
|
||||
from sys import argv
|
||||
@@ -6,6 +6,14 @@ file = open(argv[1]).readlines();
|
||||
bfcode = ""
|
||||
variables = []
|
||||
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):
|
||||
ans = ""
|
||||
@@ -38,7 +46,6 @@ def filewrite():
|
||||
with open(argv[2], "w") as file:
|
||||
file.write(bfcode)
|
||||
|
||||
filewrite()
|
||||
for i in file:
|
||||
while (i[0] == " "):
|
||||
i = i.split(" ", 1)[1]
|
||||
@@ -170,12 +177,15 @@ for i in file:
|
||||
bfcode += "<<"
|
||||
# Get type
|
||||
val = i.split("=", 1)[1].split(" ", 1)[1]
|
||||
type = None
|
||||
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"
|
||||
elif (val == "true") | (val == "false"):
|
||||
type = "bool"
|
||||
|
||||
if (type == "string"):
|
||||
for j in range(varbytes[variables.index(a)]):
|
||||
@@ -201,37 +211,59 @@ for i in file:
|
||||
for j in tempint:
|
||||
bfcode += "++>"
|
||||
bfcode += "+" * j + ">"
|
||||
elif (type == "bool"):
|
||||
bfcode += "++++>"
|
||||
if (val == "true"):
|
||||
bfcode += "+"
|
||||
bfcode += ">"
|
||||
else:
|
||||
raise TypeError("Could not find variable type")
|
||||
bfcode += "<<[[<<]<<]<<<<<<<<"
|
||||
else:
|
||||
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
|
||||
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], " ")
|
||||
if (a in variables):
|
||||
bflen = len(bfcode)
|
||||
bfcode += ">>>>>>>>>>>>"
|
||||
bfcode += "[>>]>>" * (variables.index(a))
|
||||
|
||||
|
||||
bfcode += ">[>+>+<<-]>[<+>-]>[-<<<"
|
||||
bfcode += "[[<<]<<]"
|
||||
bfcode += "+>>>>"
|
||||
bfcode += "[>>]>>" * (variables.index(a))
|
||||
bfcode += ">>>]"
|
||||
|
||||
bfcode += "<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||
bfcode += "<<<" + "[<<]<<" * (variables.index(a)+1)
|
||||
bfcode += "[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||
|
||||
conditions.append(bfcode[-(len(bfcode)-bflen):])
|
||||
|
||||
bfcode += "[-"
|
||||
else:
|
||||
raise NameError(f"Could not find variable {a}")
|
||||
|
||||
elif (i == "}"):
|
||||
elif (removeChar(removeChar(i, " "), "\n") == "}"):
|
||||
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
|
||||
elif ((i != "\n") & (i != "")):
|
||||
print(i)
|
||||
raise ValueError(f"Invalid Command")
|
||||
|
||||
print("DONE!")
|
||||
print("Successfully compiled!")
|
||||
filewrite()
|
2
test.bf
2
test.bf
@@ -1 +1 @@
|
||||
>>>>>>>>>>>>[[>>]>>]++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]><<<<<<<<<<+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<]
|
||||
>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<
|
@@ -1,5 +1,14 @@
|
||||
create bool *1 = false
|
||||
create var *5 = "Hello"
|
||||
create bool *1 = true
|
||||
create hello *5 = "Hello"
|
||||
create bool2 *1 = true
|
||||
while bool2 {
|
||||
set bool2 = false
|
||||
if bool {
|
||||
set var = "BASM!"
|
||||
set bool2 = true
|
||||
}
|
||||
if bool2 {
|
||||
set bool = false
|
||||
print hello
|
||||
}
|
||||
}
|
||||
set bool2 = true
|
Reference in New Issue
Block a user