Prefix parsing
This commit is contained in:
+9
-1
@@ -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<Expression>,
|
||||
prefixes: Vec<PreFix>,
|
||||
postfixes: Vec<Postfix>,
|
||||
},
|
||||
}
|
||||
|
||||
+11
-1
@@ -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*
|
||||
}
|
||||
|
||||
+26
-1
@@ -275,11 +275,24 @@ impl From<pest::iterators::Pair<'_, Rule>> 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<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 {
|
||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||
match pair.as_rule() {
|
||||
|
||||
Reference in New Issue
Block a user