This repository has been archived on 2023-09-10. You can view files and clone it, but cannot push or open issues/pull-requests.
joko/lib/StaticPass.cpp

129 lines
3.1 KiB
C++
Raw Normal View History

2023-09-09 22:03:28 +00:00
#include "StaticPass.hpp"
namespace jk
{
/*explicit*/ StaticPass::StaticPass(std::shared_ptr<SymTable> sym,
Logger& logger)
: m_sym { sym }
, m_logger { logger }
{
}
/*virtual*/ StaticPass::~StaticPass()
{
}
void StaticPass::pass(std::shared_ptr<Node> node)
{
switch (node->type())
{
case NODE_PROG:
case NODE_BODY: {
2023-09-10 14:16:20 +00:00
2023-09-09 22:03:28 +00:00
for (size_t i=0; i<node->size(); i++)
{
pass(node->child(i).lock());
2023-09-10 06:06:34 +00:00
pop();
2023-09-09 22:03:28 +00:00
}
} break;
case NODE_LAMBDA: {
auto params = node->child(0).lock();
auto body = node->child(1).lock();
for (size_t i=0; i<params->size(); i++)
{
auto param = params->child(i).lock();
std::string ident = params->child(i).lock()->repr();
m_sym->declare(ident,
std::make_shared<Type>(TYPE_NIL),
param->loc());
}
pass(body);
push(std::make_shared<Type>(TYPE_FUNCTION));
} break;
2023-09-10 06:06:34 +00:00
case NODE_INT: {
push(std::make_shared<Type>(TYPE_INT));
} break;
2023-09-10 12:39:23 +00:00
case NODE_BOOL: {
push(std::make_shared<Type>(TYPE_BOOL));
} break;
2023-09-10 06:06:34 +00:00
case NODE_IDENT: {
std::string ident = node->repr();
auto entry = m_sym->find(ident);
2023-09-10 14:16:20 +00:00
if (!entry
&& std::find(std::begin(m_statics), std::end(m_statics),
ident) == std::end(m_statics))
2023-09-10 06:06:34 +00:00
{
m_logger.log<symbolic_error>(LOG_ERROR, node->loc(),
std::string()
+ "'"
+ ident
+ "' is undefined");
}
2023-09-09 22:03:28 +00:00
2023-09-10 14:16:20 +00:00
if (entry)
{
push(entry->type);
}
2023-09-10 06:06:34 +00:00
} break;
case NODE_VARDECL: {
std::string ident = node->child(0).lock()->repr();
pass(node->child(1).lock());
auto type = pop();
m_sym->declare(ident, type, node->loc());
push(type);
} break;
case NODE_FUNCALL: {
2023-09-10 14:16:20 +00:00
auto is_static = std::find(std::begin(m_statics),
std::end(m_statics),
node->child(0).lock()->repr())
!= std::end(m_statics);
if (!is_static)
2023-09-10 06:06:34 +00:00
{
2023-09-10 14:16:20 +00:00
for (size_t i=0; i<node->size(); i++)
{
pass(node->child(i).lock());
pop();
}
2023-09-10 06:06:34 +00:00
}
2023-09-10 14:16:20 +00:00
2023-09-10 06:06:34 +00:00
push(std::make_shared<Type>(TYPE_NIL));
2023-09-10 14:16:20 +00:00
2023-09-09 22:03:28 +00:00
} break;
default:
std::cerr << "cannot static_pass unknown node '"
<< NodeTypeStr[node->type()] << "'" << std::endl;
abort();
}
}
2023-09-10 06:06:34 +00:00
2023-09-10 14:16:20 +00:00
void StaticPass::add_static(std::string const& name)
{
m_statics.push_back(name);
}
2023-09-10 06:06:34 +00:00
void StaticPass::push(std::shared_ptr<Type> type)
{
m_types.push_back(type);
}
std::shared_ptr<Type> StaticPass::pop()
{
auto type = m_types.back();
m_types.pop_back();
return type;
}
2023-09-09 22:03:28 +00:00
}