Variable and expression bugfixes (#325)

* Fix evaluating with Booleans

* replaced re with a function (this feature of re is not supported on circutpython)

* Fixed internalVar in expression + allows for +=, *=, etc in variable math

* Removed added +=, *=, etc operations as they are not in the official language

Removed added +=, *=, etc operations as they are not in the official language. I added them in a previous commit and removed them in this one.

* Fix duplicate lines
This commit is contained in:
Tzur Soffer
2026-01-10 20:00:15 +02:00
committed by GitHub
parent 986c919616
commit fb55875e3c

View File

@@ -189,10 +189,18 @@ def _getCodeBlock(linesIter):
code.append(line)
return code
def replaceBooleans(text): #< fix capitalization mistakes in true and false (for evaluating with booleans)
# Replace any letter-by-letter match for "true" with the proper "True"
text = re.sub(r'[Tt][Rr][Uu][Ee]', 'True', text)
# Replace any letter-by-letter match for "false" with the proper "False"
text = re.sub(r'[Ff][Aa][Ll][Ss][Ee]', 'False', text)
return text
def evaluateExpression(expression):
"""Evaluates an expression with variables and returns the result."""
# Replace variables (e.g., $FOO) in the expression with their values
expression = re.sub(r"\$(\w+)", lambda m: str(variables.get(f"${m.group(1)}", 0)), expression)
expression = replaceVariables(expression)
expression = replaceBooleans(expression) #< Cant use re due its limitation in circutpython
print(expression)
expression = expression.replace("^", "**") #< Replace ^ with ** for exponentiation
expression = expression.replace("&&", "and")
@@ -368,6 +376,7 @@ async def parseLine(line, script_lines):
expression = match.group(2)
value = evaluateExpression(expression)
variables[varName] = value
else:
raise SyntaxError(f"Invalid variable update, declare variable first: {line}")
elif line.startswith("DEFINE"):
@@ -390,13 +399,12 @@ async def parseLine(line, script_lines):
loopCode = list(_getCodeBlock(script_lines))
while evaluateExpression(condition) == True:
currentIterCode = deepcopy(loopCode)
print(loopCode)
# print(loopCode)
while currentIterCode:
loopLine = currentIterCode.pop(0)
currentIterCode = list(parseLine(loopLine, iter(currentIterCode))) #< very inefficient, should be replaced later.
elif line.upper().startswith("IF"):
# print("ENTER IF")
script_lines, ret = IF(_getIfCondition(line), script_lines).runIf()
print(f"IF returned {ret} code")
elif line.upper().startswith("END_IF"):