From e180adea8cd0754203b137c4d350350c8e7f9423 Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Mon, 6 Oct 2025 18:33:53 +1100 Subject: [PATCH] Reassign variables with set, bugfixes for strings shorter than allocated size --- docs/syntax.md | 11 ++++++++++- readme.md | 4 +++- src/main.py | 40 ++++++++++++++++++++++++++++++++++++++-- tests/main.basm | 13 +++++++------ tests/main.bf | 1 - 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index eaa654a..8f0d2e6 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -1,9 +1,18 @@ ## create var *bytes = Value Creates the variable "var" with a set number of bytes allocated to it, and initialises its value. +**IMPORTANT:** The number of bytes cannot be modified later. Example: `create var *5 = "Hello"` ## print var -Prints the value of `var` to the console, where `var` is a variable. \ No newline at end of file +Prints the value of `var` to the console, where `var` is a variable. + +Example: `print var` + +## set var = Value + +Changes the value of an already created value. + +Example `set var = "World"` \ No newline at end of file diff --git a/readme.md b/readme.md index 36dc0ee..fabb996 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ ## Welcome to BrainAssembly! -BrainAssembly is a language that compiles to Brainfuck. Similar to Assembly languages, it has simple syntax, and is designed to make Brainfuck easier to write code for. Enjoy! +BrainAssembly is a language that compiles to Brainfuck. Similar to Assembly languages, it has simple syntax, and is designed to make Brainfuck easier to write code for. Heavily inspired by Ground. Enjoy! To run: @@ -14,4 +14,6 @@ git clone https://chookspace.com/DiamondNether90/BrainAssembly python3 src/main.py path/to/file.basm path/to/file.bf ``` +(Replace path/to/file.basm with the actual path to your desired .basm file, and replace path/to/file.bf with where you want the compiled file to be created.) + This will create or replace the contents of path/to/file.bf with the compiled BrainAssembly code. \ No newline at end of file diff --git a/src/main.py b/src/main.py index c20c05f..85d65dd 100644 --- a/src/main.py +++ b/src/main.py @@ -3,6 +3,7 @@ from sys import argv file = open(argv[1]).readlines(); bfcode = "" variables = [] +varbytes = [] def removeChar(str, char): ans = "" for i in str: @@ -29,6 +30,7 @@ for i in file: bfcode += ">>>>>>>>>>>>[[>>]>>]" bytes = int(removeChar(i.split("*", 1)[1].split("=", 1)[0], " ")) + varbytes.append(bytes) a = i.split("=", 1)[1] if (a[0] == " "): @@ -47,7 +49,9 @@ for i in file: bfcode += "+++>" if (j < len(a)): bfcode += "+" * ord(a[j]) + ">" - bfcode += "<[[<<]<<]<<<<<<<<<" + else: + bfcode += ">" + bfcode += "<<[[<<]<<]<<<<<<<<" variables.append(i.split(" ")[1].split("*")[0]) @@ -57,11 +61,43 @@ for i in file: a = removeEnd(a, 1) if (a in variables): bfcode += ">>>>>>>>>>>>" - for i in range(variables.index(a)): + for j in range(variables.index(a)): bfcode += "[>>]>>" bfcode += "[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]" else: raise NameError(f"Could not find variable {a}") + + elif (i.split(" ")[0] == "set"): + a = i.split(" ")[1] + if a[len(a)-1] == "\n": + a = removeEnd(a, 1) + if (a in variables): + bfcode += ">>>>>>>>>>>>" + for j in range(variables.index(a)): + bfcode += "[>>]>>" + for j in range(varbytes[variables.index(a)]): + bfcode += "[-]>[-]>" + for j in range(varbytes[variables.index(a)]): + bfcode += "<<" + # Get type + val = i.split("=", 1)[1].split(" ", 1)[1] + 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" + + if (type == "string"): + for j in range(varbytes[variables.index(a)]): + bfcode += "+++>" + if (j >= len(val)): + bfcode += ">" + else: + bfcode += "+" * ord(val[j]) + ">" + bfcode += "<<[[<<]<<]<<<<<<<<" + else: + raise NameError(f"Could not find variable {a}") elif ((i != "\n") & (i != "")): raise ValueError(f"Invalid Command") diff --git a/tests/main.basm b/tests/main.basm index 5de0898..7449802 100644 --- a/tests/main.basm +++ b/tests/main.basm @@ -1,7 +1,8 @@ -create hello *5 = "Hello, world!" -create world *5 = "World, hello!" -create sigma *6 = "Sigma!" +create str1 *5 = "Hello" +create str2 *10 = "Hello!" -print hello -print world -print sigma \ No newline at end of file +print str1 +print str2 + +set str1 = "World!" +print str1 \ No newline at end of file diff --git a/tests/main.bf b/tests/main.bf index a1756c5..e69de29 100644 --- a/tests/main.bf +++ b/tests/main.bf @@ -1 +0,0 @@ ->>>>>>>>>>>>[[>>]>>]+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><[[<<]<<]<<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><[[<<]<<]<<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++>+++++++++++++++++++++++++++++++++><[[<<]<<]<<<<<<<<<>>>>>>>>>>>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]>>>>>>>>>>>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]>>>>>>>>>>>>[>>]>>[>>]>>[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-] \ No newline at end of file