Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable MATH #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions duckyinpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Author: Dave Bailey (dbisu, @daveisu)
#
# TODO: ADD support for the following:
# MATH: = + - * / % ^
# COMPARISON: == != < > <= >=
# ORDER OF OPERATIONS: ()
# LOGICAL: && ||
Expand Down Expand Up @@ -75,6 +74,15 @@
numbers = "0123456789"
specialChars = "!@#$%^&*()"

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)
# Replace ^ with ** for exponentiation
expression = expression.replace("^", "**")
# Evaluate the expression
return eval(expression, {}, variables)

def convertLine(line):
commands = []
# print(line)
Expand Down Expand Up @@ -115,13 +123,22 @@ def runScriptLine(line):
def sendString(line):
layout.write(line)

def replaceVariables(line):
for var in variables:
line = line.replace(var, str(variables[var]))
return line

def replaceDefines(line):
for define, value in defines.items():
line = line.replace(define, value)
return line

def parseLine(line, script_lines):
global defaultDelay, variables, functions, defines
print(line)
line = line.strip()
line = line.replace("$_RANDOM_INT", str(random.randint(int(variables.get("$_RANDOM_MIN", 0)), int(variables.get("$_RANDOM_MAX", 65535)))))
for define, value in defines.items():
line = line.replace(define, value)
line = replaceDefines(line)
if line[:10] == "INJECT_MOD":
line = line[11:]
elif line.startswith("REM_BLOCK"):
Expand All @@ -147,25 +164,32 @@ def parseLine(line, script_lines):
else:
print(f"Unknown key to RELEASE: <{key}>")
elif(line[0:5] == "DELAY"):
line = replaceVariables(line)
time.sleep(float(line[6:])/1000)
elif line == "STRINGLN": #< stringLN block
line = next(script_lines).strip()
line = replaceVariables(line)
while line.startswith("END_STRINGLN") == False:
sendString(line)
kbd.press(Keycode.ENTER)
kbd.release(Keycode.ENTER)
line = next(script_lines).strip()
line = replaceVariables(line)
line = replaceDefines(line)
elif(line[0:8] == "STRINGLN"):
sendString(line[9:])
sendString(replaceVariables(line[9:]))
kbd.press(Keycode.ENTER)
kbd.release(Keycode.ENTER)
elif line == "STRING": #< string block
line = next(script_lines).strip()
line = replaceVariables(line)
while line.startswith("END_STRING") == False:
sendString(line)
line = next(script_lines).strip()
line = replaceVariables(line)
line = replaceDefines(line)
elif(line[0:6] == "STRING"):
sendString(line[7:])
sendString(replaceVariables(line[7:]))
elif(line[0:5] == "PRINT"):
print("[SCRIPT]: " + line[6:])
elif(line[0:6] == "IMPORT"):
Expand Down Expand Up @@ -204,8 +228,22 @@ def parseLine(line, script_lines):
print("Button 1 pushed")
button_pressed = True
elif line.startswith("VAR"):
_, var, _, value = line.split()
variables[var] = int(value)
match = re.match(r"VAR\s+\$(\w+)\s*=\s*(.+)", line)
if match:
varName = f"${match.group(1)}"
value = evaluateExpression(match.group(2))
variables[varName] = value
else:
raise SyntaxError(f"Invalid variable declaration: {line}")
elif line.startswith("$"):
match = re.match(r"\$(\w+)\s*=\s*(.+)", line)
if match:
varName = f"${match.group(1)}"
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"):
defineLocation = line.find(" ")
valueLocation = line.find(" ", defineLocation + 1)
Expand Down