summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/bind/binder.cc
blob: 171e1993986c31137cd34656e82392575e108509 (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
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
/**
 ** \file bind/binder.cc
 ** \brief Implementation for bind/binder.hh.
 */

#include <ast/all.hh>
#include <bind/binder.hh>

#include <misc/contract.hh>

namespace bind
{
  /*-----------------.
  | Error handling.  |
  `-----------------*/

  /// The error handler.
  const misc::error& Binder::error_get() const { return error_; }

  // FIXME DONE: Some code was deleted here.
  void Binder::operator()(ast::SimpleVar& e)
  {
    ast::VarDec* init = scoped_map_var_.get(e.name_get());
    if (init == nullptr)
      {
        err_undef(e.location_get(), e.name_get());
        return;
      }
    e.def_set(init);
  }

  void Binder::operator()(ast::CallExp& e)
  {
    ast::FunctionDec* init = scoped_map_fun_.get(e.name_get());
    if (init == nullptr)
      {
        err_undef(e.location_get(), e.name_get());
        return;
      }
    e.def_set(init);
    super_type::operator()(e);
  }

  void Binder::operator()(ast::NameTy& e)
  {
    misc::symbol test_string("string");
    misc::symbol test_int("int");
    if (e.name_get() == test_int || e.name_get() == test_string)
    {
      auto init = scoped_map_ty_.get(e.name_get());
      e.def_set(init);
    }
    else
    {
      auto init = scoped_map_ty_.get(e.name_get());
      if (init == nullptr)
        {
          err_type_undef(e.location_get(), e.name_get());
          return;
        }
      e.def_set(init);
    }
  }

  void Binder::operator()(ast::WhileExp& e)
  {
    bool actual = in_a_while_;
    e.test_get().accept(*this);
    loop_.emplace_back(&e);
    in_a_while_ = true;
    begin_scope();
    e.body_get().accept(*this);
    end_scope();
    loop_.pop_back();
    in_a_while_ = actual;
  }

  void Binder::operator()(ast::ForExp& e)
  {
    bool actual = in_a_while_;
    begin_scope();
    in_a_while_ = true;
    e.vardec_get().accept(*this);
    e.hi_get().accept(*this);
    loop_.emplace_back(&e);
    e.body_get().accept(*this);
    end_scope();
    loop_.pop_back();
    in_a_while_ = actual;
  }

  void Binder::operator()(ast::BreakExp& e)
  {
    if (loop_.empty() || !in_a_while_)
    {
      error_ << misc::error::error_type::bind;
      error_ << e.location_get() << ": 'break' outside any loop\n";
    }
    else
    {
      e.def_set(loop_.back());
    }
  }

  //Declaration of function

  void Binder::operator()(ast::VarDec& e)
  {
    bool actual = in_a_while_;
    scoped_map_var_.put(e.name_get(), &e);
    in_a_while_ = false;
    super_type::operator()(e);
    in_a_while_ = actual;
  }

  void Binder::operator()(ast::FunctionChunk& e)
  {
    chunk_visit<ast::FunctionDec>(e);
  }

  void Binder::operator()(ast::TypeChunk& e)
  {
    chunk_visit<ast::TypeDec>(e);
  }

  template <class D> void Binder::chunk_visit(ast::Chunk<D>& e)
  {
    misc::scoped_map<const misc::symbol, D*> def = misc::scoped_map<const misc::symbol, D*>();
    for (auto machala : e)
    {
      visit_dec_head<D>(*machala);
    }
    for (auto dec : e)
    {
      auto init = def.get(dec->name_get());
      if (init == nullptr)
      {
        def.put(dec->name_get(), dec);
        visit_dec_bod<D>(*dec);
      }
      else
      {
        err_ddec(dec->location_get(), init->location_get(), dec->name_get());
      }
    }
  }

  void Binder::operator()(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);
    bool scope = in_a_scope_;
    bool actual = in_a_while_;
    in_a_while_ = false;
    in_a_scope_ = true;
    this->accept(e.result_get());
    body_func(e);
    in_a_while_ = actual;
    in_a_scope_ = scope;
  }

  void Binder::operator()(ast::TypeDec& e)
  {
    scoped_map_ty_.put(e.name_get(), &e);
    super_type::operator()(e);
  }

} // namespace bind