fix "else"

This commit is contained in:
Thomas Lindner 2023-01-05 18:30:43 +01:00
parent 0d5baf46a3
commit 2b79d9bdb2
2 changed files with 23 additions and 27 deletions

View file

@ -30,12 +30,6 @@ std::any EmitVisitor::visitFunction(xlangParser::FunctionContext *ctx) {
return {};
}
std::any EmitVisitor::visitBlock(xlangParser::BlockContext *ctx) {
output << "@block" << blockcount++ << std::endl;
visitChildren(ctx);
return {};
}
std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
if (auto identifier = ctx->Identifier()) {
tmpcount = 0;
@ -45,30 +39,33 @@ std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
return {};
}
if (ctx->If()) {
int block = blockcount;
output << "@ifexpr" << block << std::endl;
int block = blockcount++;
output << "@if" << block << "_expr" << std::endl;
tmpcount = 0;
visitExpr(ctx->expr());
output << " jnz %t" << tmpcount - 1 << ", @block" << block << ", @ifelse"
<< block << std::endl;
output << " jnz %t" << tmpcount - 1 << ", @if" << block << "_then, @if"
<< block << "_else" << std::endl;
output << "@if" << block << "_then" << std::endl;
visitBlock(ctx->block(0));
output << "@ifelse" << block << std::endl;
output << " jmp @if" << block << "_end" << std::endl;
output << "@if" << block << "_else" << std::endl;
if (ctx->Else()) {
visitBlock(ctx->block(1));
}
output << "@ifend" << block << std::endl;
output << "@if" << block << "_end" << std::endl;
return {};
}
if (ctx->While()) {
int block = blockcount;
output << "@whileexpr" << block << std::endl;
int block = blockcount++;
output << "@while" << block << "_expr" << std::endl;
tmpcount = 0;
visitExpr(ctx->expr());
output << " jnz %t" << tmpcount - 1 << ", @block" << block << ", @whileend"
<< block << std::endl;
output << " jnz %t" << tmpcount - 1 << ", @while" << block
<< "_body, @while" << block << "_end" << std::endl;
output << "@while" << block << "_body" << std::endl;
visitBlock(ctx->block(0));
output << " jmp @whileexpr" << block << std::endl;
output << "@whileend" << block << std::endl;
output << " jmp @while" << block << "_expr" << std::endl;
output << "@while" << block << "_end" << std::endl;
return {};
}
if (ctx->Return()) {
@ -89,15 +86,15 @@ std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
return {};
}
#define OPERATOR(Operator, visitLeft, left, visitRight, right, ssa_op) \
if (ctx->Operator()) { \
visitLeft(ctx->left); \
int l = tmpcount - 1; \
visitRight(ctx->right); \
#define OPERATOR(Operator, visitLeft, left, visitRight, right, ssa_op) \
if (ctx->Operator()) { \
visitLeft(ctx->left); \
int l = tmpcount - 1; \
visitRight(ctx->right); \
output << " %t" << tmpcount << " = w " ssa_op " %t" << l << ", %t" \
<< tmpcount - 1 << std::endl; \
tmpcount++; \
return {}; \
<< tmpcount - 1 << std::endl; \
tmpcount++; \
return {}; \
}
std::any EmitVisitor::visitExpr(xlangParser::ExprContext *ctx) {

View file

@ -16,7 +16,6 @@ class EmitVisitor : public xlangBaseVisitor {
std::any visitFile(xlangParser::FileContext *ctx) override;
std::any visitFunction(xlangParser::FunctionContext *ctx) override;
std::any visitBlock(xlangParser::BlockContext *ctx) override;
std::any visitStatement(xlangParser::StatementContext *ctx) override;
std::any visitExpr(xlangParser::ExprContext *ctx) override;
std::any visitSum(xlangParser::SumContext *ctx) override;