fix "else"

development
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()) {
@ -89,15 +86,15 @@ std::any EmitVisitor::visitStatement(xlangParser::StatementContext *ctx) {
return {}; return {};
} }
#define OPERATOR(Operator, visitLeft, left, visitRight, right, ssa_op) \ #define OPERATOR(Operator, visitLeft, left, visitRight, right, ssa_op) \
if (ctx->Operator()) { \ if (ctx->Operator()) { \
visitLeft(ctx->left); \ visitLeft(ctx->left); \
int l = tmpcount - 1; \ int l = tmpcount - 1; \
visitRight(ctx->right); \ visitRight(ctx->right); \
output << " %t" << tmpcount << " = w " ssa_op " %t" << l << ", %t" \ output << " %t" << tmpcount << " = w " ssa_op " %t" << l << ", %t" \
<< tmpcount - 1 << std::endl; \ << tmpcount - 1 << std::endl; \
tmpcount++; \ tmpcount++; \
return {}; \ return {}; \
} }
std::any EmitVisitor::visitExpr(xlangParser::ExprContext *ctx) { 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 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;