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

103 lines
2.6 KiB
C++
Raw Normal View History

2023-09-09 22:03:28 +00:00
#include "Compiler.hpp"
#include "lib/Opcodes.hpp"
#include "Code.hpp"
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: {
for (size_t i=0; i<node->size(); i++)
{
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();
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);
//program->push_instr(OpcodeType opcode)
} 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;
default:
std::cerr << "cannot compile unknown node '"
<< NodeTypeStr[node->type()] << "'" << std::endl;
abort();
}
}
}