From 197f0b22f33a965502da4515f8d3cd0e64fa5f77 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 9 Jul 2025 19:17:03 +0300 Subject: [PATCH] Add boolean literals --- reid/examples/reid/fibonacci.reid | 2 +- reid/src/ast/mod.rs | 1 + reid/src/ast/parse.rs | 8 ++++++++ reid/src/ast/process.rs | 1 + reid/src/codegen.rs | 1 + reid/src/lexer.rs | 6 ++++++ reid/src/mir/display.rs | 1 + reid/src/mir/mod.rs | 2 ++ reid/src/mir/typecheck.rs | 5 ++++- 9 files changed, 25 insertions(+), 2 deletions(-) diff --git a/reid/examples/reid/fibonacci.reid b/reid/examples/reid/fibonacci.reid index e092fb4..3567752 100644 --- a/reid/examples/reid/fibonacci.reid +++ b/reid/examples/reid/fibonacci.reid @@ -1,6 +1,6 @@ // Main fn main() -> bool { - return (5 == fibonacci(5)) && (2 == fibonacci(3)); + return (5 == fibonacci(5)) && false; } // Fibonacci diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 8d11539..8739dd8 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -24,6 +24,7 @@ pub enum TypeKind { #[derive(Debug, Clone)] pub enum Literal { Number(u64), + Bool(bool), } #[derive(Debug, Clone)] diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 6bc7a71..a21820f 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -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)?; diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index b5527f5..0bc7734 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -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), } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 717809f..fea3057 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -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!"), }) } diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index c1dd2be..15c5f18 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -22,6 +22,10 @@ pub enum Token { Arrow, /// `if` If, + /// `true` + True, + /// `false` + False, // Symbols /// `;` @@ -165,6 +169,8 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error "return" => Token::ReturnKeyword, "fn" => Token::FnKeyword, "if" => Token::If, + "true" => Token::True, + "false" => Token::False, _ => Token::Identifier(value), }; variant diff --git a/reid/src/mir/display.rs b/reid/src/mir/display.rs index 62c1c6a..8520666 100644 --- a/reid/src/mir/display.rs +++ b/reid/src/mir/display.rs @@ -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), } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 114210b..f48864d 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -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), } } diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index cdf14e1..69f1a2c 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -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)