From ea55468a10a23561c3bafb53adf41db13a18f301 Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Fri, 10 Oct 2025 14:19:07 +1100 Subject: [PATCH] While loop --- docs/syntax.md | 12 ++++++++++++ src/main.py | 50 ++++++++++++++++++++++++++++++++++++++--------- test.bf | 2 +- tests/create.basm | 19 +++++++++++++----- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index 57ead0e..b9fd8c9 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -30,4 +30,16 @@ create var *5 = "Hello" 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 +} ``` \ No newline at end of file diff --git a/src/main.py b/src/main.py index ae1542f..577ab07 100644 --- a/src/main.py +++ b/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,13 +177,16 @@ 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)]): bfcode += "+++>" @@ -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 == "}"): - bfcode += "]" + 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() \ No newline at end of file diff --git a/test.bf b/test.bf index 0df9cd8..3d78594 100644 --- a/test.bf +++ b/test.bf @@ -1 +1 @@ ->>>>>>>>>>>>[[>>]>>]++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]><<<<<<<<<<+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<] \ No newline at end of file +>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[-]>[-]><<++++>><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]]>>>>>>>>>>>>[>>]>>[>>]>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>[>>]>>[>>]>>>>>]<<<[<<]<<[<<]<<[<<]<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<]>>>>>>>>>>>>[>>]>>[>>]>>[-]>[-]><<++++>+><<[[<<]<<]<<<<<<<< \ No newline at end of file diff --git a/tests/create.basm b/tests/create.basm index 1a08676..246beac 100644 --- a/tests/create.basm +++ b/tests/create.basm @@ -1,5 +1,14 @@ -create bool *1 = false -create var *5 = "Hello" -if bool { - set var = "BASM!" -} \ No newline at end of file +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