2010-06-28 11:28:50 +00:00
|
|
|
#include "locals.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2010-06-28 12:17:50 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <ostream>
|
2010-06-28 11:28:50 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/lower.hpp>
|
2014-02-13 08:40:07 +00:00
|
|
|
|
2010-06-28 11:28:50 +00:00
|
|
|
namespace Compiler
|
|
|
|
{
|
|
|
|
const std::vector<std::string>& Locals::get(char type) const
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
return mShorts;
|
|
|
|
case 'l':
|
|
|
|
return mLongs;
|
2013-12-15 15:16:50 +00:00
|
|
|
case 'f':
|
|
|
|
return mFloats;
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2019-03-28 21:59:26 +00:00
|
|
|
throw std::logic_error("Unknown variable type");
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
int Locals::searchIndex(char type, std::string_view name) const
|
2010-06-28 11:28:50 +00:00
|
|
|
{
|
|
|
|
const std::vector<std::string>& collection = get(type);
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2020-10-22 21:57:53 +00:00
|
|
|
auto iter = std::find(collection.begin(), collection.end(), name);
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 16:27:45 +00:00
|
|
|
if (iter == collection.end())
|
|
|
|
return -1;
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2021-05-02 08:59:22 +00:00
|
|
|
return static_cast<int>(iter - collection.begin());
|
2010-06-28 16:27:45 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
bool Locals::search(char type, std::string_view name) const
|
2013-12-15 15:16:50 +00:00
|
|
|
{
|
2010-06-28 16:27:45 +00:00
|
|
|
return searchIndex(type, name) != -1;
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>& Locals::get(char type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 's':
|
|
|
|
return mShorts;
|
|
|
|
case 'l':
|
|
|
|
return mLongs;
|
2013-12-15 15:16:50 +00:00
|
|
|
case 'f':
|
|
|
|
return mFloats;
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2019-03-28 21:59:26 +00:00
|
|
|
throw std::logic_error("Unknown variable type");
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
char Locals::getType(std::string_view name) const
|
2010-06-28 11:28:50 +00:00
|
|
|
{
|
|
|
|
if (search('s', name))
|
|
|
|
return 's';
|
|
|
|
|
|
|
|
if (search('l', name))
|
|
|
|
return 'l';
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 11:28:50 +00:00
|
|
|
if (search('f', name))
|
|
|
|
return 'f';
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 11:28:50 +00:00
|
|
|
return ' ';
|
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
int Locals::getIndex(std::string_view name) const
|
2010-06-28 16:27:45 +00:00
|
|
|
{
|
|
|
|
int index = searchIndex('s', name);
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 16:27:45 +00:00
|
|
|
if (index != -1)
|
|
|
|
return index;
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 16:27:45 +00:00
|
|
|
index = searchIndex('l', name);
|
|
|
|
|
|
|
|
if (index != -1)
|
|
|
|
return index;
|
|
|
|
|
2013-12-15 15:16:50 +00:00
|
|
|
return searchIndex('f', name);
|
2010-06-28 16:27:45 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 12:17:50 +00:00
|
|
|
void Locals::write(std::ostream& localFile) const
|
|
|
|
{
|
|
|
|
localFile << get('s').size() << ' ' << get('l').size() << ' ' << get('f').size() << std::endl;
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2010-06-28 12:17:50 +00:00
|
|
|
std::copy(get('s').begin(), get('s').end(), std::ostream_iterator<std::string>(localFile, " "));
|
|
|
|
std::copy(get('l').begin(), get('l').end(), std::ostream_iterator<std::string>(localFile, " "));
|
|
|
|
std::copy(get('f').begin(), get('f').end(), std::ostream_iterator<std::string>(localFile, " "));
|
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2022-05-20 23:21:55 +00:00
|
|
|
void Locals::declare(char type, std::string_view name)
|
2010-06-28 11:28:50 +00:00
|
|
|
{
|
2014-02-13 08:40:07 +00:00
|
|
|
get(type).push_back(Misc::StringUtils::lowerCase(name));
|
2010-06-28 11:28:50 +00:00
|
|
|
}
|
2013-12-15 15:16:50 +00:00
|
|
|
|
2010-06-28 11:28:50 +00:00
|
|
|
void Locals::clear()
|
|
|
|
{
|
|
|
|
get('s').clear();
|
|
|
|
get('l').clear();
|
|
|
|
get('f').clear();
|
|
|
|
}
|
|
|
|
}
|