refactoring to reduce duplicate
refactorings for AST visitor

- do not use class wastefully
- do not use std::function if possible because it allocates heap memory
- use 'detail' namespace to hide implementation