aboutsummaryrefslogtreecommitdiff
path: root/asm/preprocesser.cpp
blob: f5e31e46e46d535f0a0b9d8634d9035a2c4780f6 (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
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
/* Copyright (C) 2024 Aryadev Chavali

 * You may distribute and modify this code under the terms of the
 * GPLv2 license.  You should have received a copy of the GPLv2
 * license with this file.  If not, please write to:
 * aryadev@aryadevchavali.com.

 * Created: 2024-04-14
 * Author: Aryadev Chavali
 * Description: Preprocessor which occurs after lexing before parsing.
 */

#include "./preprocesser.hpp"
#include "./base.hpp"

#include <algorithm>
#include <unordered_map>

using std::pair, std::vector, std::make_pair, std::string, std::string_view;

#define VCLEAR(V)                       \
  std::for_each((V).begin(), (V).end(), \
                [](token_t *t)          \
                {                       \
                  delete t;             \
                });

pp_err_t preprocess_use_blocks(const vector<token_t *> &tokens,
                               vector<token_t *> &vec_out)
{
  for (size_t i = 0; i < tokens.size(); ++i)
  {
    token_t *t = tokens[i];
    if (t->type == token_type_t::PP_USE)
    {
      if (i + 1 >= tokens.size() ||
          tokens[i + 1]->type != token_type_t::LITERAL_STRING)
      {
        VCLEAR(vec_out);
        vec_out.clear();
        return pp_err_t(pp_err_type_t::EXPECTED_STRING, t);
      }

      token_t *name = tokens[i + 1];
      auto source   = read_file(name->content.c_str());
      if (!source)
      {
        VCLEAR(vec_out);
        vec_out.clear();
        return pp_err_t(pp_err_type_t::FILE_NONEXISTENT, name);
      }

      std::vector<token_t *> ftokens;
      lerr_t lerr = tokenise_buffer(source.value(), ftokens);
      if (lerr.type != lerr_type_t::OK)
      {
        VCLEAR(vec_out);
        vec_out.clear();
        return pp_err_t(pp_err_type_t::FILE_PARSE_ERROR, name, lerr);
      }

      vec_out.insert(vec_out.end(), ftokens.begin(), ftokens.end());

      ++i;
    }
    else
      vec_out.push_back(new token_t{*t});
  }
  return pp_err_t();
}

struct const_t
{
  size_t start, end;
};

pp_err_t preprocess_const_blocks(const vector<token_t *> &tokens,
                                 vector<token_t *> &vec_out)
{
  std::unordered_map<string_view, const_t> blocks;
  for (size_t i = 0; i < tokens.size(); ++i)
  {
    token_t *t = tokens[i];
    if (t->type == token_type_t::PP_CONST)
    {
      string_view capture;
      if (i + 1 >= tokens.size() || tokens[i + 1]->type != token_type_t::SYMBOL)
        return pp_err_type_t::EXPECTED_NAME;

      capture = tokens[++i]->content;

      ++i;
      size_t block_start = i, block_end = 0;
      for (; i < tokens.size() && tokens[i]->type != token_type_t::PP_END; ++i)
        continue;

      if (i == tokens.size())
        return pp_err_t{pp_err_type_t::EXPECTED_END};

      block_end = i;

      blocks[capture] = const_t{block_start, block_end};
    }
  }

  if (blocks.size() == 0)
  {
    // Just construct a new vector and carry on
    for (token_t *token : tokens)
      vec_out.push_back(new token_t{*token});
  }
  else
  {
    for (size_t i = 0; i < tokens.size(); ++i)
    {
      token_t *token = tokens[i];
      // Skip the tokens that construct the const
      if (token->type == token_type_t::PP_CONST)
        for (; i < tokens.size() && tokens[i]->type != token_type_t::PP_END;
             ++i)
          continue;
      else if (token->type == token_type_t::PP_REFERENCE)
      {
        auto it = blocks.find(token->content);
        if (it == blocks.end())
        {
          VCLEAR(vec_out);
          vec_out.clear();
          return pp_err_t(pp_err_type_t::UNKNOWN_NAME, token);
        }

        const_t block = it->second;
        for (size_t i = block.start; i < block.end; ++i)
          vec_out.push_back(new token_t{*tokens[i]});
      }
      else
        vec_out.push_back(new token_t{*token});
    }
  }

  return pp_err_t();
}

pp_err_t preprocesser(const vector<token_t *> &tokens,
                      vector<token_t *> &vec_out)
{
  vector<token_t *> use_block_tokens;
  pp_err_t pperr = preprocess_use_blocks(tokens, use_block_tokens);
  if (pperr.type != pp_err_type_t::OK)
  {
    vec_out = tokens;
    return pperr;
  }

  vector<token_t *> const_block_tokens;
  pperr = preprocess_const_blocks(use_block_tokens, const_block_tokens);
  if (pperr.type != pp_err_type_t::OK)
  {
    VCLEAR(tokens);
    vec_out = use_block_tokens;
    return pperr;
  }

  VCLEAR(use_block_tokens);
  vec_out = const_block_tokens;

  return pp_err_t{pp_err_type_t::OK};
}

// TODO: Implement this
pp_err_t preprocess_macro_blocks(const vector<token_t *> &,
                                 vector<token_t *> &);

std::ostream &operator<<(std::ostream &os, pp_err_t &err)
{
  os << "PREPROCESSING_";
  switch (err.type)
  {
  case OK:
    return os << "OK";
  case EXPECTED_NAME:
    return os << "EXPECTED_NAME";
  case EXPECTED_STRING:
    return os << "EXPECTED_STRING";
  case EXPECTED_END:
    return os << "EXPECTED_END";
  case FILE_NONEXISTENT:
    return os << "FILE_NONEXISTENT";
  case FILE_PARSE_ERROR:
    return os << "FILE_PARSE_ERROR -> \n\t[" << err.reference->content
              << "]:" << err.lerr;
  case UNKNOWN_NAME:
    return os << "UNKNOWN_NAME";
  }
  return os;
}

pp_err_t::pp_err_t() : reference{nullptr}, type{pp_err_type_t::OK}, lerr{}
{}

pp_err_t::pp_err_t(pp_err_type_t e) : reference{nullptr}, type{e}, lerr{}
{}

pp_err_t::pp_err_t(pp_err_type_t err, const token_t *ref)
    : reference{ref}, type{err}
{}

pp_err_t::pp_err_t(pp_err_type_t err, const token_t *ref, lerr_t lerr)
    : reference{ref}, type{err}, lerr{lerr}
{}