diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 2dd6699..5a8fe83 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -72,6 +72,13 @@ pub enum Postfix { Binary(BinaryOp, Expression), } +#[derive(Debug, Clone, Serialize)] +pub enum PreFix { + Ref, + RefMut, + Deref, +} + #[derive(Debug, Clone, Serialize)] pub enum Statement { Expression(Expression), @@ -135,8 +142,9 @@ pub enum Expression { FloatLiteral(f64), BoolLiteral(bool), StringLiteral(String), - Postfix { + Fix { initial: Box, + prefixes: Vec, postfixes: Vec, }, } diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index ef05032..b0522c4 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -110,6 +110,16 @@ binary_px = { bin_op ~ expr } bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" } struct_field_decl = { identifier ~ ":" ~ expr } +prefix = { + deref_px + | mut_ref_px + | ref_px +} + +deref_px = { "*" } +mut_ref_px = { "&" ~ mutable } +ref_px = { "&" } + expr = { - primary ~ postfix* + prefix* ~ primary ~ postfix* } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 204dbb7..df12de8 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -275,11 +275,24 @@ impl From> for Expression { match pair.as_rule() { Rule::expr => { let mut inner = pair.into_inner(); + let mut prefixes = Vec::new(); + + while inner + .peek() + .map(|v| v.as_rule() == Rule::prefix) + .unwrap_or_default() + { + prefixes.push(PreFix::from(inner.next().unwrap())); + } + + println!("{:#?}", prefixes); + let exp = Expression::from(inner.next().unwrap()); if inner.len() > 0 { - Expression::Postfix { + Expression::Fix { initial: Box::new(exp), + prefixes, postfixes: inner.map(|p| Postfix::from(p)).collect(), } } else { @@ -313,6 +326,18 @@ impl From> for Expression { } } +impl From> for PreFix { + fn from(pair: pest::iterators::Pair) -> Self { + match pair.as_rule() { + Rule::prefix => Self::from(pair.into_inner().next().unwrap()), + Rule::deref_px => Self::Deref, + Rule::mut_ref_px => Self::RefMut, + Rule::ref_px => Self::Ref, + _ => unimplemented!("{pair:#?}"), + } + } +} + impl From> for Postfix { fn from(pair: pest::iterators::Pair) -> Self { match pair.as_rule() { diff --git a/src/codegen.rs b/src/codegen.rs index 64ade01..825b90d 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -114,7 +114,7 @@ impl GetRust for Expression { Expression::BoolLiteral(b) => b.to_string(), Expression::StringLiteral(s) => format!("\"{}\".to_string()", s), - Expression::Postfix { initial, postfixes } => { + Expression::Fix { initial, postfixes } => { let base = initial.get_rust(); postfixes.get_rust_with_base(&base) }