Support c string literals

This commit is contained in:
Lukas Wirth 2023-05-18 11:06:05 +02:00
parent 099b5b3b15
commit 4b577e2bc8
21 changed files with 176 additions and 34 deletions

View file

@ -611,6 +611,7 @@ impl<'a> Printer<'a> {
match literal {
Literal::String(it) => w!(self, "{:?}", it),
Literal::ByteString(it) => w!(self, "\"{}\"", it.escape_ascii()),
Literal::CString(it) => w!(self, "\"{}\\0\"", it),
Literal::Char(it) => w!(self, "'{}'", it.escape_debug()),
Literal::Bool(it) => w!(self, "{}", it),
Literal::Int(i, suffix) => {

View file

@ -85,6 +85,7 @@ impl fmt::Display for FloatTypeWrapper {
pub enum Literal {
String(Box<str>),
ByteString(Box<[u8]>),
CString(Box<str>),
Char(char),
Bool(bool),
Int(i128, Option<BuiltinInt>),
@ -135,6 +136,10 @@ impl From<ast::LiteralKind> for Literal {
let text = s.value().map(Box::from).unwrap_or_else(Default::default);
Literal::String(text)
}
LiteralKind::CString(s) => {
let text = s.value().map(Box::from).unwrap_or_else(Default::default);
Literal::CString(text)
}
LiteralKind::Byte(b) => {
Literal::Uint(b.value().unwrap_or_default() as u128, Some(BuiltinUint::U8))
}