From d65b0153a1b3505bca8879584777ec59d584c26b Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 15:10:39 +0300 Subject: [PATCH] Implement unary operators as syntax sugar --- README.md | 6 +++--- reid/src/ast/mod.rs | 7 +++++++ reid/src/ast/parse.rs | 19 +++++++++++++++++++ reid/src/ast/process.rs | 18 ++++++++++++++++++ reid/src/mir/linker.rs | 8 ++++---- reid_src/arithmetic.reid | 2 +- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8415810..556c5d6 100644 --- a/README.md +++ b/README.md @@ -38,11 +38,11 @@ Currently missing big features (TODOs) are: - ~~Extern functions~~ (DONE) - ~~Strings~~ (DONE) - ~~Borrows~~ (DONE) -- ~~Pointers~~ +- ~~Pointers~~ (DONE) +- Unary operators +- Floats - Loops - Debug Information (PARTIALLY DONE) -- Floats -- Unary operators - Ability to specify types in literals and variable definitions Big features that I want later but are not necessary: diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 5f50386..bd0aa5e 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -57,6 +57,13 @@ pub enum ExpressionKind { BlockExpr(Box), IfExpr(Box), StructExpression(StructExpression), + UnaryOperation(UnaryOperator, Box), +} + +#[derive(Debug, Clone)] +pub enum UnaryOperator { + Plus, + Minus, } #[derive(Debug, Clone, Copy)] diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 2d26bec..b7dd6b8 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -72,6 +72,20 @@ impl Parse for Expression { } } +impl Parse for UnaryOperator { + fn parse(mut stream: TokenStream) -> Result { + if let Some(token) = stream.next() { + match token { + Token::Plus => Ok(UnaryOperator::Plus), + Token::Minus => Ok(UnaryOperator::Minus), + _ => Err(stream.expected_err("unary operator")?), + } + } else { + Err(stream.expected_err("unary operator")?) + } + } +} + #[derive(Debug)] pub struct PrimaryExpression(Expression); @@ -117,6 +131,11 @@ impl Parse for PrimaryExpression { stream.next(); // Consume Et stream.next(); // Consume identifier Expression(Kind::Deref(name), stream.get_range().unwrap()) + } else if let Ok(unary) = stream.parse() { + Expression( + Kind::UnaryOperation(unary, Box::new(stream.parse()?)), + stream.get_range().unwrap(), + ) } else if let Some(token) = stream.next() { match &token { Token::Identifier(v) => { diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 8bc2d87..dcff4fb 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -238,6 +238,24 @@ impl ast::Expression { name.clone(), self.1.as_meta(module_id), )), + ast::ExpressionKind::UnaryOperation(unary_operator, expr) => match unary_operator { + ast::UnaryOperator::Plus => mir::ExprKind::BinOp( + mir::BinaryOperator::Add, + Box::new(mir::Expression( + mir::ExprKind::Literal(mir::Literal::Vague(mir::VagueLiteral::Number(0))), + expr.1.as_meta(module_id), + )), + Box::new(expr.process(module_id)), + ), + ast::UnaryOperator::Minus => mir::ExprKind::BinOp( + mir::BinaryOperator::Minus, + Box::new(mir::Expression( + mir::ExprKind::Literal(mir::Literal::Vague(mir::VagueLiteral::Number(0))), + expr.1.as_meta(module_id), + )), + Box::new(expr.process(module_id)), + ), + }, }; mir::Expression(kind, self.1.as_meta(module_id)) diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index 287fca2..0dcd38d 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -103,10 +103,10 @@ impl<'map> Pass for LinkerPass<'map> { modules.insert(module.name.clone(), Rc::new(RefCell::new((module, tokens)))); } - // modules.insert( - // "std".to_owned(), - // Rc::new(RefCell::new(compile_std(&mut self.module_map)?)), - // ); + modules.insert( + "std".to_owned(), + Rc::new(RefCell::new(compile_std(&mut self.module_map)?)), + ); let mut modules_to_process: Vec)>>> = modules.values().cloned().collect(); diff --git a/reid_src/arithmetic.reid b/reid_src/arithmetic.reid index cd1c87b..6b012be 100644 --- a/reid_src/arithmetic.reid +++ b/reid_src/arithmetic.reid @@ -5,5 +5,5 @@ fn main() -> u32 { let value = 6; let other = 15; - return value * other + 7 * value; + return value * other + 7 * (-value); }