tuple it up

This commit is contained in:
nora 2023-05-23 21:19:08 +02:00
parent 05c617f6de
commit dba217c18e
6 changed files with 43 additions and 79 deletions

View file

@ -169,10 +169,7 @@ pub enum IntTyKind {
}
#[derive(Debug, DebugPls, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IntTy {
pub sign: IntSign,
pub kind: IntTyKind,
}
pub struct IntTy(pub IntSign, pub IntTyKind);
#[derive(Debug, DebugPls, Clone)]
pub enum TypeSpecifier {

View file

@ -4,7 +4,7 @@ use peekmore::PeekMoreIterator;
use crate::{
ast::{
Decl, DeclAttr, DeclSpec, Declarator, DirectDeclarator, ExternalDecl, FunctionDef,
FunctionParamDecl, Ident, InitDecl, IntTy, IntTyKind, IntSign, NormalDecl, Stmt,
FunctionParamDecl, Ident, InitDecl, IntSign, IntTy, IntTyKind, NormalDecl, Stmt,
TranslationUnit, TypeSpecifier,
},
pre::Punctuator as P,
@ -292,36 +292,29 @@ where
let ty = match token {
Tok::Kw(Kw::Void) => TypeSpecifier::Void,
Tok::Kw(Kw::Char) => match signedness {
Some(signedness) => TypeSpecifier::Integer(IntTy {
sign: signedness,
kind: IntTyKind::Char,
}),
Some(signedness) => TypeSpecifier::Integer(IntTy(signedness, IntTyKind::Char)),
None => TypeSpecifier::Char,
},
Tok::Kw(Kw::Short) => {
eat!(self, Tok::Kw(Kw::Int));
TypeSpecifier::Integer(IntTy {
sign: signedness.unwrap_or_default(),
kind: IntTyKind::Short,
})
TypeSpecifier::Integer(IntTy(signedness.unwrap_or_default(), IntTyKind::Short))
}
Tok::Kw(Kw::Int) => {
TypeSpecifier::Integer(IntTy(signedness.unwrap_or_default(), IntTyKind::Int))
}
Tok::Kw(Kw::Int) => TypeSpecifier::Integer(IntTy {
sign: signedness.unwrap_or_default(),
kind: IntTyKind::Int,
}),
Tok::Kw(Kw::Long) => {
if eat!(self, Tok::Kw(Kw::Long)).is_some() {
eat!(self, Tok::Kw(Kw::Int));
TypeSpecifier::Integer(IntTy {
sign: signedness.unwrap_or_default(),
kind: IntTyKind::LongLong,
})
TypeSpecifier::Integer(IntTy(
signedness.unwrap_or_default(),
IntTyKind::LongLong,
))
} else {
eat!(self, Tok::Kw(Kw::Int));
TypeSpecifier::Integer(IntTy {
sign: signedness.unwrap_or_default(),
kind: IntTyKind::Long,
})
TypeSpecifier::Integer(IntTy(
signedness.unwrap_or_default(),
IntTyKind::Long,
))
}
}
Tok::Kw(Kw::Signed) => {
@ -338,10 +331,7 @@ where
signedness = Some(IntSign::Signed);
continue;
}
TypeSpecifier::Integer(IntTy {
sign: IntSign::Signed,
kind: IntTyKind::Int,
})
TypeSpecifier::Integer(IntTy(IntSign::Signed, IntTyKind::Int))
}
Tok::Kw(Kw::Unsigned) => {
if signedness.is_some() {
@ -357,17 +347,13 @@ where
signedness = Some(IntSign::Unsigned);
continue;
}
TypeSpecifier::Integer(IntTy {
sign: IntSign::Unsigned,
kind: IntTyKind::Int,
})
TypeSpecifier::Integer(IntTy(IntSign::Unsigned, IntTyKind::Int))
}
Tok::Kw(Kw::Float) => TypeSpecifier::Float,
Tok::Kw(Kw::Double) => TypeSpecifier::Double,
Tok::Kw(Kw::Bool) => TypeSpecifier::Integer(IntTy {
sign: IntSign::Unsigned,
kind: IntTyKind::Bool,
}),
Tok::Kw(Kw::Bool) => {
TypeSpecifier::Integer(IntTy(IntSign::Unsigned, IntTyKind::Bool))
}
Tok::Kw(Kw::Complex) => {
return Err(ParserError::new(
span,

View file

@ -6,7 +6,7 @@ use crate::{
ast::{
ArithOpKind, Atom, BinaryOp, ComparisonKind, Decl, DeclAttr, DeclSpec, Declarator,
DirectDeclarator, Expr, ExprBinary, ExprPostfix, ExprUnary, ExternalDecl, FunctionDef,
FunctionParamDecl, InitDecl, IntTyKind, IntSign, NormalDecl, PostfixOp, Stmt,
FunctionParamDecl, InitDecl, IntSign, IntTyKind, NormalDecl, PostfixOp, Stmt,
TypeSpecifier, UnaryOp,
},
sym::Symbol,
@ -233,11 +233,11 @@ impl<W: Write> PrettyPrinter<W> {
TypeSpecifier::Char => self.string("char"),
TypeSpecifier::Integer(int) => {
// prefix the unsignedness if desired
if let IntSign::Unsigned = int.sign {
if let IntSign::Unsigned = int.0 {
self.string("unsigned ")?;
}
match int.kind {
match int.1 {
IntTyKind::Bool => self.string("_Bool"),
IntTyKind::Char => self.string("char"),
IntTyKind::Short => self.string("short"),