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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::collections::HashMap;
use ast;

pub trait Walker {
    fn code(&mut self, _: &ast::Code) { }
    fn decl(&mut self, _: &ast::Decl) { }
    fn edgeref(&mut self, _: &ast::EdgeRef) { }
    fn expr(&mut self, _: &ast::Expr) { }
    fn seq(&mut self, _: &ast::Seq) { }
    fn toplevel(&mut self, _: &ast::Toplevel) { }
}

pub trait Walkable {
    fn walk<W: Walker>(&self, walker: &mut W);
}

impl Walkable for ast::Code {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.code(self);
        for item in &self.0 {
            item.walk(walker);
        }
    }
}

#[allow(unused_variables)]
impl Walkable for ast::Toplevel {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.toplevel(self);
        match self {
            &ast::Toplevel::Entity(ref id, ref args) => {
                //id.walk(walker);
                //for arg in &args {
                //    arg.walk(walker);
                //}
            }
            &ast::Toplevel::Impl(ref id, ref body) => {
                //id.walk(walker);
                for decl in body {
                    decl.walk(walker);
                }
            }
            _ => { }
        }
    }
}

impl Walkable for ast::EdgeRef {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.edgeref(self);
        //self.0
        //self.1
    }
}

impl Walkable for ast::Decl {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.decl(self);
        match *self {
            ast::Decl::On(ref edgeref, ref block) => {
                edgeref.walk(walker);
                block.walk(walker);
            }
            ast::Decl::Always(ref block) => {
                block.walk(walker);
            }
            _ => { }
        }
    }
}

impl Walkable for ast::SeqBlock {
    fn walk<W: Walker>(&self, walker: &mut W) {
        for seq in self.0.iter() {
            seq.walk(walker);
        }
    }
}

impl Walkable for ast::Seq {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.seq(self);
        match *self {
            ast::Seq::If(ref cond, ref t, ref f) => {
                cond.walk(walker);
                t.walk(walker);
                if let &Some(ref block) = f {
                    block.walk(walker);
                }
            }
            ast::Seq::Loop(ref body) => {
                body.walk(walker);
            }
            ast::Seq::While(ref cond, ref body) => {
                cond.walk(walker);
                body.walk(walker);
            }
            ast::Seq::Set(_, _, ref value) => {
                //TODO btype
                //TODO id
                value.walk(walker);
            }
            ast::Seq::SetIndex(_, _, ref idx, ref value) => {
                //TODO btype
                //TODO id
                idx.walk(walker);
                value.walk(walker);
            }
            ast::Seq::Async(_) => {
                println!("TODO: walk async");
            }
            ast::Seq::Fsm(_) => {
                println!("TODO: walk fsm");
            }
            ast::Seq::Match(ref expr, _) => {
                expr.walk(walker);
                println!("TODO: walk match");
            }
            _ => {
                println!("oh {:?}", self);
                unimplemented!();
            }
        }
    }
}


impl Walkable for ast::Expr {
    fn walk<W: Walker>(&self, walker: &mut W) {
        walker.expr(self);
        //TODO
        //match *self {
        //    ast::Seq::If(ref cond, ref t, ref f) => {
        //        // TODO cond
        //        cond.walk(walker);
        //        t.walk(walker);
        //        if let &Some(ref block) = f {
        //            block.walk(walker);
        //        }
        //    }
        //    ast::Seq::Loop(ref body) => {
        //        body.walk(walker);
        //    }
        //    ast::Seq::While(ref cond, ref body) => {
        //        // TODO cond
        //        body.walk(walker);
        //    }
        //    _ => { }
        //}
    }
}


pub struct InitWalker {
    pub init: HashMap<ast::Ident, ast::Expr>,
}

impl InitWalker {
    pub fn new() -> InitWalker {
        InitWalker {
            init: HashMap::new(),
        }
    }
}

impl Walker for InitWalker {
    fn decl(&mut self, item: &ast::Decl) {
        match *item {
            ast::Decl::Reg(ref ident, _, ref init) => {
                if let &Some(ref init) = init {
                    self.init.insert(ident.clone(), init.clone());
                }
            }
            _ => { }
        }
    }
}


pub struct CountWalker {
    pub yield_count: usize,
    fsm_transition_count: usize,
}

impl CountWalker {
    pub fn new() -> CountWalker {
        CountWalker {
            yield_count: 0,
            fsm_transition_count: 0,
        }
    }
}

impl Walker for CountWalker {
    fn seq(&mut self, item: &ast::Seq) {
        match *item {
            ast::Seq::Yield => {
                self.yield_count += 1;
            }
            ast::Seq::FsmTransition(..) => {
                self.fsm_transition_count += 1;
            }
            _ => { }
        }
    }
}


//pub struct PlaceholderReplacer;
//
//impl PlaceholderReplacer {
//    fn new() -> PlaceholderReplacer {
//        PlaceholderReplacer
//    }
//}
//
//impl Walker for PlaceholderReplacer {
//    fn expr(&mut self, item: &ast::Expr) {
//        match *item {
//            ast::Seq::Placeholder => {
//                *self = ast::Expr::VerilogLiteral("1'bx")
//            }
//            _ => { }
//        }
//    }
//}