Compare commits
7 Commits
8a7bd651b2
...
v0.0.1
Author | SHA1 | Date | |
---|---|---|---|
9202c839e2
|
|||
7af51e9e64
|
|||
8870cee179
|
|||
9076ecd95d
|
|||
acb6c05eec
|
|||
4e3e3689d0
|
|||
683236f4a8
|
11
src/ast.rs
11
src/ast.rs
@@ -26,6 +26,13 @@ pub struct ForAll
|
||||
pub argument: Box<Formula>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum ImplicationDirection
|
||||
{
|
||||
LeftToRight,
|
||||
RightToLeft,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Formula
|
||||
{
|
||||
@@ -34,7 +41,7 @@ pub enum Formula
|
||||
Not(Box<Formula>),
|
||||
And(Vec<Box<Formula>>),
|
||||
Or(Vec<Box<Formula>>),
|
||||
Implies(Box<Formula>, Box<Formula>),
|
||||
Implies(Box<Formula>, Box<Formula>, ImplicationDirection),
|
||||
Biconditional(Box<Formula>, Box<Formula>),
|
||||
Less(Term, Term),
|
||||
LessOrEqual(Term, Term),
|
||||
@@ -46,7 +53,7 @@ pub enum Formula
|
||||
Predicate(Predicate),
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Eq, Hash, PartialEq)]
|
||||
pub enum Domain
|
||||
{
|
||||
Program,
|
||||
|
135
src/format.rs
135
src/format.rs
@@ -1,30 +1,30 @@
|
||||
struct TermDisplay<'term>
|
||||
struct TermDisplay<'parent>
|
||||
{
|
||||
parent_precedence: u64,
|
||||
term: &'term crate::Term,
|
||||
parent: Option<&'parent crate::Term>,
|
||||
term: &'parent crate::Term,
|
||||
}
|
||||
|
||||
struct FormulaDisplay<'formula>
|
||||
struct FormulaDisplay<'parent>
|
||||
{
|
||||
parent_precedence: u64,
|
||||
formula: &'formula crate::Formula,
|
||||
parent: Option<&'parent crate::Formula>,
|
||||
formula: &'parent crate::Formula,
|
||||
}
|
||||
|
||||
fn display_term<'term>(term: &'term crate::Term, parent_precedence: u64) -> TermDisplay<'term>
|
||||
fn display_term<'parent>(term: &'parent crate::Term, parent: Option<&'parent crate::Term>) -> TermDisplay<'parent>
|
||||
{
|
||||
TermDisplay
|
||||
{
|
||||
parent_precedence,
|
||||
parent,
|
||||
term,
|
||||
}
|
||||
}
|
||||
|
||||
fn display_formula<'formula>(formula: &'formula crate::Formula, parent_precedence: u64)
|
||||
-> FormulaDisplay<'formula>
|
||||
fn display_formula<'parent>(formula: &'parent crate::Formula, parent: Option<&'parent crate::Formula>)
|
||||
-> FormulaDisplay<'parent>
|
||||
{
|
||||
FormulaDisplay
|
||||
{
|
||||
parent_precedence,
|
||||
parent,
|
||||
formula,
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,32 @@ fn term_precedence(term: &crate::Term) -> u64
|
||||
}
|
||||
}
|
||||
|
||||
fn term_requires_parentheses(child: &crate::Term, parent: Option<&crate::Term>) -> bool
|
||||
{
|
||||
match parent
|
||||
{
|
||||
None => false,
|
||||
Some(parent) =>
|
||||
{
|
||||
let child_precedence = term_precedence(child);
|
||||
let parent_precedence = term_precedence(parent);
|
||||
|
||||
if child_precedence != parent_precedence
|
||||
{
|
||||
return child_precedence > parent_precedence;
|
||||
}
|
||||
|
||||
// Subtraction isn’t associative, so handle them separately
|
||||
// TODO: only do this for right-hand side of subtractions
|
||||
match parent
|
||||
{
|
||||
crate::Term::Subtract(_, _) => true,
|
||||
_ => false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn formula_precedence(formula: &crate::Formula) -> u64
|
||||
{
|
||||
match formula
|
||||
@@ -49,21 +75,40 @@ fn formula_precedence(formula: &crate::Formula) -> u64
|
||||
crate::Formula::Not(_) => 2,
|
||||
crate::Formula::And(_) => 3,
|
||||
crate::Formula::Or(_) => 4,
|
||||
crate::Formula::Implies(_, _) => 5,
|
||||
crate::Formula::Implies(_, _, _) => 5,
|
||||
crate::Formula::Biconditional(_, _) => 6,
|
||||
}
|
||||
}
|
||||
|
||||
fn formula_requires_parentheses(child: &crate::Formula, parent: Option<&crate::Formula>) -> bool
|
||||
{
|
||||
match parent
|
||||
{
|
||||
None => false,
|
||||
Some(parent) =>
|
||||
{
|
||||
let child_precedence = formula_precedence(child);
|
||||
let parent_precedence = formula_precedence(parent);
|
||||
|
||||
if child_precedence != parent_precedence
|
||||
{
|
||||
return child_precedence > parent_precedence;
|
||||
}
|
||||
|
||||
// Implications aren’t associative, so handle them separately
|
||||
match parent
|
||||
{
|
||||
crate::Formula::Implies(_, _, _) => true,
|
||||
_ => false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for crate::VariableDeclaration
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
match &self.domain
|
||||
{
|
||||
crate::Domain::Program => write!(format, "X")?,
|
||||
crate::Domain::Integer => write!(format, "N")?,
|
||||
};
|
||||
|
||||
write!(format, "{}", &self.name)
|
||||
}
|
||||
}
|
||||
@@ -80,8 +125,7 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
let precedence = term_precedence(self.term);
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
let requires_parentheses = term_requires_parentheses(self.term, self.parent);
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
@@ -96,10 +140,10 @@ impl<'term> std::fmt::Debug for TermDisplay<'term>
|
||||
crate::Term::Symbolic(ref value) => write!(format, "{}", value),
|
||||
crate::Term::String(ref value) => write!(format, "\"{}\"", value),
|
||||
crate::Term::Variable(ref declaration) => write!(format, "{:?}", declaration),
|
||||
crate::Term::Add(ref left, ref right) => write!(format, "{:?} + {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Subtract(ref left, ref right) => write!(format, "{:?} - {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Multiply(ref left, ref right) => write!(format, "{:?} * {:?}", display_term(left, precedence), display_term(right, precedence)),
|
||||
crate::Term::Negative(ref argument) => write!(format, "-{:?}", display_term(argument, precedence)),
|
||||
crate::Term::Add(ref left, ref right) => write!(format, "{:?} + {:?}", display_term(left, Some(self.term)), display_term(right, Some(self.term))),
|
||||
crate::Term::Subtract(ref left, ref right) => write!(format, "{:?} - {:?}", display_term(left, Some(self.term)), display_term(right, Some(self.term))),
|
||||
crate::Term::Multiply(ref left, ref right) => write!(format, "{:?} * {:?}", display_term(left, Some(self.term)), display_term(right, Some(self.term))),
|
||||
crate::Term::Negative(ref argument) => write!(format, "-{:?}", display_term(argument, Some(self.term))),
|
||||
}?;
|
||||
|
||||
if requires_parentheses
|
||||
@@ -123,8 +167,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
let precedence = formula_precedence(self.formula);
|
||||
let requires_parentheses = precedence > self.parent_precedence;
|
||||
let requires_parentheses = formula_requires_parentheses(self.formula, self.parent);
|
||||
|
||||
if requires_parentheses
|
||||
{
|
||||
@@ -146,7 +189,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " {:?}", display_formula(&exists.argument, precedence))?;
|
||||
write!(format, " {:?}", display_formula(&exists.argument, Some(self.formula)))?;
|
||||
},
|
||||
crate::Formula::ForAll(ref for_all) =>
|
||||
{
|
||||
@@ -161,16 +204,16 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
separator = ", "
|
||||
}
|
||||
|
||||
write!(format, " {:?}", display_formula(&for_all.argument, precedence))?;
|
||||
write!(format, " {:?}", display_formula(&for_all.argument, Some(self.formula)))?;
|
||||
},
|
||||
crate::Formula::Not(ref argument) => write!(format, "not {:?}", display_formula(argument, precedence))?,
|
||||
crate::Formula::Not(ref argument) => write!(format, "not {:?}", display_formula(argument, Some(self.formula)))?,
|
||||
crate::Formula::And(ref arguments) =>
|
||||
{
|
||||
let mut separator = "";
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, Some(self.formula)))?;
|
||||
|
||||
separator = " and "
|
||||
}
|
||||
@@ -181,19 +224,23 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
|
||||
for argument in arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, precedence))?;
|
||||
write!(format, "{}{:?}", separator, display_formula(argument, Some(self.formula)))?;
|
||||
|
||||
separator = " or "
|
||||
}
|
||||
},
|
||||
crate::Formula::Implies(ref left, ref right) => write!(format, "{:?} -> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
||||
crate::Formula::Biconditional(ref left, ref right) => write!(format, "{:?} <-> {:?}", display_formula(left, precedence), display_formula(right, precedence))?,
|
||||
crate::Formula::Less(ref left, ref right) => write!(format, "{:?} < {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::LessOrEqual(ref left, ref right) => write!(format, "{:?} <= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Greater(ref left, ref right) => write!(format, "{:?} > {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::GreaterOrEqual(ref left, ref right) => write!(format, "{:?} >= {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Equal(ref left, ref right) => write!(format, "{:?} = {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::NotEqual(ref left, ref right) => write!(format, "{:?} != {:?}", display_term(left, 1000), display_term(right, 1000))?,
|
||||
crate::Formula::Implies(ref left, ref right, implication_direction) => match implication_direction
|
||||
{
|
||||
crate::ImplicationDirection::LeftToRight => write!(format, "{:?} -> {:?}", display_formula(left, Some(self.formula)), display_formula(right, Some(self.formula)))?,
|
||||
crate::ImplicationDirection::RightToLeft => write!(format, "{:?} <- {:?}", display_formula(left, Some(self.formula)), display_formula(right, Some(self.formula)))?,
|
||||
},
|
||||
crate::Formula::Biconditional(ref left, ref right) => write!(format, "{:?} <-> {:?}", display_formula(left, Some(self.formula)), display_formula(right, Some(self.formula)))?,
|
||||
crate::Formula::Less(ref left, ref right) => write!(format, "{:?} < {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::LessOrEqual(ref left, ref right) => write!(format, "{:?} <= {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Greater(ref left, ref right) => write!(format, "{:?} > {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::GreaterOrEqual(ref left, ref right) => write!(format, "{:?} >= {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Equal(ref left, ref right) => write!(format, "{:?} = {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::NotEqual(ref left, ref right) => write!(format, "{:?} != {:?}", display_term(left, None), display_term(right, None))?,
|
||||
crate::Formula::Boolean(value) =>
|
||||
match value
|
||||
{
|
||||
@@ -212,7 +259,7 @@ impl<'formula> std::fmt::Debug for FormulaDisplay<'formula>
|
||||
|
||||
for argument in &predicate.arguments
|
||||
{
|
||||
write!(format, "{}{:?}", separator, display_term(argument, 1000))?;
|
||||
write!(format, "{}{:?}", separator, display_term(argument, None))?;
|
||||
|
||||
separator = ", "
|
||||
}
|
||||
@@ -243,7 +290,7 @@ impl std::fmt::Debug for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_formula(&self, 1000))
|
||||
write!(format, "{:?}", display_formula(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,7 +298,7 @@ impl std::fmt::Display for crate::Formula
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_formula(&self, 1000))
|
||||
write!(format, "{}", display_formula(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,7 +306,7 @@ impl std::fmt::Debug for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{:?}", display_term(&self, 1000))
|
||||
write!(format, "{:?}", display_term(&self, None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,6 +314,6 @@ impl std::fmt::Display for crate::Term
|
||||
{
|
||||
fn fmt(&self, format: &mut std::fmt::Formatter) -> std::fmt::Result
|
||||
{
|
||||
write!(format, "{}", display_term(&self, 1000))
|
||||
write!(format, "{}", display_term(&self, None))
|
||||
}
|
||||
}
|
||||
|
@@ -2,5 +2,5 @@ mod ast;
|
||||
pub mod format;
|
||||
pub mod parse;
|
||||
|
||||
pub use ast::{Domain, Exists, Formula, ForAll, Predicate, PredicateDeclaration, VariableDeclaration, Term};
|
||||
pub use ast::{Domain, Exists, Formula, ForAll, ImplicationDirection, Predicate, PredicateDeclaration, VariableDeclaration, Term};
|
||||
pub use parse::{formula, formulas, term};
|
||||
|
105
src/parse.rs
105
src/parse.rs
@@ -4,7 +4,7 @@ use nom::
|
||||
bytes::complete::{take_while, take_while_m_n, is_not},
|
||||
character::complete::{digit1, multispace0, space1, not_line_ending, line_ending},
|
||||
sequence::{preceded, delimited, pair, terminated},
|
||||
combinator::{map, map_res},
|
||||
combinator::{map, map_res, recognize},
|
||||
multi::{many0, many0_count, separated_list},
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
@@ -95,7 +95,12 @@ fn is_lowercase_alphanumeric(c: char) -> bool
|
||||
c.is_alphanumeric() && c.is_lowercase()
|
||||
}
|
||||
|
||||
fn symbolic_identifier(i: &str) -> IResult<&str, String>
|
||||
fn is_uppercase_alphanumeric(c: char) -> bool
|
||||
{
|
||||
c.is_alphanumeric() && c.is_uppercase()
|
||||
}
|
||||
|
||||
pub fn symbolic_identifier(i: &str) -> IResult<&str, String>
|
||||
{
|
||||
let (i, symbolic_identifier) = map
|
||||
(
|
||||
@@ -138,17 +143,20 @@ fn string(i: &str) -> IResult<&str, crate::Term>
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn program_variable_identifier(i: &str) -> IResult<&str, String>
|
||||
fn variable_identifier(i: &str) -> IResult<&str, String>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited
|
||||
(
|
||||
whitespace0,
|
||||
preceded
|
||||
recognize
|
||||
(
|
||||
tag("X"),
|
||||
pair
|
||||
(
|
||||
take_while_m_n(1, 1, is_uppercase_alphanumeric),
|
||||
take_while(char::is_alphanumeric)
|
||||
)
|
||||
),
|
||||
whitespace0
|
||||
),
|
||||
@@ -156,76 +164,33 @@ fn program_variable_identifier(i: &str) -> IResult<&str, String>
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn integer_variable_identifier(i: &str) -> IResult<&str, String>
|
||||
{
|
||||
map
|
||||
(
|
||||
delimited
|
||||
(
|
||||
whitespace0,
|
||||
preceded
|
||||
(
|
||||
tag("N"),
|
||||
take_while(char::is_alphanumeric)
|
||||
),
|
||||
whitespace0
|
||||
),
|
||||
|s: &str| s.to_string()
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn program_variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
|
||||
{
|
||||
map
|
||||
(
|
||||
program_variable_identifier,
|
||||
|s| crate::VariableDeclaration{name: s, domain: crate::Domain::Program}
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn integer_variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
|
||||
{
|
||||
map
|
||||
(
|
||||
integer_variable_identifier,
|
||||
|s| crate::VariableDeclaration{name: s, domain: crate::Domain::Integer}
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn variable_declaration(i: &str) -> IResult<&str, crate::VariableDeclaration>
|
||||
{
|
||||
alt
|
||||
((
|
||||
program_variable_declaration,
|
||||
integer_variable_declaration
|
||||
))(i)
|
||||
}
|
||||
|
||||
fn program_variable(i: &str) -> IResult<&str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
program_variable_identifier,
|
||||
|s| crate::Term::Variable(crate::VariableDeclaration{name: s, domain: crate::Domain::Program})
|
||||
)(i)
|
||||
}
|
||||
variable_identifier,
|
||||
|name|
|
||||
{
|
||||
let domain = match name.chars().next()
|
||||
{
|
||||
Some('X') | Some('Y') | Some('Z') => crate::Domain::Program,
|
||||
Some('I') | Some('J') | Some('K') | Some('L') | Some('M') | Some('N') => crate::Domain::Integer,
|
||||
Some(other) => panic!("variable “{}” starts with character “{}”, which is not allowed", name, other),
|
||||
None => panic!("unexpected variable name, please report to bug tracker"),
|
||||
};
|
||||
|
||||
fn integer_variable(i: &str) -> IResult<&str, crate::Term>
|
||||
{
|
||||
map
|
||||
(
|
||||
integer_variable_identifier,
|
||||
|s| crate::Term::Variable(crate::VariableDeclaration{name: s, domain: crate::Domain::Integer})
|
||||
crate::VariableDeclaration{name, domain}
|
||||
}
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn variable(i: &str) -> IResult<&str, crate::Term>
|
||||
{
|
||||
alt
|
||||
((
|
||||
program_variable,
|
||||
integer_variable
|
||||
))(i)
|
||||
map
|
||||
(
|
||||
variable_declaration,
|
||||
|variable_declaration| crate::Term::Variable(variable_declaration)
|
||||
)(i)
|
||||
}
|
||||
|
||||
fn predicate_0_ary(i: &str) -> IResult<&str, crate::Formula>
|
||||
@@ -553,7 +518,7 @@ fn exists(i: &str) -> IResult<&str, crate::Formula>
|
||||
tag(","),
|
||||
variable_declaration
|
||||
),
|
||||
formula_precedence_1
|
||||
formula_precedence_2
|
||||
)
|
||||
),
|
||||
whitespace0
|
||||
@@ -588,7 +553,7 @@ fn for_all(i: &str) -> IResult<&str, crate::Formula>
|
||||
tag(","),
|
||||
variable_declaration
|
||||
),
|
||||
formula_precedence_1
|
||||
formula_precedence_2
|
||||
)
|
||||
),
|
||||
whitespace0
|
||||
@@ -682,8 +647,12 @@ fn formula_precedence_5(i: &str) -> IResult<&str, crate::Formula>
|
||||
|
||||
match preceded(tag("->"), formula_precedence_4)(i)
|
||||
{
|
||||
Ok((i, right)) => Ok((i, crate::Formula::Implies(Box::new(left), Box::new(right)))),
|
||||
Ok((i, right)) => Ok((i, crate::Formula::Implies(Box::new(left), Box::new(right), crate::ImplicationDirection::LeftToRight))),
|
||||
Err(_) => match preceded(tag("<-"), formula_precedence_4)(i)
|
||||
{
|
||||
Ok((i, right)) => Ok((i, crate::Formula::Implies(Box::new(left), Box::new(right), crate::ImplicationDirection::RightToLeft))),
|
||||
Err(_) => Ok((i, left)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user