From 197afbfb35481a4b1a89504995a59920708f16db Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Thu, 21 May 2026 07:23:11 +0200 Subject: [PATCH] bin op stuff --- mist-test/src/main.mist | 2 + parser/src/ast/expr.rs | 26 +-------- parser/src/grammar.pest | 93 +++++++++++--------------------- parser/src/parser/common/expr.rs | 49 ++--------------- src/codegen.rs | 32 ++--------- 5 files changed, 45 insertions(+), 157 deletions(-) diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 9bf7b6a..9e77692 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,6 +1,8 @@ void main() { i32 mut x = 10; + x = 1 + 1; + x++; println!("hello, world {x}"); diff --git a/parser/src/ast/expr.rs b/parser/src/ast/expr.rs index 0ecdaca..77962cb 100644 --- a/parser/src/ast/expr.rs +++ b/parser/src/ast/expr.rs @@ -2,30 +2,6 @@ use serde::Serialize; use super::*; -#[derive(Debug, Clone, Serialize)] -pub enum BinaryOp { - Plus, - Minus, - Multiply, - Divide, - Modulo, - Equal, - NotEqual, - LessThan, - GreaterThan, - LessThanOrEqual, - GreaterThanOrEqual, - And, - Or, - ShiftLeft, - ShiftRight, - RangeInclusive, - RangeExclusive, - BitAnd, - BitOr, - BitXor, -} - #[derive(Debug, Clone, Serialize)] pub enum Expression { Literal(Literal), @@ -38,7 +14,7 @@ pub enum Expression { }, Binary { lhs: Box, - op: BinaryOp, + op: String, rhs: Box, }, } diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 5f25152..e8e4d9b 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -513,23 +513,6 @@ decrement = { "--" } -compound_assign = { - "=" - | "+=" - | "-=" - | "/=" - | "%=" - | "&=" - | "|=" - | "^=" - | "<<=" - | ">>=" -} - -assign = { - compound_assign ~ expr -} - postfix = { field_px | call_px @@ -540,7 +523,6 @@ postfix = { | try_px | increment | decrement - | assign } // ------------------------------------------------------ @@ -564,51 +546,40 @@ prefix = { } // ------------------------------------------------------ -// OPERATORS (Arranged carefully for PEG priority matching) +// OPERATORS // ------------------------------------------------------ -shl = { "<<" } -shr = { ">>" } -range_inc = { "..=" } -range_exc = { ".." } -lte = { "<=" } -gte = { ">=" } -eq = { "==" } -neq = { "!=" } -and = { "&&" } -or = { "||" } -add = { "+" } -sub = { "-" } -mul = { "*" } -div = { "/" } -rem = { "%" } -lt = { "<" } -gt = { ">" } -bitand = { "&" } -bitor = { "|" } -bitxor = { "^" } - -bin_op = _{ - shl - | shr - | range_inc - | range_exc - | lte - | gte - | eq - | neq - | and - | or - | lt - | gt - | add - | sub - | mul - | div - | rem - | bitand - | bitor - | bitxor +bin_op = { + "+=" + | "-=" + | "/=" + | "%=" + | "&=" + | "|=" + | "^=" + | "<<=" + | ">>=" + | "<<" + | ">>" + | "..=" + | ".." + | "<=" + | ">=" + | "==" + | "!=" + | "&&" + | "||" + | "+" + | "-" + | "*" + | "/" + | "%" + | "<" + | ">" + | "&" + | "|" + | "^" + | "=" } // ------------------------------------------------------ diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 107c8e8..06b26dd 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -18,57 +18,18 @@ impl<'a> TryFrom> for Expression { Rule::expr => { static PRATT_PARSER: OnceLock> = OnceLock::new(); let pratt = PRATT_PARSER.get_or_init(|| { - use Rule::*; use pest::pratt_parser::{Assoc::*, Op}; - PrattParser::new() - .op(Op::infix(range_inc, Left) | Op::infix(range_exc, Left)) - .op(Op::infix(or, Left)) - .op(Op::infix(and, Left)) - .op(Op::infix(bitor, Left)) - .op(Op::infix(bitxor, Left)) - .op(Op::infix(bitand, Left)) - .op(Op::infix(eq, Left) | Op::infix(neq, Left)) - .op(Op::infix(lt, Left) - | Op::infix(lte, Left) - | Op::infix(gt, Left) - | Op::infix(gte, Left)) - .op(Op::infix(shl, Left) | Op::infix(shr, Left)) - .op(Op::infix(add, Left) | Op::infix(sub, Left)) - .op(Op::infix(mul, Left) | Op::infix(div, Left) | Op::infix(rem, Left)) + PrattParser::new().op(Op::infix(Rule::bin_op, Left)) }); pratt .map_primary(|primary_pair| Expression::try_from(primary_pair)) - .map_infix(|lhs, op, rhs| { - let bin_op = match op.as_rule() { - Rule::shl => BinaryOp::ShiftLeft, - Rule::shr => BinaryOp::ShiftRight, - Rule::range_inc => BinaryOp::RangeInclusive, - Rule::range_exc => BinaryOp::RangeExclusive, - Rule::lte => BinaryOp::LessThanOrEqual, - Rule::gte => BinaryOp::GreaterThanOrEqual, - Rule::eq => BinaryOp::Equal, - Rule::neq => BinaryOp::NotEqual, - Rule::and => BinaryOp::And, - Rule::or => BinaryOp::Or, - Rule::add => BinaryOp::Plus, - Rule::sub => BinaryOp::Minus, - Rule::mul => BinaryOp::Multiply, - Rule::div => BinaryOp::Divide, - Rule::rem => BinaryOp::Modulo, - Rule::lt => BinaryOp::LessThan, - Rule::gt => BinaryOp::GreaterThan, - Rule::bitand => BinaryOp::BitAnd, - Rule::bitor => BinaryOp::BitOr, - Rule::bitxor => BinaryOp::BitXor, - _ => return AstError::bug_unimplemented(op), - }; - + .map_infix(|expr, op, rhs| { ast_expr!(Expression::Binary { - lhs: lhs.map(Box::new), - op: Ok(bin_op) as AstResult<'_, BinaryOp>, - rhs: rhs.map(Box::new), + lhs: expr.map(Box::new).get_map(Box::new), + op: Ok(op.as_str().to_string()) as AstResult<'_, String>, + rhs: rhs.map(Box::new).get_map(Box::new), }) }) .parse(inner) diff --git a/src/codegen.rs b/src/codegen.rs index 2c8e296..61f4486 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,8 +1,8 @@ use mist_parser::ast::{ - Attribute, BinaryOp, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression, - FieldDecl, FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl, - Literal, Path, Pattern, Postfix, Prefix, Spanned, Statement, StatementBranch, TopLevel, - TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility, + Attribute, Block, ClassItem, EnumItem, ExprPath, ExprPathSegment, Expression, FieldDecl, + FunctionDecl, Generic, GenericDecl, Generics, GenericsDecl, Identifier, ImplDecl, Literal, + Path, Pattern, Postfix, Prefix, Spanned, Statement, StatementBranch, TopLevel, TopLevelKind, + TypeExpr, TypeExprKind, TypePostfix, VarDecl, VarDeclStmt, Visibility, }; // --------------------------------------------------------------------------- @@ -187,29 +187,7 @@ impl GetRust for Expression { } // Safely integrated to handle the tree structure built by the Pratt Parser Expression::Binary { lhs, op, rhs } => { - let op_str = match op { - BinaryOp::Plus => "+", - BinaryOp::Minus => "-", - BinaryOp::Multiply => "*", - BinaryOp::Divide => "/", - BinaryOp::Modulo => "%", - BinaryOp::Equal => "==", - BinaryOp::NotEqual => "!=", - BinaryOp::LessThan => "<", - BinaryOp::GreaterThan => ">", - BinaryOp::LessThanOrEqual => "<=", - BinaryOp::GreaterThanOrEqual => ">=", - BinaryOp::And => "&&", - BinaryOp::Or => "||", - BinaryOp::ShiftLeft => "<<", - BinaryOp::ShiftRight => ">>", - BinaryOp::RangeInclusive => "..=", - BinaryOp::RangeExclusive => "..", - BinaryOp::BitAnd => "&", - BinaryOp::BitOr => "|", - BinaryOp::BitXor => "^", - }; - format!("{} {} {}", lhs.get_rust(), op_str, rhs.get_rust()) + format!("{} {} {}", lhs.get_rust(), op, rhs.get_rust()) } } }