mirror of
https://github.com/dbisu/pico-ducky.git
synced 2026-01-21 09:15:59 +00:00
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:
@@ -189,10 +189,18 @@ def _getCodeBlock(linesIter):
|
|||||||
code.append(line)
|
code.append(line)
|
||||||
return code
|
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):
|
def evaluateExpression(expression):
|
||||||
"""Evaluates an expression with variables and returns the result."""
|
"""Evaluates an expression with variables and returns the result."""
|
||||||
# Replace variables (e.g., $FOO) in the expression with their values
|
expression = replaceVariables(expression)
|
||||||
expression = re.sub(r"\$(\w+)", lambda m: str(variables.get(f"${m.group(1)}", 0)), expression)
|
expression = replaceBooleans(expression) #< Cant use re due its limitation in circutpython
|
||||||
|
print(expression)
|
||||||
|
|
||||||
expression = expression.replace("^", "**") #< Replace ^ with ** for exponentiation
|
expression = expression.replace("^", "**") #< Replace ^ with ** for exponentiation
|
||||||
expression = expression.replace("&&", "and")
|
expression = expression.replace("&&", "and")
|
||||||
@@ -368,6 +376,7 @@ async def parseLine(line, script_lines):
|
|||||||
expression = match.group(2)
|
expression = match.group(2)
|
||||||
value = evaluateExpression(expression)
|
value = evaluateExpression(expression)
|
||||||
variables[varName] = value
|
variables[varName] = value
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise SyntaxError(f"Invalid variable update, declare variable first: {line}")
|
raise SyntaxError(f"Invalid variable update, declare variable first: {line}")
|
||||||
elif line.startswith("DEFINE"):
|
elif line.startswith("DEFINE"):
|
||||||
@@ -390,13 +399,12 @@ async def parseLine(line, script_lines):
|
|||||||
loopCode = list(_getCodeBlock(script_lines))
|
loopCode = list(_getCodeBlock(script_lines))
|
||||||
while evaluateExpression(condition) == True:
|
while evaluateExpression(condition) == True:
|
||||||
currentIterCode = deepcopy(loopCode)
|
currentIterCode = deepcopy(loopCode)
|
||||||
print(loopCode)
|
# print(loopCode)
|
||||||
while currentIterCode:
|
while currentIterCode:
|
||||||
loopLine = currentIterCode.pop(0)
|
loopLine = currentIterCode.pop(0)
|
||||||
currentIterCode = list(parseLine(loopLine, iter(currentIterCode))) #< very inefficient, should be replaced later.
|
currentIterCode = list(parseLine(loopLine, iter(currentIterCode))) #< very inefficient, should be replaced later.
|
||||||
|
|
||||||
elif line.upper().startswith("IF"):
|
elif line.upper().startswith("IF"):
|
||||||
# print("ENTER IF")
|
|
||||||
script_lines, ret = IF(_getIfCondition(line), script_lines).runIf()
|
script_lines, ret = IF(_getIfCondition(line), script_lines).runIf()
|
||||||
print(f"IF returned {ret} code")
|
print(f"IF returned {ret} code")
|
||||||
elif line.upper().startswith("END_IF"):
|
elif line.upper().startswith("END_IF"):
|
||||||
|
|||||||
Reference in New Issue
Block a user