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

79 lines
1.8 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(std::shared_ptr<SymTable> sym,
Logger& logger)
: m_sym { sym }
, m_logger(logger)
{
}
/*virtual*/ Compiler::~Compiler()
{
}
void Compiler::compile(std::shared_ptr<Node> node,
std::shared_ptr<Program> program)
{
switch (node->type())
{
case NODE_PROG: {
for (size_t i=0; i<node->size(); i++)
{
compile(node->child(i).lock(), program);
}
} break;
case NODE_FUNCALL: {
for (size_t i=0; i<node->size(); i++)
{
compile(node->child(i).lock(), program);
}
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 = m_sym->find(ident);
assert(entry);
compile(node->child(1).lock(), program);
program->push_instr(OPCODE_STORE, entry->addr);
} break;
2023-09-09 22:03:28 +00:00
case NODE_IDENT: {
std::string ident = node->repr();
auto sym = m_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 (sym->is_global)
{
op_load = OPCODE_LOAD_GLOBAL;
}
2023-09-10 06:06:34 +00:00
program->push_instr(op_load, sym->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();
}
}
}