Skip to content
Snippets Groups Projects
Commit edc52ccc authored by Carlos Valencia's avatar Carlos Valencia
Browse files

Better Expr AST

parent 8f3fc620
No related branches found
No related tags found
No related merge requests found
use std::fmt::Debug;
use crate::Parser;
use crate::parser::Context;
......@@ -10,48 +12,76 @@ use crate::parser_impls::whitespace::{Whitespace};
// This version of Expression places the resursive part in ()
pub(crate) struct Expr;
impl Parser<(char, (String, String))> for Expr {
fn parse_fun(&self, input: &str, _ctx: &mut Context) -> Result<((char, (String, String)), String), String> {
pub struct ExprOutput {
left: String,
operator: char,
right: Box<Rest>,
}
#[derive(Debug)]
enum Rest {
I(String),
E(ExprOutput)
}
let parser_plus = Map {
map_a_b_fun: |(plus, w) : (char, char)| {plus},
parser_a: Box::new(Zip {
parser_first: Box::new(Alternative {
parser_first: Box::new(Sat {
sat_fun: |c| {c == '+'}
}),
parser_second: Box::new(Sat {
sat_fun: |c| {c == '-'}
}),
impl Debug for ExprOutput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {} {:#?}", self.left, self.operator, self.right)
}
}
impl Parser<ExprOutput> for Expr {
fn parse_fun(&self, input: &str, _ctx: &mut Context) -> Result<(ExprOutput, String), String> {
let parse_left = Zip {
parser_first: Box::new (Map {
map_a_b_fun: |(num, _w) : (String, char)| num,
parser_a: Box::new(Zip {
parser_first: Box::new(Int),
parser_second: Box::new(Whitespace)
}),
parser_second: Box::new(Whitespace)
}),
parser_second: Box::new(Map {
map_a_b_fun: |(c, w): (char, char)| c,
parser_a: Box::new(Zip {
parser_first: Box::new(Alternative {
parser_first: Box::new(Sat {
sat_fun: |c| c == '+'
}),
parser_second: Box::new(Sat {
sat_fun: |c| c == '-'
})
}),
parser_second: Box::new(Whitespace)
})
})
};
let parser_second_int = Map {
map_a_b_fun: |(_w, c) : (char, String)| {c},
Map {
map_a_b_fun: |((num, c), rest): ((String, char), Rest)| {
ExprOutput{
left: num,
operator: c,
right: Box::new(rest)
}
},
parser_a: Box::new(Zip {
parser_first: Box::new(Whitespace),
parser_first: Box::new(parse_left),
parser_second: Box::new(Alternative {
parser_first: Box::new(Map {
map_a_b_fun: |(c, (sa, sb)): (char, (String, String))| {format!("({} {} {})", c, sa, sb)},
map_a_b_fun: |eo| Rest::E(eo),
parser_a: Box::new(Expr)
}),
parser_second: Box::new(Int)
})
})
};
let parser_ints = Zip {
parser_first: Box::new(Int),
parser_second: Box::new(parser_second_int)
};
parser_second:Box::new(Map {
map_a_b_fun: |eo| Rest::I(eo),
parser_a: Box::new(Int)
})
})
Zip {
parser_first: Box::new(parser_plus),
parser_second: Box::new(parser_ints)
})
}.parse(input)
}
......
......@@ -31,14 +31,16 @@ fn main() {
// let res = Expr.parse(_file.as_str());
// println!("{:#?}", res);
let mut count = 0;
let max = 200000;
// let mut count = 0;
// let max = 200000;
loop {
if (count == max) {break};
Expr.parse(format!("- 221 + {} + 8 11077770", count).as_str());
count += 1
// loop {
// if (count == max) {break};
// println!("{:#?}", Expr.parse(format!("- 221 + {} + 8 11077770", count).as_str()));
// count += 1
}
// }
println!("{:#?}", Expr.parse(format!("221 - {} + 8 + 11077770", 4).as_str()));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment