Increment variables

This commit is contained in:
2025-10-30 17:11:10 +11:00
parent 89065711ce
commit 976e9dfaa0
6 changed files with 98 additions and 13 deletions

View File

@@ -17,6 +17,15 @@ bool = {
"false": 0,
}
def getIndex(name: str) -> int:
varnames = []
for var in variables:
varnames.append(var.name)
try:
return varnames.index(name)
except IndexError:
return -1
def generateCode(line: list[str], offset: int) -> str:
keyword = line[0]
if keyword == 'bool':
@@ -98,7 +107,10 @@ def generateCode(line: list[str], offset: int) -> str:
error()
returnval += '>'
try:
input: int = int(line[2]) % 4294967296
input = int(line[2])
if abs(input) > 4294967295:
warn(f'Input {line[2]} is too large for int. Overflowing...')
input %= 4294967296
except ValueError:
error(f'Invalid integer: {line[2]}')
@@ -107,7 +119,10 @@ def generateCode(line: list[str], offset: int) -> str:
values.append(m.floor(input/(256**i)))
input -= values[-1] * (256**i)
for num in values:
returnval += '+' * num + '>'
if num < 128:
returnval += '+' * num + '>'
else:
returnval += '-' * (256-num) + '>'
returnval += '<<<<<'
for var in reversed(variables[:-1]):
if var.bytes >= 0:
@@ -124,13 +139,9 @@ def generateCode(line: list[str], offset: int) -> str:
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])
idx = getIndex(line[1])
if idx != -1:
for var in variables[:idx]:
if var.bytes >= 0:
returnval += '>' * (var.bytes+1)
@@ -161,4 +172,71 @@ def generateCode(line: list[str], offset: int) -> str:
error(f'{line[1]} is not a variable')
return returnval
error()
elif keyword == 'inc':
warn('Increment not correctly implemented yet, do not use this command.')
returnval = '>' * offset + '[>]'
idx = getIndex(line[1])
for var in variables[:idx]:
if var.bytes > 0:
returnval += '>' * (var.bytes + 1)
elif var.bytes == -1:
returnval += '>[>]'
# Increment logic
'''
The logic in pseudo-python is like this:
byte[3]++
carry = 1
if byte[3] != 0:
carry = 0
if carry == 1:
byte[2]++
if byte[2] != 0:
carry = 0
if carry == 1:
byte[1]++
if byte[1] != 0:
carry = 0
if carry == 1:
byte[0]++
carry = 0
'''
# Carry will be stored to the immediate right of cells
# byte[3]++; carry = 1
returnval += '>>>>+>+'
# If byte[3] != 0: carry = 0
returnval += '<[>-<[<<<<+>>>>-]]<<<<[>>>>+<<<<-]>>>>'
# If carry == 1
returnval += '>['
# byte[2]++
returnval += '<<+'
# if byte[2] != 0: carry = 0
returnval += '[>>-<<[<<<+>>>-]]'
returnval += '<<<[>>>+<<<-]>>>'
# if carry == 1
returnval += '>>['
# byte[1]++
returnval += '<<<+'
# if byte[1] != 0: carry = 0
returnval += '[>>>-<<<[<<+>>-]]<<[>>+<<-]>>'
# if carry == 1
returnval += '>>>['
# byte[0]++; carry = 0
returnval += '<<<<+>>>>-]]]'
# Return to start
returnval += '<<<<<'
for var in reversed(variables[:idx]):
print(var.bytes)
if var.bytes >= 0:
returnval += '<' * (var.bytes+1)
elif var.bytes == -1:
returnval += '<[<]'
else:
error()
returnval += '<[<]' + '<' * (offset-1)
return returnval
else:
error(f'Invalid keyword: {line[0]}')

View File

View File

@@ -6,9 +6,9 @@ def preprocess(code: str) -> list[list[str]]:
for i in code:
if i == '\n':
isComment = False
if tokenise[-1] != []:
if token != '':
tokenise[-1].append(token)
if token != '':
tokenise[-1].append(token)
if tokenise[-1]:
tokenise.append([])
token = ''
elif isComment:

View File

View File

@@ -1,4 +1,11 @@
string str1 13 "Hello, world!"
<<<<<<< HEAD
string str2 -1 "Testing dynamic string!"
int var -1
inc var
inc var
=======
int var 100000
inc str1
print var
print var
>>>>>>> 89065711cea711879e4f0bfc53b3da3974590b8c