Compare commits
2 Commits
operators
...
2c6d14234c
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c6d14234c | |||
| 66e1659c5b |
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -1,4 +0,0 @@
|
||||
[submodule "winnow"]
|
||||
path = winnow
|
||||
url = git@github.com:supersurviveur/winnow.git
|
||||
branch = operator-in-expression
|
||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -323,6 +323,8 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
@ -7,6 +7,6 @@ edition = "2024"
|
||||
bimap = "0.6.3"
|
||||
env_logger = "0.11.8"
|
||||
log = "0.4.29"
|
||||
winnow = { version = "0.7.14", path = "./winnow" }
|
||||
lru = "0.16.3"
|
||||
owo-colors = "4.2.3"
|
||||
winnow = "0.7.14"
|
||||
|
||||
1
doz/.gitignore
vendored
Normal file
1
doz/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
7
doz/Cargo.lock
generated
Normal file
7
doz/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "doz"
|
||||
version = "0.1.0"
|
||||
6
doz/Cargo.toml
Normal file
6
doz/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "doz"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
254
doz/src/domain.rs
Normal file
254
doz/src/domain.rs
Normal file
@ -0,0 +1,254 @@
|
||||
pub mod simple_range;
|
||||
pub mod union;
|
||||
|
||||
use std::fmt::Display;
|
||||
use std::ops::Range;
|
||||
use std::ops::RangeFrom;
|
||||
use std::ops::RangeTo;
|
||||
|
||||
pub struct Domain
|
||||
{
|
||||
pub ranges: Vec<DomainRange>,
|
||||
}
|
||||
|
||||
/// Represents the set
|
||||
/// $$\{x \in [\text{start}; \text{end}[ | x \equiv \text{congruent} \text{mod} \text{modulo}}$$
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct DomainRange
|
||||
{
|
||||
pub start: LowerBound,
|
||||
pub end: HigherBound,
|
||||
|
||||
pub modulo: u64,
|
||||
pub congruent: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct SimpleRange
|
||||
{
|
||||
pub start: LowerBound,
|
||||
pub end: HigherBound,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum LowerBound
|
||||
{
|
||||
Infinity,
|
||||
Bounded(i64),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum HigherBound
|
||||
{
|
||||
Infinity,
|
||||
Bounded(i64),
|
||||
}
|
||||
|
||||
pub enum UnionResult<T>
|
||||
{
|
||||
Single(T),
|
||||
Union(T, T),
|
||||
}
|
||||
|
||||
impl Domain
|
||||
{
|
||||
pub fn empty(&self) -> Domain
|
||||
{
|
||||
Domain { ranges: vec![] }
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool
|
||||
{
|
||||
self.ranges.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl DomainRange
|
||||
{
|
||||
pub fn range(from: LowerBound, to: HigherBound) -> DomainRange
|
||||
{
|
||||
DomainRange {
|
||||
start: from,
|
||||
end: to,
|
||||
modulo: 1,
|
||||
congruent: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_simple(&self) -> Option<SimpleRange>
|
||||
{
|
||||
if self.modulo <= 1
|
||||
{
|
||||
Some(SimpleRange {
|
||||
start: self.start,
|
||||
end: self.end,
|
||||
})
|
||||
}
|
||||
else
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Range<i64>> for Domain
|
||||
{
|
||||
fn from(value: Range<i64>) -> Self
|
||||
{
|
||||
Domain {
|
||||
ranges: vec![value.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeTo<i64>> for Domain
|
||||
{
|
||||
fn from(value: RangeTo<i64>) -> Self
|
||||
{
|
||||
Domain {
|
||||
ranges: vec![value.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeFrom<i64>> for Domain
|
||||
{
|
||||
fn from(value: RangeFrom<i64>) -> Self
|
||||
{
|
||||
Domain {
|
||||
ranges: vec![value.into()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Range<i64>> for DomainRange
|
||||
{
|
||||
fn from(value: Range<i64>) -> Self
|
||||
{
|
||||
DomainRange::range(
|
||||
LowerBound::Bounded(value.start),
|
||||
HigherBound::Bounded(value.end),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeTo<i64>> for DomainRange
|
||||
{
|
||||
fn from(value: RangeTo<i64>) -> Self
|
||||
{
|
||||
DomainRange::range(LowerBound::Infinity, HigherBound::Bounded(value.end))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeFrom<i64>> for DomainRange
|
||||
{
|
||||
fn from(value: RangeFrom<i64>) -> Self
|
||||
{
|
||||
DomainRange::range(LowerBound::Bounded(value.start), HigherBound::Infinity)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for LowerBound
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
match self
|
||||
{
|
||||
LowerBound::Infinity => write!(f, "-∞"),
|
||||
LowerBound::Bounded(x) => write!(f, "{}", x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for HigherBound
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
match self
|
||||
{
|
||||
HigherBound::Infinity => write!(f, "+∞"),
|
||||
HigherBound::Bounded(x) => write!(f, "{}", x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for DomainRange
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
write!(f, "{}..{}", self.start, self.end)?;
|
||||
if self.modulo > 1
|
||||
{
|
||||
write!(f, " ≡ {} [{}]", self.congruent, self.modulo)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Domain
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
if self.is_empty()
|
||||
{
|
||||
write!(f, "Ø")
|
||||
}
|
||||
else
|
||||
{
|
||||
let len = self.ranges.len();
|
||||
for (i, r) in self.ranges.iter().enumerate()
|
||||
{
|
||||
write!(f, "{}", r)?;
|
||||
if i != len - 1
|
||||
{
|
||||
write!(f, " ∪ ")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for LowerBound
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>
|
||||
{
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for LowerBound
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering
|
||||
{
|
||||
match (self, other)
|
||||
{
|
||||
(LowerBound::Infinity, LowerBound::Infinity) => std::cmp::Ordering::Equal,
|
||||
(LowerBound::Infinity, LowerBound::Bounded(_)) => std::cmp::Ordering::Less,
|
||||
(LowerBound::Bounded(_), LowerBound::Infinity) => std::cmp::Ordering::Greater,
|
||||
(LowerBound::Bounded(a), LowerBound::Bounded(b)) => a.cmp(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for HigherBound
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering>
|
||||
{
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for HigherBound
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering
|
||||
{
|
||||
match (self, other)
|
||||
{
|
||||
(HigherBound::Infinity, HigherBound::Infinity) => std::cmp::Ordering::Equal,
|
||||
(HigherBound::Infinity, HigherBound::Bounded(_)) => std::cmp::Ordering::Greater,
|
||||
(HigherBound::Bounded(_), HigherBound::Infinity) => std::cmp::Ordering::Less,
|
||||
(HigherBound::Bounded(a), HigherBound::Bounded(b)) => a.cmp(b),
|
||||
}
|
||||
}
|
||||
}
|
||||
153
doz/src/domain/simple_range.rs
Normal file
153
doz/src/domain/simple_range.rs
Normal file
@ -0,0 +1,153 @@
|
||||
use std::ops::Range;
|
||||
use std::ops::RangeFrom;
|
||||
use std::ops::RangeFull;
|
||||
use std::ops::RangeTo;
|
||||
|
||||
use crate::domain::HigherBound;
|
||||
use crate::domain::LowerBound;
|
||||
use crate::domain::SimpleRange;
|
||||
use crate::domain::UnionResult;
|
||||
|
||||
impl SimpleRange
|
||||
{
|
||||
pub fn union(&self, range: &SimpleRange) -> UnionResult<SimpleRange>
|
||||
{
|
||||
let (a, b) = (self.min_by_start(range), self.max_by_start(range));
|
||||
match (a.end, b.start)
|
||||
{
|
||||
(HigherBound::Infinity, LowerBound::Infinity) =>
|
||||
{
|
||||
UnionResult::Single(SimpleRange::from(..))
|
||||
}
|
||||
(HigherBound::Infinity, LowerBound::Bounded(_)) => UnionResult::Single(SimpleRange {
|
||||
start: a.start,
|
||||
end: HigherBound::Infinity,
|
||||
}),
|
||||
(HigherBound::Bounded(_), LowerBound::Infinity) => UnionResult::Single(SimpleRange {
|
||||
start: LowerBound::Infinity,
|
||||
end: b.end,
|
||||
}),
|
||||
(HigherBound::Bounded(c), LowerBound::Bounded(d)) if c < d => UnionResult::Union(a, b),
|
||||
(HigherBound::Bounded(_), LowerBound::Bounded(_)) => UnionResult::Single(SimpleRange {
|
||||
start: a.start,
|
||||
end: b.end,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn intersection(&self, range: &SimpleRange) -> Option<SimpleRange>
|
||||
{
|
||||
let (a, b) = (self.min_by_start(range), self.max_by_start(range));
|
||||
match (a.end, b.start)
|
||||
{
|
||||
(HigherBound::Infinity, LowerBound::Infinity) => Some(SimpleRange::from(..)),
|
||||
(HigherBound::Infinity, LowerBound::Bounded(a)) => Some(SimpleRange::from(a..)),
|
||||
(HigherBound::Bounded(a), LowerBound::Infinity) => Some(SimpleRange::from(..a)),
|
||||
(HigherBound::Bounded(a), LowerBound::Bounded(b)) if a <= b => None,
|
||||
(HigherBound::Bounded(a), LowerBound::Bounded(b)) => Some(SimpleRange::from(a..b)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn without(&self, range: &SimpleRange) -> SimpleRange
|
||||
{
|
||||
match self.intersection(range)
|
||||
{
|
||||
Some(intersecting_part) => match self.union(&intersecting_part)
|
||||
{
|
||||
UnionResult::Single(res) => res,
|
||||
UnionResult::Union(_, _) => unreachable!(),
|
||||
},
|
||||
None => *self,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn symetrical_difference(&self, range: &SimpleRange) -> Option<UnionResult<SimpleRange>>
|
||||
{
|
||||
match self.intersection(range)
|
||||
{
|
||||
Some(intersecting_part) =>
|
||||
{
|
||||
Some((self.without(&intersecting_part)).union(&range.without(&intersecting_part)))
|
||||
}
|
||||
None => Some(self.union(range)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_by_start(&self, range: &SimpleRange) -> SimpleRange
|
||||
{
|
||||
if self.start < range.start
|
||||
{
|
||||
*self
|
||||
}
|
||||
else
|
||||
{
|
||||
*range
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_by_start(&self, range: &SimpleRange) -> SimpleRange
|
||||
{
|
||||
if self.start < range.start
|
||||
{
|
||||
*range
|
||||
}
|
||||
else
|
||||
{
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min_by_end(&self, range: &SimpleRange) -> SimpleRange
|
||||
{
|
||||
if self.end < range.end { *self } else { *range }
|
||||
}
|
||||
|
||||
pub fn max_by_end(&self, range: &SimpleRange) -> SimpleRange
|
||||
{
|
||||
if self.end < range.end { *range } else { *self }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Range<i64>> for SimpleRange
|
||||
{
|
||||
fn from(value: Range<i64>) -> Self
|
||||
{
|
||||
SimpleRange {
|
||||
start: LowerBound::Bounded(value.start),
|
||||
end: HigherBound::Bounded(value.end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeTo<i64>> for SimpleRange
|
||||
{
|
||||
fn from(value: RangeTo<i64>) -> Self
|
||||
{
|
||||
SimpleRange {
|
||||
start: LowerBound::Infinity,
|
||||
end: HigherBound::Bounded(value.end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeFrom<i64>> for SimpleRange
|
||||
{
|
||||
fn from(value: RangeFrom<i64>) -> Self
|
||||
{
|
||||
SimpleRange {
|
||||
start: LowerBound::Bounded(value.start),
|
||||
end: HigherBound::Infinity,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RangeFull> for SimpleRange
|
||||
{
|
||||
fn from(_: RangeFull) -> Self
|
||||
{
|
||||
SimpleRange {
|
||||
start: LowerBound::Infinity,
|
||||
end: HigherBound::Infinity,
|
||||
}
|
||||
}
|
||||
}
|
||||
16
doz/src/domain/union.rs
Normal file
16
doz/src/domain/union.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// Module for computing intersection of domains
|
||||
|
||||
use crate::domain::Domain;
|
||||
use crate::domain::DomainRange;
|
||||
|
||||
impl DomainRange
|
||||
{
|
||||
pub fn union(&self, domain: &DomainRange) -> Domain
|
||||
{
|
||||
if let Some(simple_a) = self.is_simple()
|
||||
&& let Some(simple_b) = self.is_simple()
|
||||
{
|
||||
simple_a.union(&simple_b)
|
||||
}
|
||||
}
|
||||
}
|
||||
1
doz/src/lib.rs
Normal file
1
doz/src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod domain;
|
||||
16
doz/src/main.rs
Normal file
16
doz/src/main.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use doz::domain::Domain;
|
||||
use doz::domain::DomainRange;
|
||||
|
||||
fn main()
|
||||
{
|
||||
let even = Domain {
|
||||
ranges: vec![DomainRange {
|
||||
start: doz::domain::LowerBound::Infinity,
|
||||
end: doz::domain::HigherBound::Infinity,
|
||||
modulo: 2,
|
||||
congruent: 0,
|
||||
}],
|
||||
};
|
||||
|
||||
println!("{}", even);
|
||||
}
|
||||
57
src/ast.rs
57
src/ast.rs
@ -30,43 +30,7 @@ pub enum Body
|
||||
pub enum Predicate
|
||||
{
|
||||
Variable(Variable), // Upercase variable like X
|
||||
Fixed(Functor, Vec<Predicate>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Functor
|
||||
{
|
||||
Operator(Operator),
|
||||
Functor(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Operator
|
||||
{
|
||||
pub op: String,
|
||||
pub precedence: usize,
|
||||
pub op_type: OperatorType,
|
||||
}
|
||||
|
||||
impl Operator
|
||||
{
|
||||
pub fn new(op: String, precedence: usize, op_type: OperatorType) -> Self
|
||||
{
|
||||
Self {
|
||||
op,
|
||||
precedence,
|
||||
op_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum OperatorType
|
||||
{
|
||||
Prefix,
|
||||
Postfix,
|
||||
LeftInfix,
|
||||
RightInfix,
|
||||
Fixed(String, Vec<Predicate>),
|
||||
}
|
||||
|
||||
impl Display for Body
|
||||
@ -162,22 +126,3 @@ impl Display for Module
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Functor
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
match self
|
||||
{
|
||||
Functor::Operator(op) => write!(f, "{}", op),
|
||||
Functor::Functor(name) => write!(f, "{}", name),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Display for Operator
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
||||
{
|
||||
write!(f, "{}", self.op)
|
||||
}
|
||||
}
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@ -17,24 +17,9 @@ fn main()
|
||||
|
||||
mult(zero, X, zero).
|
||||
mult(s(Y), X, Z) :- mult(Y, X, W), add(W, X, Z).
|
||||
|
||||
op(10, yfx, +).
|
||||
op(8, yfx, ^).
|
||||
op(6, xfy, ::).
|
||||
op(2, fx, [).
|
||||
op(2, xf, ]).
|
||||
op(3, yfx, |).
|
||||
|
||||
A + B :- test.
|
||||
|
||||
A ^ B + C :- test.
|
||||
A::B::C :- A.
|
||||
[Hd|Tl] :- Hd::Tl.
|
||||
"
|
||||
.into();
|
||||
|
||||
// println!("{}", module);
|
||||
// let prop: Body = "mult(X, s(s(s(zero))), s(s(s(s(s(s(s(s(s(zero))))))))))".into();
|
||||
//let prop: Body = "integer(s(X))".into();
|
||||
let prop: Body = "mult(X, s(s(zero)), s(s(s(s(zero)))))".into();
|
||||
//let prop: Body = "mult(X, Y, Z)".into();
|
||||
|
||||
275
src/parsing.rs
275
src/parsing.rs
@ -1,205 +1,25 @@
|
||||
use std::{collections::HashMap, path::Path};
|
||||
use std::path::Path;
|
||||
|
||||
use winnow::{
|
||||
ascii::{self, alphanumeric1, multispace0},
|
||||
combinator::{
|
||||
alt, delimited, expression, opt, preceded, repeat, separated, seq, terminated, Infix,
|
||||
Postfix, Prefix,
|
||||
},
|
||||
error::ContextError,
|
||||
Parser, Result, Stateful,
|
||||
};
|
||||
use winnow::Parser;
|
||||
use winnow::Result;
|
||||
use winnow::ascii::alphanumeric0;
|
||||
use winnow::ascii::multispace0;
|
||||
use winnow::combinator::alt;
|
||||
use winnow::combinator::delimited;
|
||||
use winnow::combinator::opt;
|
||||
use winnow::combinator::separated;
|
||||
use winnow::combinator::seq;
|
||||
use winnow::error::ContextError;
|
||||
|
||||
use crate::ast::Body;
|
||||
use crate::ast::Clause;
|
||||
use crate::ast::Functor;
|
||||
use crate::ast::Module;
|
||||
use crate::ast::Operator;
|
||||
use crate::ast::Predicate;
|
||||
use crate::ast::{Body, OperatorType};
|
||||
use crate::ast::Variable;
|
||||
|
||||
impl Operator
|
||||
pub fn predicate_parse(input: &mut &str) -> Result<Predicate>
|
||||
{
|
||||
pub fn get_precedence(&self) -> usize
|
||||
{
|
||||
self.precedence
|
||||
}
|
||||
pub fn make_infix_operator<'a>(self) -> Infix<Stream<'a>, Predicate, Operator, ContextError>
|
||||
{
|
||||
fn make_predicate(
|
||||
_: &mut Stream,
|
||||
a: Predicate,
|
||||
op: Operator,
|
||||
b: Predicate,
|
||||
) -> Result<Predicate>
|
||||
{
|
||||
Ok(Predicate::Fixed(Functor::Operator(op), vec![a, b]))
|
||||
}
|
||||
let precedence = self.get_precedence() as i64;
|
||||
match self.op_type
|
||||
{
|
||||
OperatorType::LeftInfix => Infix::Left(precedence, self, make_predicate),
|
||||
OperatorType::RightInfix => Infix::Right(precedence, self, make_predicate),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Operator
|
||||
{
|
||||
fn infix(value: String, state: &State) -> Result<Self>
|
||||
{
|
||||
state
|
||||
.custom_operators
|
||||
.get(&(value.clone(), OperatorType::RightInfix))
|
||||
.or_else(|| {
|
||||
state
|
||||
.custom_operators
|
||||
.get(&(value, OperatorType::LeftInfix))
|
||||
})
|
||||
.ok_or(ContextError::new())
|
||||
.cloned()
|
||||
}
|
||||
fn prefix(value: String, state: &State) -> Result<Self>
|
||||
{
|
||||
state
|
||||
.custom_operators
|
||||
.get(&(value, OperatorType::Prefix))
|
||||
.ok_or(ContextError::new())
|
||||
.cloned()
|
||||
}
|
||||
fn postfix(value: String, state: &State) -> Result<Self>
|
||||
{
|
||||
state
|
||||
.custom_operators
|
||||
.get(&(value, OperatorType::Postfix))
|
||||
.ok_or(ContextError::new())
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
|
||||
const OPERATORS: [&str; 9] = [":", "-", "+", "|", "/", "*", "[", "]", "^"];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State
|
||||
{
|
||||
custom_operators: HashMap<(String, OperatorType), Operator>,
|
||||
}
|
||||
|
||||
impl Default for State
|
||||
{
|
||||
fn default() -> Self
|
||||
{
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl State
|
||||
{
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self {
|
||||
custom_operators: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Stream<'is> = Stateful<&'is str, State>;
|
||||
|
||||
pub fn operator_parse(input: &mut Stream) -> Result<String>
|
||||
{
|
||||
delimited(multispace0, repeat(1.., alt(OPERATORS)), multispace0)
|
||||
.map(|op: String| Ok(op))
|
||||
.parse_next(input)?
|
||||
}
|
||||
|
||||
pub fn operator_parse_infix(input: &mut Stream) -> Result<Operator>
|
||||
{
|
||||
operator_parse(input).map(|op| Operator::infix(op, &input.state))?
|
||||
}
|
||||
pub fn operator_parse_postfix(input: &mut Stream) -> Result<Operator>
|
||||
{
|
||||
operator_parse(input).map(|op| Operator::postfix(op, &input.state))?
|
||||
}
|
||||
pub fn operator_parse_prefix(input: &mut Stream) -> Result<Operator>
|
||||
{
|
||||
operator_parse(input).map(|op| Operator::prefix(op, &input.state))?
|
||||
}
|
||||
|
||||
pub fn operator_definition_parse(input: &mut Stream) -> Result<()>
|
||||
{
|
||||
let (precedence, op_type, op) = preceded(
|
||||
"op",
|
||||
delimited(
|
||||
("(", multispace0),
|
||||
seq! {
|
||||
ascii::dec_uint,
|
||||
_: (multispace0, ",", multispace0),
|
||||
alt(("xfx", "xfy", "yfx", "xf", "yf", "fy", "fx")),
|
||||
_: (multispace0, ",", multispace0),
|
||||
operator_parse,
|
||||
},
|
||||
(multispace0, ")", multispace0, "."),
|
||||
),
|
||||
)
|
||||
.parse_next(input)?;
|
||||
let op_type = match op_type
|
||||
{
|
||||
"xf" | "yf" => OperatorType::Postfix,
|
||||
"fx" | "fy" => OperatorType::Prefix,
|
||||
"xfx" => unimplemented!(),
|
||||
"yfx" => OperatorType::LeftInfix,
|
||||
"xfy" => OperatorType::RightInfix,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
input.state.custom_operators.insert(
|
||||
(op.clone(), op_type.clone()),
|
||||
Operator::new(op, precedence, op_type),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn predicate_parse_infix_expression<'a>(
|
||||
input: &mut Stream<'a>,
|
||||
) -> Result<Infix<Stream<'a>, Predicate, Operator, ContextError>>
|
||||
{
|
||||
let op = operator_parse_infix.parse_next(input)?;
|
||||
Ok(op.make_infix_operator())
|
||||
}
|
||||
|
||||
pub fn predicate_parse_prefix_expression<'a>(
|
||||
input: &mut Stream<'a>,
|
||||
) -> Result<Prefix<Stream<'a>, Predicate, Operator, ContextError>>
|
||||
{
|
||||
let op = operator_parse_prefix.parse_next(input)?;
|
||||
let precedence = op.get_precedence() as i64;
|
||||
Ok(Prefix(precedence, op, |_, op, a| {
|
||||
Ok(Predicate::Fixed(Functor::Operator(op), vec![a]))
|
||||
}))
|
||||
}
|
||||
pub fn predicate_parse_postfix_expression<'a>(
|
||||
input: &mut Stream<'a>,
|
||||
) -> Result<Postfix<Stream<'a>, Predicate, Operator, ContextError>>
|
||||
{
|
||||
let op = operator_parse_postfix.parse_next(input)?;
|
||||
let precedence = op.get_precedence() as i64;
|
||||
Ok(Postfix(precedence, op, |_, a, op| {
|
||||
Ok(Predicate::Fixed(Functor::Operator(op), vec![a]))
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn predicate_parse_expression(input: &mut Stream) -> Result<Predicate>
|
||||
{
|
||||
expression(predicate_parse_recursive)
|
||||
.infix(predicate_parse_infix_expression)
|
||||
.postfix(predicate_parse_postfix_expression)
|
||||
.prefix(predicate_parse_prefix_expression)
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
pub fn predicate_parse_variable_or_functor(input: &mut Stream) -> Result<Predicate>
|
||||
{
|
||||
let ident = alphanumeric1.parse_next(input)?;
|
||||
let ident = alphanumeric0.parse_next(input)?;
|
||||
|
||||
// Check if output is a variable
|
||||
if ident.chars().next().is_some_and(|char| char.is_uppercase())
|
||||
@ -215,28 +35,11 @@ pub fn predicate_parse_variable_or_functor(input: &mut Stream) -> Result<Predica
|
||||
)
|
||||
.parse_next(input)
|
||||
.unwrap_or(Vec::new());
|
||||
Ok(Predicate::Fixed(
|
||||
Functor::Functor(String::from(ident)),
|
||||
arguments,
|
||||
))
|
||||
Ok(Predicate::Fixed(String::from(ident), arguments))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn predicate_parse_recursive(input: &mut Stream) -> Result<Predicate>
|
||||
{
|
||||
alt((
|
||||
delimited("(", predicate_parse, ")"),
|
||||
predicate_parse_variable_or_functor,
|
||||
))
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
pub fn predicate_parse(input: &mut Stream) -> Result<Predicate>
|
||||
{
|
||||
alt((predicate_parse_expression, predicate_parse_recursive)).parse_next(input)
|
||||
}
|
||||
|
||||
fn body_parse_or(input: &mut Stream) -> Result<Body>
|
||||
fn body_parse_or(input: &mut &str) -> Result<Body>
|
||||
{
|
||||
separated(
|
||||
1..,
|
||||
@ -250,7 +53,7 @@ fn body_parse_or(input: &mut Stream) -> Result<Body>
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
pub fn body_parse(input: &mut Stream) -> Result<Body>
|
||||
pub fn body_parse(input: &mut &str) -> Result<Body>
|
||||
{
|
||||
// Parse and
|
||||
separated(1.., body_parse_or, (multispace0, ",", multispace0))
|
||||
@ -258,7 +61,7 @@ pub fn body_parse(input: &mut Stream) -> Result<Body>
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
pub fn clause_parse(input: &mut Stream) -> Result<Clause>
|
||||
pub fn clause_parse(input: &mut &str) -> Result<Clause>
|
||||
{
|
||||
seq! {
|
||||
Clause
|
||||
@ -272,19 +75,12 @@ pub fn clause_parse(input: &mut Stream) -> Result<Clause>
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
pub fn module_parse(input: &mut Stream) -> Result<Module>
|
||||
pub fn module_parse(input: &mut &str) -> Result<Module>
|
||||
{
|
||||
let _: Result<&str, ContextError> = multispace0.parse_next(input);
|
||||
separated(
|
||||
0..,
|
||||
preceded::<_, (), _, _, _, _>(
|
||||
repeat(0.., terminated(operator_definition_parse, multispace0)),
|
||||
clause_parse,
|
||||
),
|
||||
multispace0,
|
||||
)
|
||||
.map(|clauses| Module { clauses })
|
||||
.parse_next(input)
|
||||
separated(0.., clause_parse, multispace0)
|
||||
.map(|clauses| Module { clauses })
|
||||
.parse_next(input)
|
||||
}
|
||||
|
||||
impl<T> From<T> for Module
|
||||
@ -293,13 +89,8 @@ where
|
||||
{
|
||||
fn from(value: T) -> Self
|
||||
{
|
||||
let str: &str = value.as_ref();
|
||||
module_parse
|
||||
.parse_next(&mut Stream {
|
||||
input: str,
|
||||
state: State::new(),
|
||||
})
|
||||
.unwrap()
|
||||
let mut str: &str = value.as_ref();
|
||||
module_parse.parse_next(&mut str).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -317,13 +108,8 @@ where
|
||||
{
|
||||
fn from(value: T) -> Self
|
||||
{
|
||||
let str: &str = value.as_ref();
|
||||
predicate_parse
|
||||
.parse_next(&mut Stream {
|
||||
input: str,
|
||||
state: State::new(),
|
||||
})
|
||||
.unwrap()
|
||||
let mut str: &str = value.as_ref();
|
||||
predicate_parse.parse_next(&mut str).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,12 +119,7 @@ where
|
||||
{
|
||||
fn from(value: T) -> Self
|
||||
{
|
||||
let str: &str = value.as_ref();
|
||||
body_parse
|
||||
.parse_next(&mut Stream {
|
||||
input: str,
|
||||
state: State::new(),
|
||||
})
|
||||
.unwrap()
|
||||
let mut str: &str = value.as_ref();
|
||||
body_parse.parse_next(&mut str).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
use log::info;
|
||||
use owo_colors::colors::css::DarkGray;
|
||||
use owo_colors::colors::css::Gray;
|
||||
use owo_colors::OwoColorize;
|
||||
use owo_colors::Style;
|
||||
use owo_colors::colors::css::DarkGray;
|
||||
use owo_colors::colors::css::Gray;
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
1
winnow
1
winnow
Submodule winnow deleted from cc0438a28f
Reference in New Issue
Block a user