From 5364f51fc1d055868dc133927446cddaa093fc66 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 20:07:20 +0200 Subject: [PATCH] Prefix --- parser/src/ast.rs | 4 ++-- parser/src/lib.rs | 4 ++-- src/codegen.rs | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 5a8fe83..2a58fed 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -73,7 +73,7 @@ pub enum Postfix { } #[derive(Debug, Clone, Serialize)] -pub enum PreFix { +pub enum Prefix { Ref, RefMut, Deref, @@ -144,7 +144,7 @@ pub enum Expression { StringLiteral(String), Fix { initial: Box, - prefixes: Vec, + prefixes: Vec, postfixes: Vec, }, } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index df12de8..22d0183 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -282,7 +282,7 @@ impl From> for Expression { .map(|v| v.as_rule() == Rule::prefix) .unwrap_or_default() { - prefixes.push(PreFix::from(inner.next().unwrap())); + prefixes.push(Prefix::from(inner.next().unwrap())); } println!("{:#?}", prefixes); @@ -326,7 +326,7 @@ impl From> for Expression { } } -impl From> for PreFix { +impl From> for Prefix { fn from(pair: pest::iterators::Pair) -> Self { match pair.as_rule() { Rule::prefix => Self::from(pair.into_inner().next().unwrap()), diff --git a/src/codegen.rs b/src/codegen.rs index 9978ebb..7463650 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,6 +1,6 @@ use parser::ast::{ - BinaryOp, Block, Expression, IfStmt, Postfix, Statement, StaticPath, TopLevel, TypeExpr, - TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, + BinaryOp, Block, Expression, IfStmt, Postfix, Prefix, Statement, StaticPath, TopLevel, + TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, }; // --------------------------------------------------------------------------- @@ -118,11 +118,28 @@ impl GetRust for Expression { initial, prefixes, postfixes, - } => initial.get_rust() + &postfixes.get_rust(), + } => prefixes.get_rust() + &initial.get_rust() + &postfixes.get_rust(), } } } +impl GetRust for Prefix { + fn get_rust(&self) -> String { + match self { + Self::Deref => "*", + Self::Ref => "&", + Self::RefMut => "&mut ", + } + .to_string() + } +} + +impl GetRust for [Prefix] { + fn get_rust(&self) -> String { + self.iter().map(Prefix::get_rust).collect() + } +} + impl GetRust for Postfix { fn get_rust(&self) -> String { match self {