Add boolean literals

This commit is contained in:
Sofia 2025-07-09 19:17:03 +03:00
parent b84672ef8c
commit 197f0b22f3
9 changed files with 25 additions and 2 deletions

View File

@ -1,6 +1,6 @@
// Main
fn main() -> bool {
return (5 == fibonacci(5)) && (2 == fibonacci(3));
return (5 == fibonacci(5)) && false;
}
// Fibonacci

View File

@ -24,6 +24,7 @@ pub enum TypeKind {
#[derive(Debug, Clone)]
pub enum Literal {
Number(u64),
Bool(bool),
}
#[derive(Debug, Clone)]

View File

@ -71,6 +71,14 @@ impl Parse for PrimaryExpression {
Kind::Literal(Literal::Number(v.parse().unwrap())),
stream.get_range().unwrap(),
),
Token::True => Expression(
Kind::Literal(Literal::Bool(true)),
stream.get_range().unwrap(),
),
Token::False => Expression(
Kind::Literal(Literal::Bool(false)),
stream.get_range().unwrap(),
),
Token::ParenOpen => {
let exp = stream.parse()?;
stream.expect(Token::ParenClose)?;

View File

@ -164,6 +164,7 @@ impl ast::Literal {
fn mir(&self) -> mir::Literal {
match *self {
ast::Literal::Number(v) => mir::Literal::Vague(mir::VagueLiteral::Number(v)),
ast::Literal::Bool(v) => mir::Literal::Bool(v),
}
}
}

View File

@ -341,6 +341,7 @@ impl mir::Literal {
mir::Literal::U32(val) => ConstValue::U32(val),
mir::Literal::U64(val) => ConstValue::U64(val),
mir::Literal::U128(val) => ConstValue::U128(val),
mir::Literal::Bool(val) => ConstValue::Bool(val),
mir::Literal::Vague(_) => panic!("Got vague literal!"),
})
}

View File

@ -22,6 +22,10 @@ pub enum Token {
Arrow,
/// `if`
If,
/// `true`
True,
/// `false`
False,
// Symbols
/// `;`
@ -165,6 +169,8 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"return" => Token::ReturnKeyword,
"fn" => Token::FnKeyword,
"if" => Token::If,
"true" => Token::True,
"false" => Token::False,
_ => Token::Identifier(value),
};
variant

View File

@ -166,6 +166,7 @@ impl Display for Literal {
Self::U32(val) => write!(f, "{}u32", val),
Self::U64(val) => write!(f, "{}u64", val),
Self::U128(val) => write!(f, "{}u128", val),
Self::Bool(val) => write!(f, "{}", val),
Self::Vague(val) => val.fmt(f),
}
}

View File

@ -128,6 +128,7 @@ pub enum Literal {
U32(u32),
U64(u64),
U128(u128),
Bool(bool),
Vague(VagueLiteral),
}
@ -149,6 +150,7 @@ impl Literal {
Literal::U32(_) => TypeKind::U32,
Literal::U64(_) => TypeKind::U64,
Literal::U128(_) => TypeKind::U128,
Literal::Bool(_) => TypeKind::Bool,
Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Number),
}
}

View File

@ -15,6 +15,8 @@ pub enum ErrorKind {
TypeIsVague(VagueType),
#[error("Can not coerce {0} to vague type {1}")]
HintIsVague(TypeKind, VagueType),
#[error("Literal {0} can not be coerced to type {1}")]
LiteralIncompatible(Literal, TypeKind),
#[error("Types {0} and {1} are incompatible")]
TypesIncompatible(TypeKind, TypeKind),
#[error("Variable not defined: {0}")]
@ -252,6 +254,7 @@ impl Literal {
(L::U32(_), U32) => self,
(L::U64(_), U64) => self,
(L::U128(_), U128) => self,
(L::Bool(_), Bool) => self,
(L::Vague(VagueL::Number(v)), I8) => L::I8(v as i8),
(L::Vague(VagueL::Number(v)), I16) => L::I16(v as i16),
(L::Vague(VagueL::Number(v)), I32) => L::I32(v as i32),
@ -265,7 +268,7 @@ impl Literal {
// Default type for number literal if unable to find true type.
(L::Vague(VagueL::Number(v)), Vague(Number)) => L::I32(v as i32),
(_, Vague(_)) => self,
_ => Err(ErrorKind::TypesIncompatible(self.as_type(), hint))?,
_ => Err(ErrorKind::LiteralIncompatible(self, hint))?,
})
} else {
Ok(self)