summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/bind/binder.hxx
blob: 0facad9f202e9738d0e2c3dfff777398b4d0a03a (plain)
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
/**
 ** \file bind/binder.hxx
 ** \brief Inline methods of bind::Binder.
 **/

// FIXME DONE: Some code was deleted here.

#pragma once

#include <bind/binder.hh>

namespace bind
{
    inline Binder::Binder()
    {
        scoped_map_fun_ = misc::scoped_map<const misc::symbol, ast::FunctionDec*>();
        scoped_map_var_ = misc::scoped_map<const misc::symbol, ast::VarDec*>();
        scoped_map_ty_ = misc::scoped_map<const misc::symbol, ast::TypeDec*>();
        loop_ = std::vector<ast::Exp*>();
        error_ = misc::error();
    }

    inline void Binder::body_func(ast::FunctionDec& e)
    {
        begin_scope();
        bool tmp = in_a_scope_;
        in_a_scope_ = true;
        e.formals_get().accept(*this);
        this->accept(e.body_get());
        in_a_scope_ = tmp;
        end_scope();
    }

    template <> 
    inline void Binder::visit_dec_head<ast::FunctionDec>(ast::FunctionDec& e)
    {
      if (e.name_get() == "_main")
      {
        if (is_main_already_)
        {
          error_ << misc::error::error_type::bind;
          error_ << "Un deuxieme _main??????\n";
          return;
        }
        is_main_already_ = true;
        if (in_a_scope_)
        {
          error_ << misc::error::error_type::bind;
          error_ << "BRADDOCK, je vous préviens, attention ou vous mettez votre _main!!";
          return;
        }
      }
      scoped_map_fun_.put(e.name_get(), &e);
    }

    template <> 
    inline void Binder::visit_dec_bod<ast::FunctionDec>(ast::FunctionDec& e)
    {
        bool actual = in_a_while_;
        in_a_while_ = false;
        this->accept(e.result_get());
        body_func(e);
        in_a_while_ = actual;
    }

    /// @brief visit header so the Ast can know this type exist 
    /// @param e an Dec of a funcrion or ast
    template <> 
    inline void Binder::visit_dec_head<ast::TypeDec>(ast::TypeDec& e)
    {
      scoped_map_ty_.put(e.name_get(), &e);
    }

    template <> 
    inline void Binder::visit_dec_bod<ast::TypeDec>(ast::TypeDec& e)
    {
      super_type::operator()(e);
    }

    inline void Binder::begin_scope()
    {
        scoped_map_fun_.scope_begin();
        scoped_map_ty_.scope_begin();
        scoped_map_var_.scope_begin();
    }

    inline void Binder::end_scope()
    {
        scoped_map_fun_.scope_end();
        scoped_map_ty_.scope_end();
        scoped_map_var_.scope_end();
    }

    inline void Binder::err_undef(const ast::Location& loc, const misc::symbol& name)
    {
      error_ << misc::error::error_type::bind;
      error_ << loc << ": undeclared variable: " << name << "\n";
    }

    inline void Binder::err_type_undef(const ast::Location& loc, const misc::symbol& name)
    {
        error_ << misc::error::error_type::bind;
        error_ << loc << ": undeclared type: " << name << "\n";
    }

    inline void Binder::is_there__main()
    {
        if (!is_main_already_)
        {
            error_ << misc::error::error_type::bind;
            error_ << "Bah bro il est ou ton _main??????\n";
        }
    }

    inline void Binder::err_ddec(const ast::Location& loc,
                                const ast::Location& first,
                                const misc::symbol& name)
    {
      error_ << misc::error::error_type::bind;
      error_ << loc << ": redefinition: " << name << "\n";
      error_ << first << ": first definition\n";
    }

} // namespace bind