Input and equal commands
This commit is contained in:
@@ -19,16 +19,26 @@ Changes the value of an already created value.
|
|||||||
|
|
||||||
Example: `set var = "World"`
|
Example: `set var = "World"`
|
||||||
|
|
||||||
|
## input var *bytes = type *ibytes
|
||||||
|
|
||||||
|
Creates a new variable that is `bytes` long, that reads the first `ibytes` of input.
|
||||||
|
|
||||||
|
Example: `input str *5 = string *3` (3 bytes of input, remaining 2 bytes are left blank)
|
||||||
|
|
||||||
## if bool
|
## if bool
|
||||||
|
|
||||||
Runs code if a boolean is true.
|
Runs code if a boolean is true.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```
|
```
|
||||||
create bool *1 = false
|
# If a boolean is true
|
||||||
create var *5 = "Hello"
|
|
||||||
if bool {
|
if bool {
|
||||||
set var = "BASM!"
|
set str = "Hello"
|
||||||
|
}
|
||||||
|
|
||||||
|
# If second byte of var and third byte of var are equal
|
||||||
|
if var[1] == var[2] {
|
||||||
|
set bool = true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
79
src/main.py
79
src/main.py
@@ -9,11 +9,18 @@ varbytes = []
|
|||||||
braces = []
|
braces = []
|
||||||
conditions = []
|
conditions = []
|
||||||
|
|
||||||
|
types = {
|
||||||
|
"int": 2,
|
||||||
|
"string": 3,
|
||||||
|
"bool": 4,
|
||||||
|
}
|
||||||
|
|
||||||
def find_file(filename, root_dir):
|
def find_file(filename, root_dir):
|
||||||
for dirpath, dirnames, filenames in os.walk(root_dir):
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
||||||
if filename in filenames:
|
if filename in filenames:
|
||||||
return os.path.join(dirpath, filename)
|
return os.path.join(dirpath, filename)
|
||||||
return None
|
return None
|
||||||
|
print(var[1])
|
||||||
|
|
||||||
def removeChar(str, char):
|
def removeChar(str, char):
|
||||||
ans = ""
|
ans = ""
|
||||||
@@ -49,6 +56,8 @@ def 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]
|
||||||
|
|
||||||
|
module = i.split(" ")[0]
|
||||||
if (i[0] == "#"):
|
if (i[0] == "#"):
|
||||||
# Skip line if it is a comment
|
# Skip line if it is a comment
|
||||||
pass
|
pass
|
||||||
@@ -74,6 +83,8 @@ for i in file:
|
|||||||
type = "string"
|
type = "string"
|
||||||
elif (a == "true") | (a == "false"):
|
elif (a == "true") | (a == "false"):
|
||||||
type = "bool"
|
type = "bool"
|
||||||
|
elif (a.split("*")[0][:5] == "input"):
|
||||||
|
type = "input"
|
||||||
|
|
||||||
if (type == "string"):
|
if (type == "string"):
|
||||||
a = a.split("\"", 1)[1]
|
a = a.split("\"", 1)[1]
|
||||||
@@ -111,6 +122,24 @@ for i in file:
|
|||||||
|
|
||||||
variables.append(i.split(" ")[1].split("*")[0])
|
variables.append(i.split(" ")[1].split("*")[0])
|
||||||
|
|
||||||
|
elif (module == "input"):
|
||||||
|
var = removeChar(i.split(" ", 1)[1].split("*")[0], " ")
|
||||||
|
bytes = int(removeChar(i.split("*")[1].split("=")[0], " "))
|
||||||
|
variables.append(var)
|
||||||
|
varbytes.append(bytes)
|
||||||
|
|
||||||
|
bfcode += ">>>>>>>>>>>>[[>>]>>]"
|
||||||
|
|
||||||
|
# Get type
|
||||||
|
type = removeChar(i.split("=", 1)[1].split("*")[0], " ")
|
||||||
|
inputbytes = int(removeChar(i.split("*", 2)[2], " "))
|
||||||
|
for j in range(bytes):
|
||||||
|
bfcode += "+" * types[type] + ">"
|
||||||
|
if (j < inputbytes):
|
||||||
|
bfcode += ","
|
||||||
|
bfcode += ">"
|
||||||
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
||||||
|
|
||||||
elif (i.split(" ")[0] == "print"):
|
elif (i.split(" ")[0] == "print"):
|
||||||
a = i.split(" ", 1)[1]
|
a = i.split(" ", 1)[1]
|
||||||
if a[len(a)-1] == "\n":
|
if a[len(a)-1] == "\n":
|
||||||
@@ -162,7 +191,6 @@ for i in file:
|
|||||||
bfcode += "[<<]<<" * (j+1)
|
bfcode += "[<<]<<" * (j+1)
|
||||||
bfcode += "[<]>"
|
bfcode += "[<]>"
|
||||||
|
|
||||||
|
|
||||||
elif (i.split(" ")[0] == "set"):
|
elif (i.split(" ")[0] == "set"):
|
||||||
a = i.split(" ")[1]
|
a = i.split(" ")[1]
|
||||||
if a[len(a)-1] == "\n":
|
if a[len(a)-1] == "\n":
|
||||||
@@ -227,10 +255,12 @@ for i in file:
|
|||||||
|
|
||||||
# Argument must be boolean
|
# Argument must be boolean
|
||||||
if not ('{' in i):
|
if not ('{' in i):
|
||||||
raise SyntaxError("Expected { in if statement")
|
raise SyntaxError(f"Expected {{ in {module} statement")
|
||||||
a = removeChar(i.split(" ", 1)[1].split("{")[0], " ")
|
a = removeChar(i.split(" ", 1)[1].split("{")[0], " ")
|
||||||
|
|
||||||
|
# input is bool
|
||||||
|
bflen = len(bfcode)
|
||||||
if (a in variables):
|
if (a in variables):
|
||||||
bflen = len(bfcode)
|
|
||||||
bfcode += ">>>>>>>>>>>>"
|
bfcode += ">>>>>>>>>>>>"
|
||||||
bfcode += "[>>]>>" * (variables.index(a))
|
bfcode += "[>>]>>" * (variables.index(a))
|
||||||
|
|
||||||
@@ -243,13 +273,52 @@ for i in file:
|
|||||||
|
|
||||||
bfcode += "<<<" + "[<<]<<" * (variables.index(a)+1)
|
bfcode += "<<<" + "[<<]<<" * (variables.index(a)+1)
|
||||||
bfcode += "[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
bfcode += "[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||||
|
elif ("==" in a):
|
||||||
|
# if input is equal
|
||||||
|
arg1 = i.split("if ")[1].split("==")[0].lstrip().rstrip()
|
||||||
|
arg2 = i.split("==", 1)[1].lstrip().rstrip()
|
||||||
|
try:
|
||||||
|
var1 = variables.index(arg1.split("[")[0])
|
||||||
|
except:
|
||||||
|
raise NameError(f"Could not find variable {arg1.split("[")[0]}")
|
||||||
|
idx1 = int(arg1.split("[")[1].split("]")[0])
|
||||||
|
try:
|
||||||
|
var2 = variables.index(arg2.split("[")[0])
|
||||||
|
except:
|
||||||
|
raise NameError(f"Could not find variable {arg2.split("[")[0]}")
|
||||||
|
idx2 = int(arg2.split("[")[1].split("]")[0])
|
||||||
|
# Get first data
|
||||||
|
bfcode += ">>>>>>>>>>>>"
|
||||||
|
bfcode += "[>>]>>" * var1
|
||||||
|
bfcode += ">>" * idx1 + ">"
|
||||||
|
|
||||||
conditions.append(bfcode[-(len(bfcode)-bflen):])
|
# Create Copy
|
||||||
|
bfcode += "[<" + "<<" * (idx1+1) + "+>+>" + ">>" * idx1 + ">-]"
|
||||||
|
bfcode += "<" + "<<" * (idx1+1) + ">[" + ">>" * (idx1+1) + "+" + "<<" * (idx1+1) + "-]"
|
||||||
|
|
||||||
|
# Move copy to start
|
||||||
|
print(var1)
|
||||||
|
bfcode += "<[" + "[<<]<<" * (var1) + "+>>>>" + "[>>]>>" * (var1-1) + ">>" * varbytes[var1-1] + "-]"
|
||||||
|
bfcode += "<<" + "[<<]<<" * (var1) + "[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||||
|
|
||||||
|
# Get second data by copying the above
|
||||||
|
bfcode += ">>>>>>>>>>>>"
|
||||||
|
bfcode += "[>>]>>" * var2
|
||||||
|
bfcode += ">>" * idx2 + ">"
|
||||||
|
bfcode += "[<" + "<<" * (idx2+1) + "+>+>" + ">>" * idx2 + ">-]"
|
||||||
|
bfcode += "<" + "<<" * (idx2+1) + ">[" + ">>" * (idx2+1) + "+" + "<<" * (idx2+1) + "-]"
|
||||||
|
bfcode += "<[" + "[<<]<<" * (var2) + "+>>>>" + "[>>]>>" * (var2-1) + ">>" * varbytes[var2-1] + "-]"
|
||||||
|
bfcode += "<<" + "[<<]<<" * (var2) + "[<<<<<<<+>>>>>>>-]<<<<<<<"
|
||||||
|
|
||||||
|
# Check if they are equal
|
||||||
|
bfcode += "[<->-]+<[>-]>[>]<<[-]>[<+>-]<"
|
||||||
|
|
||||||
bfcode += "[-"
|
|
||||||
else:
|
else:
|
||||||
raise NameError(f"Could not find variable {a}")
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
|
conditions.append(bfcode[-(len(bfcode)-bflen):])
|
||||||
|
bfcode += "[[-]"
|
||||||
|
|
||||||
elif (removeChar(removeChar(i, " "), "\n") == "}"):
|
elif (removeChar(removeChar(i, " "), "\n") == "}"):
|
||||||
if (braces[len(braces)-1] == "if"):
|
if (braces[len(braces)-1] == "if"):
|
||||||
bfcode += "]"
|
bfcode += "]"
|
||||||
|
|||||||
1
test.bf
1
test.bf
@@ -1 +0,0 @@
|
|||||||
>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<
|
|
||||||
@@ -1,14 +1,6 @@
|
|||||||
create bool *1 = true
|
create buffer *1 = true
|
||||||
create hello *5 = "Hello"
|
create var *5 = "Hello"
|
||||||
create bool2 *1 = true
|
input str *5 = string *5
|
||||||
while bool2 {
|
if var[1] == var[2] {
|
||||||
set bool2 = false
|
set buffer = false
|
||||||
if bool {
|
|
||||||
set bool2 = true
|
|
||||||
}
|
|
||||||
if bool2 {
|
|
||||||
set bool = false
|
|
||||||
print hello
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
set bool2 = true
|
|
||||||
Reference in New Issue
Block a user