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/Compiler.cpp

123 lines
3.2 KiB
C++
Raw Normal View History

2023-09-09 22:03:28 +00:00
#include "Compiler.hpp"
#include "lib/Opcodes.hpp"
#include "Code.hpp"
2023-09-10 14:16:20 +00:00
#include "StaticFunction.hpp"
2023-09-09 22:03:28 +00:00
namespace jk
{
/*explicit*/ Compiler::Compiler(Logger& logger)
: m_logger(logger)
2023-09-09 22:03:28 +00:00
{
}
/*virtual*/ Compiler::~Compiler()
{
}
void Compiler::compile(std::shared_ptr<Node> node,
std::shared_ptr<Program> program,
std::shared_ptr<SymTable> sym)
2023-09-09 22:03:28 +00:00
{
switch (node->type())
{
case NODE_PROG:
case NODE_BODY: {
2023-09-09 22:03:28 +00:00
for (size_t i=0; i<node->size(); i++)
{
compile(node->child(i).lock(), program, sym);
2023-09-09 22:03:28 +00:00
}
} break;
case NODE_FUNCALL: {
2023-09-10 14:16:20 +00:00
if (auto itr=m_statics.find(node->child(0).lock()->repr());
itr != std::end(m_statics))
2023-09-09 22:03:28 +00:00
{
2023-09-10 14:16:20 +00:00
itr->second->call(node, program, sym);
}
else
{
for (size_t i=0; i<node->size(); i++)
{
compile(node->child(i).lock(), program, sym);
}
program->push_instr(OPCODE_CALL, node->size() - 1);
2023-09-09 22:03:28 +00:00
}
} break;
2023-09-10 06:06:34 +00:00
case NODE_VARDECL: {
std::string ident = node->child(0).lock()->repr();
auto entry = sym->find(ident);
2023-09-10 06:06:34 +00:00
assert(entry);
compile(node->child(1).lock(), program, sym);
2023-09-10 06:06:34 +00:00
program->push_instr(OPCODE_STORE, entry->addr);
} break;
case NODE_LAMBDA: {
auto params = node->child(0).lock();
auto body = node->child(1).lock();
auto prog = std::make_shared<Program>();
auto fun_sym = std::make_shared<SymTable>(m_logger, sym);
for (size_t i=0; i<params->size(); i++)
{
std::string param = params->child(i).lock()->repr();
fun_sym->declare(param,
std::make_shared<Type>(TYPE_NIL),
node->loc());
}
compile(body, prog, fun_sym);
prog->push_instr(OPCODE_RET);
auto code = std::make_shared<Code>(prog);
size_t addr = program->push_constant(Value::make_code(code));
program->push_instr(OPCODE_PUSH_CONST, addr);
program->push_instr(OPCODE_MK_FUNCTION);
} break;
2023-09-09 22:03:28 +00:00
case NODE_IDENT: {
std::string ident = node->repr();
auto mysym = sym->find(ident);
2023-09-10 06:06:34 +00:00
assert(sym);
2023-09-09 22:03:28 +00:00
OpcodeType op_load = OPCODE_LOAD;
if (mysym->is_global)
2023-09-09 22:03:28 +00:00
{
op_load = OPCODE_LOAD_GLOBAL;
}
program->push_instr(op_load, mysym->addr);
2023-09-09 22:03:28 +00:00
} break;
case NODE_INT: {
auto value = Value::make_int(std::stoi(node->repr()));
size_t addr = program->push_constant(value);
program->push_instr(OPCODE_PUSH_CONST, addr);
} break;
2023-09-10 12:39:23 +00:00
case NODE_BOOL: {
auto value = Value::make_bool(node->repr() == "true");
size_t addr = program->push_constant(value);
program->push_instr(OPCODE_PUSH_CONST, addr);
} break;
2023-09-09 22:03:28 +00:00
default:
std::cerr << "cannot compile unknown node '"
<< NodeTypeStr[node->type()] << "'" << std::endl;
abort();
}
}
2023-09-10 14:16:20 +00:00
void Compiler::declare_static(std::string const& name,
std::shared_ptr<StaticFunction> fun)
{
m_statics[name] = fun;
}
2023-09-09 22:03:28 +00:00
}