xlang/bootstrap/emit.cc

39 lines
1.1 KiB
C++
Raw Normal View History

#include <emit.hh>
#include <iostream>
namespace xlang {
EmitListener::EmitListener(std::string_view outputfile) : output{outputfile} {}
void EmitListener::enterFile(xlangParser::FileContext *ctx) {
output << "data $printformat = { b \"%ld\\n\", b 0 }" << std::endl;
(void)ctx;
}
void EmitListener::enterFunction(xlangParser::FunctionContext *ctx) {
output << std::endl
<< "export function w $" << ctx->Identifier()->getSymbol()->getText()
<< "()" << std::endl
<< "{" << std::endl
<< "@start" << std::endl;
}
void EmitListener::exitFunction(xlangParser::FunctionContext *ctx) {
output << " ret 0" << std::endl << "}" << std::endl;
(void)ctx;
}
void EmitListener::exitStatement(xlangParser::StatementContext *ctx) {
if (ctx->Print()) {
output << " call $printf(l $printformat, ..., w %v)" << std::endl;
}
}
void EmitListener::exitFactor(xlangParser::FactorContext *ctx) {
if (auto integer = ctx->Integer()) {
output << " %v = w copy " << integer->getSymbol()->getText() << std::endl;
}
}
} // namespace xlang