diff --git a/docs/syntax.md b/docs/syntax.md index 29500cf..44df2f4 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -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. \ No newline at end of file +**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 +``` \ No newline at end of file diff --git a/readme.md b/readme.md index 7974206..cccf0a8 100644 --- a/readme.md +++ b/readme.md @@ -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. \ No newline at end of file +This will create a Brainfuck file named test.bf that contains the compiled code of the 'create' test program. \ No newline at end of file diff --git a/src/codegen.py b/src/codegen.py index 61a82a5..286c079 100644 --- a/src/codegen.py +++ b/src/codegen.py @@ -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() \ No newline at end of file diff --git a/src/error.py b/src/error.py index 1fb7803..fc1d970 100644 --- a/src/error.py +++ b/src/error.py @@ -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}") \ No newline at end of file + print(f"\033[33mWarning: {message}\033[0m") \ No newline at end of file diff --git a/tests/create.basm b/tests/create.basm index c696e3a..884ba13 100644 --- a/tests/create.basm +++ b/tests/create.basm @@ -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 \ No newline at end of file +string str1 13 "Hello, world!" +int var 100000 +inc str1 +print var \ No newline at end of file