1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt::Display;
use std::write;

use crate::firstpass::{GetReqAttributes, RewriteToSynth};

use super::{Parse, LexerBridge, ParseResult, ParseOption, Tokens, SpannableNode, ResultPeek};

use super::helpers::TokenMapper;

use wagon_lexer::math::Math;

use super::sum::Sum;
use wagon_macros::TokenMapper;

use quote::{ToTokens, quote};

use wagon_macros::new_unspanned;

#[derive(PartialEq, Debug, Eq, Hash, Clone)]
#[new_unspanned]
/// A comparison between two [`Sum`]s.
///
/// If `comp == None`, then this is just a `Sum`.
///
/// # Grammar
/// `Comparison -> [Sum] ([CompOp] [Sum])?;`
pub struct Comparison {
    /// The left-hand side of the comparison
	pub sum: SpannableNode<Sum>,
    /// The optional operator and right-hand side.
	pub comp: Option<Comp>
}

#[derive(PartialEq, Debug, Eq, Hash, Clone)]
#[cfg_attr(test, new_unspanned)]
/// The operator and right-hand side of a comparison.
pub struct Comp {
    /// The operator.
	pub op: CompOp,
    /// The right-hand side.
	pub right: SpannableNode<Sum>
}

#[derive(TokenMapper, PartialEq, Debug, Eq, Hash, Clone)]
/// All possible comparison operators.
///
/// # Grammar
/// `CompOp -> "<" | "<=" | ">" | ">=" | "==" | "!=" | "in";`
pub enum CompOp {
    /// `==`
	Eq,
    /// `!=` 
	Neq,
    /// `<=`
	Lte,
    /// `<`
	Lt,
    /// `>=`
	Gte,
    /// `>`
	Gt,
    /// `in`
	In
}

impl Parse for Comparison {

    fn parse(lexer: &mut LexerBridge) -> ParseResult<Self> where Self: Sized {
        Ok(Self { sum: SpannableNode::parse(lexer)?, comp: Comp::parse_option(lexer)? })
    }

}

impl ParseOption for Comp {

    fn parse_option(lexer: &mut LexerBridge) -> ParseResult<Option<Self>> where Self: Sized {
        if let Some(op) = CompOp::token_to_enum(lexer.peek_result()?) {
        	lexer.next();
        	Ok(Some(Self { op, right: SpannableNode::parse(lexer)?}))
        } else {
        	Ok(None)
        }
    }
}

impl GetReqAttributes for Comparison {
    fn get_req_attributes(&self) -> crate::firstpass::ReqAttributes {
        let mut req = self.sum.get_req_attributes();
        if let Some(cont) = &self.comp {
            req.extend(cont.right.get_req_attributes());
        }
        req
    }
}

impl RewriteToSynth for Comparison {
    fn rewrite_to_synth(&mut self) -> crate::firstpass::ReqAttributes {
        let mut req = self.sum.rewrite_to_synth();
        if let Some(cont) = &mut self.comp {
            req.extend(cont.right.rewrite_to_synth());
        }
        req
    }
}

impl Display for Comparison {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(c) = &self.comp {
            write!(f, "{} {} {}", self.sum, c.op, c.right)
        } else {
            write!(f, "{}", self.sum)
        }
    }
}

impl Display for CompOp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Eq => write!(f, "=="),
            Self::Neq => write!(f, "!="),
            Self::Lte => write!(f, "<="),
            Self::Lt => write!(f, "<"),
            Self::Gte => write!(f, ">="),
            Self::Gt => write!(f, ">"),
            Self::In => write!(f, "in"),
        }
    }
}

impl ToTokens for CompOp {
    fn to_tokens(&self, tokens: &mut quote::__private::TokenStream) {
        match self {
            Self::Eq => tokens.extend(quote!(==)),
            Self::Neq => tokens.extend(quote!(!=)),
            Self::Lte => tokens.extend(quote!(<=)),
            Self::Lt => tokens.extend(quote!(<)),
            Self::Gte => tokens.extend(quote!(>=)),
            Self::Gt => tokens.extend(quote!(>)),
            Self::In => unimplemented!("Should be a special case!"),
        };
    }
}