mirror of
https://github.com/Klipper3d/klipper.git
synced 2025-07-07 06:57:38 -06:00
command: Support evaluating C expressions in DECL_CONSTANT()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
b28e95ca1a
commit
7d73a35805
3 changed files with 41 additions and 15 deletions
|
@ -101,19 +101,36 @@ Handlers.append(HandleStaticStrings())
|
|||
# Constants
|
||||
######################################################################
|
||||
|
||||
def decode_integer(value):
|
||||
value = value.strip()
|
||||
if len(value) != 7 or value[0] not in '-+':
|
||||
error("Invalid encoded integer '%s'" % (value,))
|
||||
out = sum([(ord(c) - 48) << (i*6) for i, c in enumerate(value[1:])])
|
||||
if value[0] == '-':
|
||||
out -= 1<<32
|
||||
return out
|
||||
|
||||
# Allow adding build time constants to the data dictionary
|
||||
class HandleConstants:
|
||||
def __init__(self):
|
||||
self.constants = {}
|
||||
self.ctr_dispatch = { '_DECL_CONSTANT': self.decl_constant }
|
||||
self.ctr_dispatch = {
|
||||
'_DECL_CONSTANT': self.decl_constant,
|
||||
'_DECL_CONSTANT_STR': self.decl_constant_str,
|
||||
}
|
||||
def set_value(self, name, value):
|
||||
if name in self.constants and self.constants[name] != value:
|
||||
error("Conflicting definition for constant '%s'" % name)
|
||||
self.constants[name] = value
|
||||
def decl_constant(self, req):
|
||||
name, value = req.split()[1:]
|
||||
self.set_value(name, decode_integer(value))
|
||||
def decl_constant_str(self, req):
|
||||
name, value = req.split()[1:]
|
||||
value = value.strip()
|
||||
if value.startswith('"') and value.endswith('"'):
|
||||
value = value[1:-1]
|
||||
if name in self.constants and self.constants[name] != value:
|
||||
error("Conflicting definition for constant '%s'" % name)
|
||||
self.constants[name] = value
|
||||
self.set_value(name, value)
|
||||
def update_data_dictionary(self, data):
|
||||
data['config'] = self.constants
|
||||
def generate_code(self, options):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue