Print strings
This commit is contained in:
@@ -8,4 +8,12 @@ Create a boolean: `bool name value`
|
||||
|
||||
Create an integer: `int name value`
|
||||
|
||||
**Note**: A string of dynamic length can be created by setting the bytes to -1.
|
||||
**Note**: A string of dynamic length can be created by setting the bytes to -1.
|
||||
|
||||
Print a string: `print name`
|
||||
|
||||
Example program (prints "Hello, World"):
|
||||
```cpp
|
||||
string var 13 "Hello, world!"
|
||||
print var
|
||||
```
|
||||
@@ -33,4 +33,4 @@ Example:
|
||||
python src/main.py tests/create.basm test.bf
|
||||
```
|
||||
|
||||
This will create a Brainfuck file named test.bf that contains the compiled code of our test programs.
|
||||
This will create a Brainfuck file named test.bf that contains the compiled code of the 'create' test program.
|
||||
@@ -1,4 +1,4 @@
|
||||
from error import error
|
||||
from error import error, warn
|
||||
import math as m
|
||||
|
||||
class Variable:
|
||||
@@ -118,5 +118,47 @@ def generateCode(line: list[str], offset: int) -> str:
|
||||
error()
|
||||
returnval += '<[<]' + '<' * (offset-1)
|
||||
|
||||
return returnval
|
||||
elif keyword == 'print':
|
||||
if len(line) != 2:
|
||||
error(f'Print command requires 1 argument (got {len(line)-1})')
|
||||
|
||||
returnval = '>' * offset + '[>]'
|
||||
varnames = []
|
||||
for var in variables:
|
||||
varnames.append(var.name)
|
||||
|
||||
if line[1] in varnames:
|
||||
idx = varnames.index(line[1])
|
||||
|
||||
for var in variables[:idx]:
|
||||
if var.bytes >= 0:
|
||||
returnval += '>' * (var.bytes+1)
|
||||
elif var.bytes == -1:
|
||||
returnval += '>[>]'
|
||||
else:
|
||||
error()
|
||||
|
||||
var = variables[idx]
|
||||
if var.type != 1:
|
||||
error(f'Can\'t print {var.name}: Invalid type')
|
||||
if var.bytes >= 0:
|
||||
returnval += '>' + '.>' * var.bytes + '<' * (var.bytes+1)
|
||||
elif var.bytes == -1:
|
||||
returnval += '>[.>]' + '<[<]'
|
||||
else:
|
||||
error()
|
||||
|
||||
for var in reversed(variables[:idx]):
|
||||
if var.bytes >= 0:
|
||||
returnval += '<' * (var.bytes+1)
|
||||
elif var.bytes == -1:
|
||||
returnval += '<[<]'
|
||||
else:
|
||||
error()
|
||||
returnval += '<[<]' + '<' * (offset-1)
|
||||
else:
|
||||
error(f'{line[1]} is not a variable')
|
||||
|
||||
return returnval
|
||||
error()
|
||||
@@ -2,4 +2,4 @@ def error(message: str = "This is a bugged error message. Please report this iss
|
||||
exit(f"\033[91mError: \033[0m{message}")
|
||||
|
||||
def warn(message: str):
|
||||
print(f"\033[33mWarning: {message}")
|
||||
print(f"\033[33mWarning: {message}\033[0m")
|
||||
@@ -1,8 +1,4 @@
|
||||
bool var true
|
||||
bool var2 false
|
||||
string str1 -1 "Hell"
|
||||
bool var3 true
|
||||
int num1 -1736671613
|
||||
int num2 9182364129
|
||||
string str2 5 "World!"
|
||||
bool var4 true
|
||||
string str1 13 "Hello, world!"
|
||||
int var 100000
|
||||
inc str1
|
||||
print var
|
||||
Reference in New Issue
Block a user