Prefix parsing

This commit is contained in:
2026-05-04 19:58:34 +02:00
parent c083a547d7
commit ba24db481c
4 changed files with 47 additions and 4 deletions
+9 -1
View File
@@ -72,6 +72,13 @@ pub enum Postfix {
Binary(BinaryOp, Expression), Binary(BinaryOp, Expression),
} }
#[derive(Debug, Clone, Serialize)]
pub enum PreFix {
Ref,
RefMut,
Deref,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Statement { pub enum Statement {
Expression(Expression), Expression(Expression),
@@ -135,8 +142,9 @@ pub enum Expression {
FloatLiteral(f64), FloatLiteral(f64),
BoolLiteral(bool), BoolLiteral(bool),
StringLiteral(String), StringLiteral(String),
Postfix { Fix {
initial: Box<Expression>, initial: Box<Expression>,
prefixes: Vec<PreFix>,
postfixes: Vec<Postfix>, postfixes: Vec<Postfix>,
}, },
} }
+11 -1
View File
@@ -110,6 +110,16 @@ binary_px = { bin_op ~ expr }
bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" } bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" }
struct_field_decl = { identifier ~ ":" ~ expr } struct_field_decl = { identifier ~ ":" ~ expr }
prefix = {
deref_px
| mut_ref_px
| ref_px
}
deref_px = { "*" }
mut_ref_px = { "&" ~ mutable }
ref_px = { "&" }
expr = { expr = {
primary ~ postfix* prefix* ~ primary ~ postfix*
} }
+26 -1
View File
@@ -275,11 +275,24 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
match pair.as_rule() { match pair.as_rule() {
Rule::expr => { Rule::expr => {
let mut inner = pair.into_inner(); 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()); let exp = Expression::from(inner.next().unwrap());
if inner.len() > 0 { if inner.len() > 0 {
Expression::Postfix { Expression::Fix {
initial: Box::new(exp), initial: Box::new(exp),
prefixes,
postfixes: inner.map(|p| Postfix::from(p)).collect(), postfixes: inner.map(|p| Postfix::from(p)).collect(),
} }
} else { } else {
@@ -313,6 +326,18 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for PreFix {
fn from(pair: pest::iterators::Pair<Rule>) -> 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<pest::iterators::Pair<'_, Rule>> for Postfix { impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
fn from(pair: pest::iterators::Pair<Rule>) -> Self { fn from(pair: pest::iterators::Pair<Rule>) -> Self {
match pair.as_rule() { match pair.as_rule() {
+1 -1
View File
@@ -114,7 +114,7 @@ impl GetRust for Expression {
Expression::BoolLiteral(b) => b.to_string(), Expression::BoolLiteral(b) => b.to_string(),
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s), Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
Expression::Postfix { initial, postfixes } => { Expression::Fix { initial, postfixes } => {
let base = initial.get_rust(); let base = initial.get_rust();
postfixes.get_rust_with_base(&base) postfixes.get_rust_with_base(&base)
} }