Compare commits
2 Commits
e180adea8c
...
91f090dc12
| Author | SHA1 | Date | |
|---|---|---|---|
| 91f090dc12 | |||
| 3ccf7f845f |
@@ -1,3 +1,5 @@
|
|||||||
|
To create a comment, begin a line with a hash (#) symbol, like python.
|
||||||
|
|
||||||
## create var *bytes = Value
|
## create var *bytes = Value
|
||||||
|
|
||||||
Creates the variable "var" with a set number of bytes allocated to it, and initialises its value.
|
Creates the variable "var" with a set number of bytes allocated to it, and initialises its value.
|
||||||
@@ -15,4 +17,16 @@ Example: `print var`
|
|||||||
|
|
||||||
Changes the value of an already created value.
|
Changes the value of an already created value.
|
||||||
|
|
||||||
Example `set var = "World"`
|
Example: `set var = "World"`
|
||||||
|
|
||||||
|
## if bool
|
||||||
|
|
||||||
|
Runs code if a boolean is true.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```
|
||||||
|
create *1 bool = true
|
||||||
|
if bool {
|
||||||
|
set var = "BASM!"
|
||||||
|
}
|
||||||
|
```
|
||||||
149
src/main.py
149
src/main.py
@@ -1,9 +1,12 @@
|
|||||||
|
from signal import Sigmasks
|
||||||
import string
|
import string
|
||||||
|
import math as m
|
||||||
from sys import argv
|
from sys import argv
|
||||||
file = open(argv[1]).readlines();
|
file = open(argv[1]).readlines();
|
||||||
bfcode = ""
|
bfcode = ""
|
||||||
variables = []
|
variables = []
|
||||||
varbytes = []
|
varbytes = []
|
||||||
|
|
||||||
def removeChar(str, char):
|
def removeChar(str, char):
|
||||||
ans = ""
|
ans = ""
|
||||||
for i in str:
|
for i in str:
|
||||||
@@ -23,8 +26,27 @@ def ord2(char):
|
|||||||
elif (len(char == 0)):
|
elif (len(char == 0)):
|
||||||
return(0)
|
return(0)
|
||||||
|
|
||||||
|
def sign(num):
|
||||||
|
if num < 0:
|
||||||
|
return -1
|
||||||
|
elif num == 0:
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
def filewrite():
|
||||||
|
with open(argv[2], "w") as file:
|
||||||
|
file.write(bfcode)
|
||||||
|
|
||||||
|
filewrite()
|
||||||
for i in file:
|
for i in file:
|
||||||
if (i.split(" ")[0] == "create"):
|
while (i[0] == " "):
|
||||||
|
i = i.split(" ", 1)[1]
|
||||||
|
if (i[0] == "#"):
|
||||||
|
# Skip line if it is a comment
|
||||||
|
pass
|
||||||
|
# Create command
|
||||||
|
elif (i.split(" ")[0] == "create"):
|
||||||
if (i[len(i)-1] == "\n"):
|
if (i[len(i)-1] == "\n"):
|
||||||
i = removeEnd(i, 1)
|
i = removeEnd(i, 1)
|
||||||
bfcode += ">>>>>>>>>>>>[[>>]>>]"
|
bfcode += ">>>>>>>>>>>>[[>>]>>]"
|
||||||
@@ -36,11 +58,15 @@ for i in file:
|
|||||||
if (a[0] == " "):
|
if (a[0] == " "):
|
||||||
a = a.split(" ", 1)[1]
|
a = a.split(" ", 1)[1]
|
||||||
|
|
||||||
# Get type
|
# Get type
|
||||||
if ((a[0] == "\"") & (a[len(a)-1] == "\"")):
|
try:
|
||||||
type = "string"
|
a = int(a)
|
||||||
else:
|
type = "int"
|
||||||
type = "undefined"
|
except ValueError:
|
||||||
|
if ((a[0] == "\"") & (a[len(a)-1] == "\"")):
|
||||||
|
type = "string"
|
||||||
|
elif (a == "true") | (a == "false"):
|
||||||
|
type = "bool"
|
||||||
|
|
||||||
if (type == "string"):
|
if (type == "string"):
|
||||||
a = a.split("\"", 1)[1]
|
a = a.split("\"", 1)[1]
|
||||||
@@ -51,8 +77,31 @@ for i in file:
|
|||||||
bfcode += "+" * ord(a[j]) + ">"
|
bfcode += "+" * ord(a[j]) + ">"
|
||||||
else:
|
else:
|
||||||
bfcode += ">"
|
bfcode += ">"
|
||||||
bfcode += "<<[[<<]<<]<<<<<<<<"
|
elif (type == "int"):
|
||||||
|
tempint = [sign(a)]
|
||||||
|
a = abs(a)
|
||||||
|
tempbytes = m.floor(m.log2(a)/8)
|
||||||
|
a = a % (256 ** (bytes - 1))
|
||||||
|
for j in range(bytes - 1):
|
||||||
|
if (a == 0):
|
||||||
|
modulus = 1
|
||||||
|
else:
|
||||||
|
modulus = 256 ** (bytes - j - 2)
|
||||||
|
tempint.append(m.floor(a / modulus))
|
||||||
|
a -= modulus * m.floor(a/modulus)
|
||||||
|
|
||||||
|
for j in tempint:
|
||||||
|
bfcode += "++>"
|
||||||
|
bfcode += "+" * j + ">"
|
||||||
|
elif type == "bool":
|
||||||
|
if (bytes == 1):
|
||||||
|
bfcode += "++++>" + "+" * (a == "true") + ">"
|
||||||
|
else:
|
||||||
|
raise MemoryError("Booleans can only be made as 1 byte data.")
|
||||||
|
else:
|
||||||
|
raise TypeError("Could not find argument type")
|
||||||
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
||||||
|
|
||||||
variables.append(i.split(" ")[1].split("*")[0])
|
variables.append(i.split(" ")[1].split("*")[0])
|
||||||
|
|
||||||
elif (i.split(" ")[0] == "print"):
|
elif (i.split(" ")[0] == "print"):
|
||||||
@@ -63,9 +112,49 @@ for i in file:
|
|||||||
bfcode += ">>>>>>>>>>>>"
|
bfcode += ">>>>>>>>>>>>"
|
||||||
for j in range(variables.index(a)):
|
for j in range(variables.index(a)):
|
||||||
bfcode += "[>>]>>"
|
bfcode += "[>>]>>"
|
||||||
|
|
||||||
bfcode += "[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]"
|
bfcode += "[>.>]<<[[<<]<<]<<<<<<<<++++++++++.[-]"
|
||||||
else:
|
else:
|
||||||
raise NameError(f"Could not find variable {a}")
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
|
elif (i.split(" ")[0] == "printnum"):
|
||||||
|
varnum = variables.index(i.split(" ", 1)[1])
|
||||||
|
bfcode += ">>>>>>>>>>>>"
|
||||||
|
for j in range(varnum):
|
||||||
|
bfcode += "[>>]>>"
|
||||||
|
for j in range(varbytes[varnum]):
|
||||||
|
# Go to correct cell
|
||||||
|
bfcode += ">>" * j + ">"
|
||||||
|
# Create Copy
|
||||||
|
bfcode += "[" + "<<" * (j+1) + "+<+>" + ">>" * (j+1) + "-]"
|
||||||
|
bfcode += "<<" * j + "<"
|
||||||
|
bfcode += "<<[" + ">>" * (j+1) + ">+" + "<<" * (j+1) + "<-]>"
|
||||||
|
|
||||||
|
# Move copy to end
|
||||||
|
bfcode += "[->"
|
||||||
|
for k in range(len(variables)-varnum+j):
|
||||||
|
bfcode += "[>>]>>"
|
||||||
|
bfcode += "+<<<<"
|
||||||
|
|
||||||
|
for k in range(len(variables)-varnum+j):
|
||||||
|
bfcode += "[<<]<<"
|
||||||
|
bfcode += ">>>]>"
|
||||||
|
|
||||||
|
for k in range(len(variables)-varnum+j):
|
||||||
|
bfcode += "[>>]>>"
|
||||||
|
|
||||||
|
# Split up digits (modulo copied from https://esolangs.org/wiki/Brainfuck_algorithms#Modulo)
|
||||||
|
bfcode += ">>++++++++++<<"
|
||||||
|
bfcode += "[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]" # MODULO
|
||||||
|
bfcode += "++>[-]>[-]>[<<+>>-]>>>++++++++++<<"
|
||||||
|
bfcode += "[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]" # MODULO
|
||||||
|
bfcode += "++<<++>>>[-]>[-]>[<<<<+>>>>-]>[<<<+>>>-]"
|
||||||
|
|
||||||
|
# Return to start
|
||||||
|
bfcode += "<<<<"
|
||||||
|
bfcode += "[<<]<<" * (j+1)
|
||||||
|
bfcode += "[<]>"
|
||||||
|
|
||||||
|
|
||||||
elif (i.split(" ")[0] == "set"):
|
elif (i.split(" ")[0] == "set"):
|
||||||
a = i.split(" ")[1]
|
a = i.split(" ")[1]
|
||||||
@@ -95,12 +184,54 @@ for i in file:
|
|||||||
bfcode += ">"
|
bfcode += ">"
|
||||||
else:
|
else:
|
||||||
bfcode += "+" * ord(val[j]) + ">"
|
bfcode += "+" * ord(val[j]) + ">"
|
||||||
|
elif (type == "int"):
|
||||||
|
bytes = varbytes[variables.index(a)]
|
||||||
|
a = int(val)
|
||||||
|
tempint = [sign(a)]
|
||||||
|
a = abs(a)
|
||||||
|
tempbytes = m.floor(m.log2(a)/8)
|
||||||
|
a = a % (256 ** (bytes - 1))
|
||||||
|
for j in range(bytes - 1):
|
||||||
|
if (a == 0):
|
||||||
|
modulus = 1
|
||||||
|
else:
|
||||||
|
modulus = 256 ** (bytes - j - 2)
|
||||||
|
tempint.append(m.floor(a / modulus))
|
||||||
|
a -= modulus * m.floor(a/modulus)
|
||||||
|
for j in tempint:
|
||||||
|
bfcode += "++>"
|
||||||
|
bfcode += "+" * j + ">"
|
||||||
bfcode += "<<[[<<]<<]<<<<<<<<"
|
bfcode += "<<[[<<]<<]<<<<<<<<"
|
||||||
else:
|
else:
|
||||||
raise NameError(f"Could not find variable {a}")
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
|
elif (i.split(" ")[0] == "if"):
|
||||||
|
# Argument must be boolean
|
||||||
|
if not ('{' in i):
|
||||||
|
raise SyntaxError(f"Expected {{ in if statement")
|
||||||
|
a = removeChar(i.split(" ", 1)[1].split("{")[0], " ")
|
||||||
|
if (a in variables):
|
||||||
|
bfcode += ">>>>>>>>>>>>"
|
||||||
|
bfcode += "[>>]>>" * (variables.index(a))
|
||||||
|
|
||||||
|
bfcode += ">[>+>+<<-]>[<+>-]>[-<<<"
|
||||||
|
bfcode += "[[<<]<<]"
|
||||||
|
bfcode += "+>>>>"
|
||||||
|
bfcode += "[>>]>>" * (variables.index(a))
|
||||||
|
bfcode += ">>>]"
|
||||||
|
|
||||||
|
bfcode += "<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<"
|
||||||
|
|
||||||
|
bfcode += "[-"
|
||||||
|
else:
|
||||||
|
raise NameError(f"Could not find variable {a}")
|
||||||
|
|
||||||
|
elif (i == "}"):
|
||||||
|
bfcode += "]"
|
||||||
|
|
||||||
|
# No command
|
||||||
elif ((i != "\n") & (i != "")):
|
elif ((i != "\n") & (i != "")):
|
||||||
raise ValueError(f"Invalid Command")
|
raise ValueError(f"Invalid Command")
|
||||||
|
|
||||||
with open(argv[2], "w") as file:
|
print("DONE!")
|
||||||
file.write(bfcode)
|
filewrite()
|
||||||
1
test.bf
Normal file
1
test.bf
Normal file
@@ -0,0 +1 @@
|
|||||||
|
>>>>>>>>>>>>[[>>]>>]++++>+><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>[[>>]>>]+++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<>>>>>>>>>>>>>[>+>+<<-]>[<+>-]>[-<<<[[<<]<<]+>>>>>>>]<<<<<<<[<<<<<<<<+>>>>>>>>-]<<<<<<<<[->>>>>>>>>>>>[>>]>>[-]>[-]><<+++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[[<<]<<]<<<<<<<<]
|
||||||
5
tests/create.basm
Normal file
5
tests/create.basm
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
create bool *1 = true
|
||||||
|
create string *1 = "A"
|
||||||
|
if bool {
|
||||||
|
set string = "B"
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
create str1 *5 = "Hello"
|
|
||||||
create str2 *10 = "Hello!"
|
|
||||||
|
|
||||||
print str1
|
|
||||||
print str2
|
|
||||||
|
|
||||||
set str1 = "World!"
|
|
||||||
print str1
|
|
||||||
Reference in New Issue
Block a user