c what is this

This commit is contained in:
nora 2023-05-23 20:42:51 +02:00
parent ffd3dad040
commit 54226ad499
11 changed files with 305 additions and 57 deletions

View file

@ -17,7 +17,7 @@ pub enum Atom {
Ident(Ident),
Int(u128),
Float(f64),
String(String),
String(Vec<u8>),
Char(u8),
}
@ -157,8 +157,11 @@ impl Default for IntTySignedness {
}
}
#[derive(Debug, DebugPls, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, DebugPls, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
// N.B: Ord, order matters.
pub enum IntTyKind {
Bool,
Char,
Short,
Int,
Long,
@ -175,13 +178,10 @@ pub struct IntTy {
pub enum TypeSpecifier {
Void,
Char,
SChar,
UChar,
Integer(IntTy),
Float,
Double,
LongDouble,
Bool,
// TODO
// complex
// atomic-type-specifier
@ -294,3 +294,13 @@ impl DirectDeclarator {
}
}
}
impl IntTySignedness {
pub fn signed(self) -> bool {
matches!(self, Self::Signed)
}
pub fn unsigned(self) -> bool {
matches!(self, Self::Unsigned)
}
}

View file

@ -292,8 +292,10 @@ where
let ty = match token {
Tok::Kw(Kw::Void) => TypeSpecifier::Void,
Tok::Kw(Kw::Char) => match signedness {
Some(IntTySignedness::Signed) => TypeSpecifier::SChar,
Some(IntTySignedness::Unsigned) => TypeSpecifier::UChar,
Some(signedness) => TypeSpecifier::Integer(IntTy {
sign: signedness,
kind: IntTyKind::Char,
}),
None => TypeSpecifier::Char,
},
Tok::Kw(Kw::Short) => {
@ -362,7 +364,10 @@ where
}
Tok::Kw(Kw::Float) => TypeSpecifier::Float,
Tok::Kw(Kw::Double) => TypeSpecifier::Double,
Tok::Kw(Kw::Bool) => TypeSpecifier::Bool,
Tok::Kw(Kw::Bool) => TypeSpecifier::Integer(IntTy {
sign: IntTySignedness::Unsigned,
kind: IntTyKind::Bool,
}),
Tok::Kw(Kw::Complex) => {
return Err(ParserError::new(
span,

View file

@ -25,7 +25,9 @@ where
fn get_lhs(&mut self) -> Result<Spanned<Expr>> {
let (typ, span) = match self.peek_t()? {
&(Tok::Ident(ident), span) => (Atom::Ident((Symbol::intern(ident), span)), span),
&(Tok::StringLiteral(literal), span) => (Atom::String(literal.to_string()), span),
&(Tok::StringLiteral(literal), span) => {
(Atom::String(literal.to_string().into_bytes()), span)
}
&(Tok::Constant(Constant::Int(int)), span) => (Atom::Int(int), span),
&(Tok::Constant(Constant::Float(float)), span) => (Atom::Float(float), span),
&(Tok::Constant(Constant::Char(char)), span) => (Atom::Char(char), span),

View file

@ -231,8 +231,6 @@ impl<W: Write> PrettyPrinter<W> {
match spec {
TypeSpecifier::Void => self.string("void"),
TypeSpecifier::Char => self.string("char"),
TypeSpecifier::SChar => self.string("signed char"),
TypeSpecifier::UChar => self.string("unsigned char"),
TypeSpecifier::Integer(int) => {
// prefix the unsignedness if desired
if let IntTySignedness::Unsigned = int.sign {
@ -240,6 +238,8 @@ impl<W: Write> PrettyPrinter<W> {
}
match int.kind {
IntTyKind::Bool => self.string("_Bool"),
IntTyKind::Char => self.string("char"),
IntTyKind::Short => self.string("short"),
IntTyKind::Int => self.string("int"),
IntTyKind::Long => self.string("long"),
@ -249,7 +249,6 @@ impl<W: Write> PrettyPrinter<W> {
TypeSpecifier::Float => self.string("float"),
TypeSpecifier::Double => self.string("double"),
TypeSpecifier::LongDouble => self.string("long double"),
TypeSpecifier::Bool => self.string("_Bool"),
}
}
@ -316,7 +315,11 @@ impl<W: Write> PrettyPrinter<W> {
Atom::String(string) => {
self.string("\"")?;
// bare attempt at escpaing
self.string(&string.replace('\"', "\\\""))?;
self.string(
&std::str::from_utf8(string)
.unwrap_or("<invalid utf-8>")
.replace('\"', "\\\""),
)?;
self.string("\"")?;
Ok(())
}