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

Introduce prepending and appending to a literal define list (+=, =+) #433

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
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
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
Loading