diff --git a/docs/syntax.md b/docs/syntax.md index b9fd8c9..ab667ae 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -19,16 +19,26 @@ Changes the value of an already created value. 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 Runs code if a boolean is true. Example: ``` -create bool *1 = false -create var *5 = "Hello" +# If a boolean is true 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 } ``` diff --git a/src/main.py b/src/main.py index 577ab07..c99cbb9 100644 --- a/src/main.py +++ b/src/main.py @@ -9,11 +9,18 @@ varbytes = [] braces = [] conditions = [] +types = { + "int": 2, + "string": 3, + "bool": 4, +} + 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 + print(var[1]) def removeChar(str, char): ans = "" @@ -49,6 +56,8 @@ def filewrite(): for i in file: while (i[0] == " "): i = i.split(" ", 1)[1] + + module = i.split(" ")[0] if (i[0] == "#"): # Skip line if it is a comment pass @@ -65,7 +74,7 @@ for i in file: if (a[0] == " "): a = a.split(" ", 1)[1] - # Get type + # Get type try: a = int(a) type = "int" @@ -74,6 +83,8 @@ for i in file: type = "string" elif (a == "true") | (a == "false"): type = "bool" + elif (a.split("*")[0][:5] == "input"): + type = "input" if (type == "string"): a = a.split("\"", 1)[1] @@ -111,6 +122,24 @@ for i in file: 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"): a = i.split(" ", 1)[1] if a[len(a)-1] == "\n": @@ -161,7 +190,6 @@ for i in file: bfcode += "<<<<" bfcode += "[<<]<<" * (j+1) bfcode += "[<]>" - elif (i.split(" ")[0] == "set"): a = i.split(" ")[1] @@ -227,10 +255,12 @@ for i in file: # Argument must be boolean 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], " ") + + # input is bool + bflen = len(bfcode) if (a in variables): - bflen = len(bfcode) bfcode += ">>>>>>>>>>>>" bfcode += "[>>]>>" * (variables.index(a)) @@ -243,12 +273,51 @@ for i in file: bfcode += "<<<" + "[<<]<<" * (variables.index(a)+1) 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: raise NameError(f"Could not find variable {a}") + + conditions.append(bfcode[-(len(bfcode)-bflen):]) + bfcode += "[[-]" elif (removeChar(removeChar(i, " "), "\n") == "}"): if (braces[len(braces)-1] == "if"): diff --git a/test.bf b/test.bf index 3d78594..e69de29 100644 --- a/test.bf +++ b/test.bf @@ -1 +0,0 @@ ->>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<< \ No newline at end of file diff --git a/tests/create.basm b/tests/create.basm index 246beac..21333b3 100644 --- a/tests/create.basm +++ b/tests/create.basm @@ -1,14 +1,6 @@ -create bool *1 = true -create hello *5 = "Hello" -create bool2 *1 = true -while bool2 { - set bool2 = false - if bool { - set bool2 = true - } - if bool2 { - set bool = false - print hello - } -} -set bool2 = true \ No newline at end of file +create buffer *1 = true +create var *5 = "Hello" +input str *5 = string *5 +if var[1] == var[2] { + set buffer = false +} \ No newline at end of file