Add <=, ==, >=, > to parser
This commit is contained in:
parent
5b091638fd
commit
1b3760f4a6
4 changed files with 52 additions and 19 deletions
|
@ -16,4 +16,4 @@ def wavelet(in1, in2, in3, in4, in5, in6, in7, in8):
|
|||
l = (g - h) / 2
|
||||
// third iteration
|
||||
out1 = (i + j) / 2
|
||||
return (k + l) / 2
|
||||
return (k - l) / 2
|
||||
|
|
12
src/absy.rs
12
src/absy.rs
|
@ -191,11 +191,19 @@ impl fmt::Debug for Expression {
|
|||
#[derive(Clone,PartialEq)]
|
||||
pub enum Condition {
|
||||
Lt(Expression, Expression),
|
||||
Le(Expression, Expression),
|
||||
Eq(Expression, Expression),
|
||||
Ge(Expression, Expression),
|
||||
Gt(Expression, Expression),
|
||||
}
|
||||
impl Condition {
|
||||
fn solve(&self, inputs: &mut HashMap<String, i32>) -> bool {
|
||||
match *self {
|
||||
Condition::Lt(ref lhs, ref rhs) => lhs.solve(inputs) < rhs.solve(inputs),
|
||||
Condition::Le(ref lhs, ref rhs) => lhs.solve(inputs) <= rhs.solve(inputs),
|
||||
Condition::Eq(ref lhs, ref rhs) => lhs.solve(inputs) == rhs.solve(inputs),
|
||||
Condition::Ge(ref lhs, ref rhs) => lhs.solve(inputs) >= rhs.solve(inputs),
|
||||
Condition::Gt(ref lhs, ref rhs) => lhs.solve(inputs) > rhs.solve(inputs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -203,6 +211,10 @@ impl fmt::Display for Condition {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Condition::Lt(ref lhs, ref rhs) => write!(f, "{} < {}", lhs, rhs),
|
||||
Condition::Le(ref lhs, ref rhs) => write!(f, "{} <= {}", lhs, rhs),
|
||||
Condition::Eq(ref lhs, ref rhs) => write!(f, "{} == {}", lhs, rhs),
|
||||
Condition::Ge(ref lhs, ref rhs) => write!(f, "{} >= {}", lhs, rhs),
|
||||
Condition::Gt(ref lhs, ref rhs) => write!(f, "{} > {}", lhs, rhs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,8 @@ fn flatten_condition(statements_flattened: &mut Vec<Statement>, num_variables: &
|
|||
|
||||
let cond_true = format!("{}_b{}", &cond_result, bits - 1);
|
||||
VariableReference(cond_true)
|
||||
}
|
||||
},
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -217,30 +217,50 @@ fn next_token(input: &String, pos: &Position) -> (Token, String, Position) {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_then_else(cond: Condition, input: &String, pos: &Position) -> Result<(Expression, String, Position), Error> {
|
||||
match next_token(input, pos) {
|
||||
(Token::Then, s5, p5) => match parse_expr(&s5, &p5) {
|
||||
Ok((e6, s6, p6)) => match next_token(&s6, &p6) {
|
||||
(Token::Else, s7, p7) => match parse_expr(&s7, &p7) {
|
||||
Ok((e8, s8, p8)) => match next_token(&s8, &p8) {
|
||||
(Token::Fi, s9, p9) => parse_expr1(Expression::IfElse(box cond, box e6, box e8), s9, p9),
|
||||
(t10, _, p10) => Err(Error { expected: vec![Token::Fi], got: t10 , pos: p10 }),
|
||||
},
|
||||
err => err,
|
||||
},
|
||||
(t7, _, p7) => Err(Error { expected: vec![Token::Else], got: t7 , pos: p7 }),
|
||||
},
|
||||
err => err,
|
||||
},
|
||||
(t5, _, p5) => Err(Error { expected: vec![Token::Then], got: t5 , pos: p5 }),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_if_then_else(input: &String, pos: &Position) -> Result<(Expression, String, Position), Error> {
|
||||
match next_token(input, pos) {
|
||||
(Token::If, s1, p1) => match parse_expr(&s1, &p1) {
|
||||
Ok((e2, s2, p2)) => match next_token(&s2, &p2) {
|
||||
(Token::Lt, s3, p3) => match parse_expr(&s3, &p3) {
|
||||
Ok((e4, s4, p4)) => match next_token(&s4, &p4) {
|
||||
(Token::Then, s5, p5) => match parse_expr(&s5, &p5) {
|
||||
Ok((e6, s6, p6)) => match next_token(&s6, &p6) {
|
||||
(Token::Else, s7, p7) => match parse_expr(&s7, &p7) {
|
||||
Ok((e8, s8, p8)) => match next_token(&s8, &p8) {
|
||||
(Token::Fi, s9, p9) => parse_expr1(Expression::IfElse(box Condition::Lt(e2, e4), box e6, box e8), s9, p9),
|
||||
(t10, _, p10) => Err(Error { expected: vec![Token::Fi], got: t10 , pos: p10 }),
|
||||
},
|
||||
err => err,
|
||||
},
|
||||
(t7, _, p7) => Err(Error { expected: vec![Token::Else], got: t7 , pos: p7 }),
|
||||
},
|
||||
err => err,
|
||||
},
|
||||
(t5, _, p5) => Err(Error { expected: vec![Token::Then], got: t5 , pos: p5 }),
|
||||
},
|
||||
Ok((e4, s4, p4)) => parse_then_else(Condition::Lt(e2, e4), &s4, &p4),
|
||||
err => err,
|
||||
},
|
||||
_ => unimplemented!()
|
||||
(Token::Le, s3, p3) => match parse_expr(&s3, &p3) {
|
||||
Ok((e4, s4, p4)) => parse_then_else(Condition::Le(e2, e4), &s4, &p4),
|
||||
err => err,
|
||||
},
|
||||
(Token::Eqeq, s3, p3) => match parse_expr(&s3, &p3) {
|
||||
Ok((e4, s4, p4)) => parse_then_else(Condition::Eq(e2, e4), &s4, &p4),
|
||||
err => err,
|
||||
},
|
||||
(Token::Ge, s3, p3) => match parse_expr(&s3, &p3) {
|
||||
Ok((e4, s4, p4)) => parse_then_else(Condition::Ge(e2, e4), &s4, &p4),
|
||||
err => err,
|
||||
},
|
||||
(Token::Gt, s3, p3) => match parse_expr(&s3, &p3) {
|
||||
Ok((e4, s4, p4)) => parse_then_else(Condition::Gt(e2, e4), &s4, &p4),
|
||||
err => err,
|
||||
},
|
||||
(t3, _, p3) => Err(Error { expected: vec![Token::Lt, Token::Le, Token::Eqeq, Token::Ge, Token::Gt], got: t3 , pos: p3 }),
|
||||
},
|
||||
err => err,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue