Skip to content

Commit

Permalink
Fix issue with blank string characters
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Aug 1, 2023
1 parent 5e034e1 commit 0538b86
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ class ExpressionParser(
if (!requireNumber || characterPixel.isNumber || (characterPixel.characterContent == '-' && components.isEmpty())) {
// Grayscale pixel -> character, except for null character (code 0)
// Therefore, the null character is useful to force string initialization or concatenation.
characterPixel.characterContent.takeIf { it.code != 0 }
?.let { components += StringCharacter(it) }
characterPixel.characterContent.let { char ->
components += if (char.code != 0) {
StringCharacter(char)
} else {
StringBlankCharacter
}
}
} else {
reader.error("Member not expected while parsing number.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ data class StringCharacter(val character: Char) : StringComponent
*/
data class StringReference(val sequence: PixelSequence) : StringComponent

/**
* An empty character that serves no purpose to the content of a string,
* but may be useful to force a string initialization or to initialize an empty string.
*/
object StringBlankCharacter : StringComponent

/**
* Expression that wraps a string value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ class KotlinExpressionTranspiler(private val scope: Scope) : ExpressionTranspile
when (it) {
is StringCharacter -> it.character.toString()
is StringReference -> "\${${sequence(it.sequence)}}"
is StringBlankCharacter -> ""
}
} + "\""

override fun number(expression: StringExpression) = expression.components.joinToString("") {
when (it) {
is StringCharacter -> it.character.toString()
is StringReference -> "" // TODO maybe interpet it as a sum between variables?
is StringBlankCharacter -> ""
}
}

Expand Down

0 comments on commit 0538b86

Please sign in to comment.