mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 15:38:48 +00:00
Update homework 1 and 2
This commit is contained in:
35
src/utils.rs
35
src/utils.rs
@@ -44,38 +44,3 @@ macro_rules! some_or_exit {
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub trait Translate<S> {
|
||||
type Target;
|
||||
type Error;
|
||||
|
||||
fn translate(&mut self, source: &S) -> Result<Self::Target, Self::Error>;
|
||||
}
|
||||
|
||||
pub trait Optimize<T> {
|
||||
fn optimize(&mut self, code: &mut T) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Repeat<O> {
|
||||
inner: O,
|
||||
}
|
||||
|
||||
impl<T, O1: Optimize<T>, O2: Optimize<T>> Optimize<T> for (O1, O2) {
|
||||
fn optimize(&mut self, code: &mut T) -> bool {
|
||||
let changed1 = self.0.optimize(code);
|
||||
let changed2 = self.1.optimize(code);
|
||||
changed1 || changed2
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, O: Optimize<T>> Optimize<T> for Repeat<O> {
|
||||
fn optimize(&mut self, code: &mut T) -> bool {
|
||||
if !self.inner.optimize(code) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while self.inner.optimize(code) {}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user