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 {}; return {};
} }
std::any EmitVisitor::visitBlock(xlangParser::BlockContext *ctx) {
output << "@block" << blockcount++ << std::endl;
visitChildren(ctx);
return {};
}
std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) { std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
if (auto identifier = ctx->Identifier()) { if (auto identifier = ctx->Identifier()) {
tmpcount = 0; tmpcount = 0;
@ -45,30 +39,33 @@ std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
return {}; return {};
} }
if (ctx->If()) { if (ctx->If()) {
int block = blockcount; int block = blockcount++;
output << "@ifexpr" << block << std::endl; output << "@if" << block << "_expr" << std::endl;
tmpcount = 0; tmpcount = 0;
visitExpr(ctx->expr()); visitExpr(ctx->expr());
output << " jnz %t" << tmpcount - 1 << ", @block" << block << ", @ifelse" output << " jnz %t" << tmpcount - 1 << ", @if" << block << "_then, @if"
<< block << std::endl; << block << "_else" << std::endl;
output << "@if" << block << "_then" << std::endl;
visitBlock(ctx->block(0)); 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()) { if (ctx->Else()) {
visitBlock(ctx->block(1)); visitBlock(ctx->block(1));
} }
output << "@ifend" << block << std::endl; output << "@if" << block << "_end" << std::endl;
return {}; return {};
} }
if (ctx->While()) { if (ctx->While()) {
int block = blockcount; int block = blockcount++;
output << "@whileexpr" << block << std::endl; output << "@while" << block << "_expr" << std::endl;
tmpcount = 0; tmpcount = 0;
visitExpr(ctx->expr()); visitExpr(ctx->expr());
output << " jnz %t" << tmpcount - 1 << ", @block" << block << ", @whileend" output << " jnz %t" << tmpcount - 1 << ", @while" << block
<< block << std::endl; << "_body, @while" << block << "_end" << std::endl;
output << "@while" << block << "_body" << std::endl;
visitBlock(ctx->block(0)); visitBlock(ctx->block(0));
output << " jmp @whileexpr" << block << std::endl; output << " jmp @while" << block << "_expr" << std::endl;
output << "@whileend" << block << std::endl; output << "@while" << block << "_end" << std::endl;
return {}; return {};
} }
if (ctx->Return()) { if (ctx->Return()) {

View file

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