Skip to content

Commit

Permalink
Introduce prepending and appending to a literal define list (+=, =+) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mkruselj authored Jan 27, 2024
1 parent e58deda commit a2f843e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion KSP.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ contexts:
comment: Identifier
scope: variable.source.ksp

- match: ':=|&|<=|>=|<|>|#|=|->|\.\.\.'
- match: '[:+]=|[=][+]|&|<=|>=|<|>|#|=|->|\.\.\.'
comment: Operator
scope: operator.source.ksp

Expand Down
45 changes: 35 additions & 10 deletions compiler/preprocessor_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,8 @@ def substituteValue(self, command, listOfOtherDefines, line=None):

def handleDefineConstants(lines, define_cache = None):
defineRe = r"^define\s+%s\s*(?:\((?P<args>.+)\))?\s*:=(?P<val>.+)$" % variableNameRe
defineAppendRe = r"^define\s+%s\s*(?:\((?P<args>.+)\))?\s*\+=(?P<val>.+)$" % variableNameRe
definePrependRe = r"^define\s+%s\s*(?:\((?P<args>.+)\))?\s*=\+(?P<val>.+)$" % variableNameRe

if define_cache is not None:
defineConstants = define_cache
Expand All @@ -1645,9 +1647,22 @@ def handleDefineConstants(lines, define_cache = None):
newLines.append(l)
continue

define_type = 'none'
m = re.search(defineRe, command)
if m:
define_type = 'new'

if define_type == 'none':
m = re.search(defineAppendRe, command)
if m:
define_type = 'append'

if define_type == 'none':
m = re.search(definePrependRe, command)
if m:
define_type = 'prepend'

if not m:
if define_type == 'none':
newLines.append(l)
continue

Expand All @@ -1660,17 +1675,27 @@ def handleDefineConstants(lines, define_cache = None):
defineConstants.append(defineSizeObj)

# Create define and evaluate if legitimate
defineObj = DefineConstant(m.group("whole"), m.group("val").strip(), m.group("args"), l)

existing = list(filter(lambda d: d.name == m.group("name"), defineConstants))

if len(existing) > 0:
if existing[0].value == m.group("val").strip():
continue
else:
defineObj = None
existing = list(filter(lambda d: d.name == m.group("name"), defineConstants))\

if define_type == 'append' and len(existing) > 0:
# If appending to existing, remove existing and concatente
defineObj = DefineConstant(m.group("whole"), existing[0].value + ', ' + m.group("val").strip(), m.group("args"), l)
defineConstants.remove(existing[0])
elif define_type == 'prepend' and len(existing) > 0:
# If appending to existing, remove existing and concatente
defineObj = DefineConstant(m.group("whole"), m.group("val").strip() + ', ' + existing[0].value, m.group("args"), l)
defineConstants.remove(existing[0])
elif define_type == 'new' and len(existing) > 0:
# If new and exists already, raise Exception
if existing[0].value != m.group("val").strip():
raise ParseException(l, "Define constant was already declared!")
else:
# All other cases, create a new define
defineObj = DefineConstant(m.group("whole"), m.group("val").strip(), m.group("args"), l)

defineConstants.append(defineObj)
if defineObj:
defineConstants.append(defineObj)

if defineConstants:
# Replace all occurences where other defines are used in define values - do it a few times to catch some deeper nested defines.
Expand Down

0 comments on commit a2f843e

Please sign in to comment.