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

58 lines
1.4 KiB
C++

#include "Compiler.hpp"
#include "Program.hpp"
#include "Value.hpp"
#include "Node.hpp"
#include "lib/opcodes.hpp"
namespace sk
{
/*explicit*/ Compiler::Compiler()
{
}
/*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_INT: {
auto val = Value::make_int(std::stoi(node->repr()));
program->push_instr(OPCODE_PUSH_CONST, program->push_value(val));
} break;
case NODE_FLOAT: {
auto val = Value::make_float(std::stof(node->repr()));
program->push_instr(OPCODE_PUSH_CONST, program->push_value(val));
} break;
case NODE_BOOL: {
auto val = Value::make_bool(node->repr() == "true");
program->push_instr(OPCODE_PUSH_CONST, program->push_value(val));
} break;
case NODE_STRING: {
auto val = Value::make_string(node->repr());
program->push_instr(OPCODE_PUSH_CONST, program->push_value(val));
} break;
default:
throw std::runtime_error {std::string()
+ "cannot compile unknown node '"
+ NodeTypeStr[node->type()]
+ "'"};
}
}
}