From e89f5e805c8a4e5e7656baf51dfb5b036ba7e369 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 17 May 2026 06:00:26 +0200 Subject: [PATCH] Cleaned up AST --- parser/src/ast/expr.rs | 4 +--- parser/src/ast/statement.rs | 4 ---- parser/src/grammar.pest | 22 +++++++++------------- parser/src/parser/common/expr.rs | 4 +--- parser/src/parser/common/statement.rs | 11 ++--------- src/codegen.rs | 8 ++------ 6 files changed, 15 insertions(+), 38 deletions(-) diff --git a/parser/src/ast/expr.rs b/parser/src/ast/expr.rs index 267da27..9680af9 100644 --- a/parser/src/ast/expr.rs +++ b/parser/src/ast/expr.rs @@ -90,6 +90,4 @@ pub struct ExprPathSegment { } #[derive(Debug, Clone, Serialize)] -pub struct ExprPath { - pub segments: Vec, -} +pub struct ExprPath(pub Vec); diff --git a/parser/src/ast/statement.rs b/parser/src/ast/statement.rs index 3d5c565..8ae23e9 100644 --- a/parser/src/ast/statement.rs +++ b/parser/src/ast/statement.rs @@ -12,10 +12,6 @@ pub enum Statement { VarDecl(VarDeclStmt), Assign { - target: Expression, - value: Expression, - }, - CompoundAssign { target: Expression, compound: String, value: Expression, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 308cbd6..056511f 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -314,7 +314,6 @@ statement = _{ expr_stmt | match_stmt | assign_statement - | compound_assign_statement | if_stmt | while_stmt | c_for_stmt @@ -352,10 +351,6 @@ var_decl_statement = { var_decl ~ ("=" ~ expr)? ~ semicolon } -assign_statement = { - expr ~ "=" ~ expr ~ semicolon -} - increment_statement = { expr ~ "++" ~ semicolon } @@ -365,7 +360,8 @@ decrement_statement = { } compound_assign = { - "+=" + "=" + | "+=" | "-=" | "/=" | "%=" @@ -376,7 +372,7 @@ compound_assign = { | ">>=" } -compound_assign_statement = { +assign_statement = { expr ~ compound_assign ~ expr ~ semicolon } @@ -517,12 +513,12 @@ postfix = { // PREFIX EXPRESSIONS // ------------------------------------------------------ -deref_px = { "*" } -mut_ref_px = { "&" ~ mutable } -ref_px = { "&" } -new_px = { "new" ~ generics? } -not_px = { "!" } -neg_px = { "-" } +deref_px = { "*" } +mut_ref_px = { "&" ~ mutable } +ref_px = { "&" } +new_px = { "new" ~ generics? } +not_px = { "!" } +neg_px = { "-" } prefix = { deref_px diff --git a/parser/src/parser/common/expr.rs b/parser/src/parser/common/expr.rs index 9dbb5c1..94baab8 100644 --- a/parser/src/parser/common/expr.rs +++ b/parser/src/parser/common/expr.rs @@ -193,9 +193,7 @@ impl<'a> TryFrom> for ExprPath { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { ast_ensure!(pair, Rule::expr_path => { - ast_expr!(ExprPath { - segments: collect_recovered(pair.into_inner()), - }) + ast_expr!(ExprPath(collect_recovered(pair.into_inner()))) }) } } diff --git a/parser/src/parser/common/statement.rs b/parser/src/parser/common/statement.rs index 28ebb0f..860ec61 100644 --- a/parser/src/parser/common/statement.rs +++ b/parser/src/parser/common/statement.rs @@ -90,6 +90,8 @@ impl<'a> TryFrom> for Statement { Rule::assign_statement => ast_expr!(Statement::Assign { target: inner.next().unwrap().try_into(), + compound: Ok(inner.next().unwrap().as_str().trim().to_string()) + as AstResult<'_, String>, value: inner.next().unwrap().try_into(), }), Rule::match_stmt => ast_expr!(Statement::Match( @@ -105,15 +107,6 @@ impl<'a> TryFrom> for Statement { .collect::>>(), )), - Rule::compound_assign_statement => { - ast_expr!(Statement::CompoundAssign { - target: inner.next().unwrap().try_into(), - compound: Ok(inner.next().unwrap().as_str().trim().to_string()) - as AstResult<'_, String>, - value: inner.next().unwrap().try_into(), - }) - } - Rule::increment_statement => { ast_expr!(Statement::Increment(inner.next().unwrap().try_into())) } diff --git a/src/codegen.rs b/src/codegen.rs index c9877dc..8ba285b 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -219,7 +219,7 @@ impl GetRust for [Prefix] { impl GetRust for ExprPath { fn get_rust(&self) -> String { - self.segments + self.0 .iter() .map(ExprPathSegment::get_rust) .collect::>() @@ -601,11 +601,7 @@ impl ToRust for Statement { cg.add_indentedln(&format!("let {}{};", decl.get_rust(), init)); } - Statement::Assign { target, value } => { - cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),)); - } - - Statement::CompoundAssign { + Statement::Assign { target, compound, value,