diff --git a/src/codegen.py b/src/codegen.py index 8f04631..0788b93 100644 --- a/src/codegen.py +++ b/src/codegen.py @@ -79,6 +79,8 @@ def generateCode(line: list[str], offset: int) -> str: line[3] = line[3][:(int(line[2])-len(line[3]))] for char in line[3]: returnval += '+' * ord(char) + '>' + for char in range(int(line[2])-len(line[3])): + returnval += '>' else: error(f'Invalid string: {line[3]}') @@ -173,8 +175,14 @@ def generateCode(line: list[str], offset: int) -> str: return returnval elif keyword == 'inc': + if len(line) != 2: + error(f'Inc command requires 1 argument (got {len(line)-1})') + returnval = '>' * offset + '[>]' idx = getIndex(line[1]) + if variables[idx].type != 2: + error(f'Cannot increment {line[1]}: Invalid type') + for var in variables[:idx]: if var.bytes > 0: returnval += '>' * (var.bytes + 1) @@ -236,5 +244,58 @@ def generateCode(line: list[str], offset: int) -> str: returnval += '<[<]' + '<' * (offset-1) return returnval + elif keyword == 'if': + if len(line) != 2: + error(f'If command requires 1 argument (got {len(line)-1})') + + returnval = '>' * offset + '[>]' + idx = getIndex(line[1]) + for var in variables[:idx]: + if var.bytes >= 0: + returnval += '>' * (var.bytes+1) + elif var.bytes == -1: + returnval += '>[>]' + else: + error() + + returnval += '>[<+>->+<]<[>+<-]>>[<<' + for var in reversed(variables[:idx]): + if var.bytes >= 0: + returnval += '<' * (var.bytes+1) + elif var.bytes == -1: + returnval += '<[<]' + else: + error() + + returnval += '<[<]<+>>[>]' + for var in variables[:idx]: + if var.bytes >= 0: + returnval += '>' * (var.bytes+1) + elif var.bytes == -1: + returnval += '>[>]' + else: + error() + + returnval += '>>-]<<' + + for var in reversed(variables[:idx]): + if var.bytes >= 0: + returnval += '<' * (var.bytes+1) + elif var.bytes == -1: + returnval += '<[<]' + else: + error() + + returnval += '<[<]<[' + '<' * (offset-2) + '+' + '>' * (offset-2) + '-]' + '<' * (offset-2) + '[-' + + return returnval + elif keyword == 'end': + if len(line) != 2: + error(f'End command requires 2 arguments (got {len(line)-1})') + + if line[1] == 'if': + return ']' + else: + error(f'Invalid argument for end: {line[1]}') else: error(f'Invalid keyword: {line[0]}') diff --git a/src/preprocess.py b/src/preprocess.py index 5d493cd..87dc253 100644 --- a/src/preprocess.py +++ b/src/preprocess.py @@ -19,7 +19,7 @@ def preprocess(code: str) -> list[list[str]]: token += i if (i == '\"'): isString = not(isString) - else: + elif token != '': tokenise[-1].append(token) token = '' diff --git a/syntax.md b/syntax.md index 4c8cd52..1b4af1f 100644 --- a/syntax.md +++ b/syntax.md @@ -14,6 +14,13 @@ Print a string: `print name` Increment an int: `inc name` +If statement: +```py +if bool + # Code here +end if +``` + Example program (prints "Hello, World"): ```py string var 13 "Hello, world!" # Or -1 bytes for a dynamic string diff --git a/tests/create.basm b/tests/create.basm deleted file mode 100644 index cd900a7..0000000 --- a/tests/create.basm +++ /dev/null @@ -1,5 +0,0 @@ -string str1 13 "Hello, world!" -string str2 -1 "Testing dynamic string!" -int var -1 -inc var -inc var \ No newline at end of file diff --git a/tests/test.basm b/tests/test.basm new file mode 100644 index 0000000..cc659e8 --- /dev/null +++ b/tests/test.basm @@ -0,0 +1,7 @@ +string str1 -1 "Hello, world!" +int num 100 +bool cond true + +if cond + inc num +end if \ No newline at end of file