fix: escape characters within assembly

This commit is contained in:
Gleb Koval 2025-02-25 17:07:21 +00:00
parent 8558733719
commit 5f8b87221c
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
2 changed files with 22 additions and 10 deletions

View File

@ -7,6 +7,7 @@ object asmGenerator {
import microWacc._
import assemblyIR._
import wacc.types._
import lexer.escapedChars
val RAX = Register(RegSize.R64, RegName.AX)
val EAX = Register(RegSize.E32, RegName.AX)
@ -53,7 +54,7 @@ object asmGenerator {
List(
Directive.Int(str.size),
LabelDef(s".L.str$i"),
Directive.Asciz(str.replace("\"", "\\\""))
Directive.Asciz(str.escaped)
)
}
@ -387,4 +388,10 @@ object asmGenerator {
// TODO: Might want to actually properly handle this with the LinkedHashMap too
def align(): AsmLine = And(RSP, ImmediateVal(-16))
}
private val escapedCharsMapping = escapedChars.map { case (k, v) => v -> s"\\$k" }
extension (s: String) {
private def escaped: String =
s.flatMap(c => escapedCharsMapping.getOrElse(c, c.toString))
}
}

View File

@ -39,6 +39,17 @@ val errConfig = new ErrorConfig {
)
}
object lexer {
val escapedChars: Map[String, Int] = Map(
"0" -> '\u0000',
"b" -> '\b',
"t" -> '\t',
"n" -> '\n',
"f" -> '\f',
"r" -> '\r',
"\\" -> '\\',
"'" -> '\'',
"\"" -> '\"'
)
/** Language description for the WACC lexer
*/
@ -63,15 +74,9 @@ object lexer {
textDesc = TextDesc.plain.copy(
graphicCharacter = Basic(c => c >= ' ' && c != '\\' && c != '\'' && c != '"'),
escapeSequences = EscapeDesc.plain.copy(
literals = Set('\\', '"', '\''),
mapping = Map(
"0" -> '\u0000',
"b" -> '\b',
"t" -> '\t',
"n" -> '\n',
"f" -> '\f',
"r" -> '\r'
)
literals =
escapedChars.filter { (s, chr) => chr.toChar.toString == s }.map(_._2.toChar).toSet,
mapping = escapedChars.filter { (s, chr) => chr.toChar.toString != s }
)
),
numericDesc = NumericDesc.plain.copy(