summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/desugar/desugar-visitor.cc
blob: 770cdf6516e5b271c88f03771f0120bef46cddcb (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
/**
 ** \file desugar/desugar-visitor.cc
 ** \brief Implementation of desugar::DesugarVisitor.
 */

#include <ast/all.hh>
#include <ast/libast.hh>
#include <desugar/desugar-visitor.hh>
#include <misc/algorithm.hh>
#include <misc/symbol.hh>
#include <parse/libparse.hh>
#include <parse/tweast.hh>

namespace desugar
{
  DesugarVisitor::DesugarVisitor(bool desugar_for_p, bool desugar_string_cmp_p)
    : super_type()
    , desugar_for_p_(desugar_for_p)
    , desugar_string_cmp_p_(desugar_string_cmp_p)
  {}

  /*-----------------------------.
  | Desugar string comparisons.  |
  `-----------------------------*/
  void DesugarVisitor::operator()(const ast::OpExp& e)
  {
    // FIXME DONE: Some code was deleted here.
    // Check to see that all of the elements to operate on are strings
    if (dynamic_cast<const type::String*>(e.left_get().type_get()) == nullptr
        || dynamic_cast<const type::String*>(e.right_get().type_get()) == nullptr)
      {
        return super_type::operator()(e);
      }

    const auto left = recurse(e.left_get());
    const auto right = recurse(e.right_get());

    misc::variant<ast::Exp*, ast::ChunkList*> res;

    switch (e.oper_get())
    {
    case ast::OpExp::Oper::eq:
    {
        res = parse::parse(parse::Tweast() << "streq(" << left << ", " << right << ") = 0");
        break;
    }
    case ast::OpExp::Oper::ne:
    {
        res = parse::parse(parse::Tweast() << "streq(" << left << ", " << right << ") <> 0");
        break;
    }
    case ast::OpExp::Oper::lt:
    {
        res = parse::parse(parse::Tweast() << "strcmp(" << left << ", " << right << ") = -1");
        break;
    }
    case ast::OpExp::Oper::le:
    {
        res = parse::parse(parse::Tweast() << "strcmp(" << left << ", " << right << ") <= 0");
        break;
    }
    case ast::OpExp::Oper::gt:
    {
        res = parse::parse(parse::Tweast() << "strcmp(" << left << ", " << right << ") = 1");
        break;
    }
    case ast::OpExp::Oper::ge:
    {
        res = parse::parse(parse::Tweast() << "strcmp(" << left << ", " << right << ") >= 0");
        break;
    }
    default:
        break;
    }

    ast::Exp* final = std::get<ast::Exp*>(res);
    result_ = final;
  }

  /*----------------------.
  | Desugar `for' loops.  |
  `----------------------*/

  /*<<-
    Desugar `for' loops as `while' loops:

       for i := lo to hi do
         body

     is transformed as:

       let
         var _lo := lo
         var _hi := hi
         var i := _lo
       in
         if i <= _hi then
           while 1 do
             (
               body;
               if i = _hi then
                 break;
               i := i + 1
             )
       end

     Notice that:

     - a `_hi' variable is introduced so that `hi' is evaluated only
       once;

     - a `_lo' variable is introduced to prevent `i' from being in the
       scope of `_hi';

     - a first test is performed before entering the loop, so that the
       loop condition becomes `i < _hi' (instead of `i <= _hi'); this
       is done to prevent overflows on INT_MAX.
       ->>*/

  void DesugarVisitor::operator()(const ast::ForExp& e)
  {
    // FIXME DONE: Some code was deleted here.
    auto var_init = recurse(e.vardec_get().init_get());
    auto lim = recurse(e.hi_get());
    auto body = recurse(e.body_get());
    auto iterator = e.vardec_get().name_get();
    auto res = parse::parse(parse::Tweast() << "let "
                            "var _lo := " << var_init <<
                            " var _hi := " << lim <<
                            " var " << iterator << " := _lo "
                            "in "
                            "if " << iterator << " <= _hi then "
                            "while 1 do "
                            "( " << body << ";"
                            "if " << iterator << " = _hi then "
                            "break;"
                             << iterator << " := " << iterator << " + 1 "
                            ") "
                            "end");
    ast::Exp* final = std::get<ast::Exp*>(res);
    result_ = final;
  }

} // namespace desugar