Print strings

This commit is contained in:
2025-10-28 14:48:11 +11:00
parent 9ad3bdbc92
commit 89065711ce
5 changed files with 58 additions and 12 deletions

View File

@@ -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()