-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcompound_assign.rs
138 lines (119 loc) · 3.34 KB
/
compound_assign.rs
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
use crate::nodes::{BinaryOperator, Expression, Token, Variable};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompoundOperator {
Plus,
Minus,
Asterisk,
Slash,
DoubleSlash,
Percent,
Caret,
Concat,
}
impl CompoundOperator {
pub fn to_str(&self) -> &'static str {
match self {
Self::Plus => "+=",
Self::Minus => "-=",
Self::Asterisk => "*=",
Self::Slash => "/=",
Self::DoubleSlash => "//=",
Self::Percent => "%=",
Self::Caret => "^=",
Self::Concat => "..=",
}
}
pub fn to_binary_operator(&self) -> BinaryOperator {
match self {
Self::Plus => BinaryOperator::Plus,
Self::Minus => BinaryOperator::Minus,
Self::Asterisk => BinaryOperator::Asterisk,
Self::Slash => BinaryOperator::Slash,
Self::DoubleSlash => BinaryOperator::DoubleSlash,
Self::Percent => BinaryOperator::Percent,
Self::Caret => BinaryOperator::Caret,
Self::Concat => BinaryOperator::Concat,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompoundAssignTokens {
pub operator: Token,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CompoundAssignStatement {
operator: CompoundOperator,
variable: Variable,
value: Expression,
tokens: Option<CompoundAssignTokens>,
}
impl CompoundAssignStatement {
pub fn new<V: Into<Variable>, E: Into<Expression>>(
operator: CompoundOperator,
variable: V,
value: E,
) -> Self {
Self {
operator,
variable: variable.into(),
value: value.into(),
tokens: None,
}
}
pub fn with_tokens(mut self, tokens: CompoundAssignTokens) -> Self {
self.tokens = Some(tokens);
self
}
#[inline]
pub fn set_tokens(&mut self, tokens: CompoundAssignTokens) {
self.tokens = Some(tokens);
}
#[inline]
pub fn get_tokens(&self) -> Option<&CompoundAssignTokens> {
self.tokens.as_ref()
}
#[inline]
pub fn get_operator(&self) -> CompoundOperator {
self.operator
}
#[inline]
pub fn get_variable(&self) -> &Variable {
&self.variable
}
#[inline]
pub fn get_value(&self) -> &Expression {
&self.value
}
#[inline]
pub fn extract_assignment(self) -> (Variable, Expression) {
(self.variable, self.value)
}
#[inline]
pub fn mutate_variable(&mut self) -> &mut Variable {
&mut self.variable
}
#[inline]
pub fn mutate_value(&mut self) -> &mut Expression {
&mut self.value
}
pub fn clear_comments(&mut self) {
if let Some(tokens) = &mut self.tokens {
tokens.operator.clear_comments();
}
}
pub fn clear_whitespaces(&mut self) {
if let Some(tokens) = &mut self.tokens {
tokens.operator.clear_whitespaces();
}
}
pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
if let Some(tokens) = &mut self.tokens {
tokens.operator.replace_referenced_tokens(code);
}
}
pub(crate) fn shift_token_line(&mut self, amount: usize) {
if let Some(tokens) = &mut self.tokens {
tokens.operator.shift_token_line(amount);
}
}
}