2023-09-09 22:03:28 +00:00
|
|
|
#include "Compiler.hpp"
|
|
|
|
#include "lib/Opcodes.hpp"
|
|
|
|
#include "Code.hpp"
|
|
|
|
|
|
|
|
namespace jk
|
|
|
|
{
|
2023-09-10 07:59:51 +00:00
|
|
|
/*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,
|
2023-09-10 07:59:51 +00:00
|
|
|
std::shared_ptr<Program> program,
|
|
|
|
std::shared_ptr<SymTable> sym)
|
2023-09-09 22:03:28 +00:00
|
|
|
{
|
|
|
|
switch (node->type())
|
|
|
|
{
|
2023-09-10 07:59:51 +00:00
|
|
|
case NODE_PROG:
|
|
|
|
case NODE_BODY: {
|
2023-09-09 22:03:28 +00:00
|
|
|
for (size_t i=0; i<node->size(); i++)
|
|
|
|
{
|
2023-09-10 07:59:51 +00:00
|
|
|
compile(node->child(i).lock(), program, sym);
|
2023-09-09 22:03:28 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NODE_FUNCALL: {
|
|
|
|
for (size_t i=0; i<node->size(); i++)
|
|
|
|
{
|
2023-09-10 07:59:51 +00:00
|
|
|
compile(node->child(i).lock(), program, sym);
|
2023-09-09 22:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
program->push_instr(OPCODE_CALL, node->size() - 1);
|
|
|
|
} break;
|
|
|
|
|
2023-09-10 06:06:34 +00:00
|
|
|
case NODE_VARDECL: {
|
|
|
|
std::string ident = node->child(0).lock()->repr();
|
2023-09-10 07:59:51 +00:00
|
|
|
auto entry = sym->find(ident);
|
2023-09-10 06:06:34 +00:00
|
|
|
assert(entry);
|
|
|
|
|
2023-09-10 07:59:51 +00:00
|
|
|
compile(node->child(1).lock(), program, sym);
|
2023-09-10 06:06:34 +00:00
|
|
|
program->push_instr(OPCODE_STORE, entry->addr);
|
|
|
|
} break;
|
|
|
|
|
2023-09-10 07:59:51 +00:00
|
|
|
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);
|
|
|
|
//program->push_instr(OpcodeType opcode)
|
|
|
|
} break;
|
|
|
|
|
2023-09-09 22:03:28 +00:00
|
|
|
case NODE_IDENT: {
|
|
|
|
std::string ident = node->repr();
|
2023-09-10 07:59:51 +00:00
|
|
|
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;
|
|
|
|
|
2023-09-10 07:59:51 +00:00
|
|
|
if (mysym->is_global)
|
2023-09-09 22:03:28 +00:00
|
|
|
{
|
|
|
|
op_load = OPCODE_LOAD_GLOBAL;
|
|
|
|
}
|
|
|
|
|
2023-09-10 07:59:51 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cerr << "cannot compile unknown node '"
|
|
|
|
<< NodeTypeStr[node->type()] << "'" << std::endl;
|
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|