function def

This commit is contained in:
bronku 2025-12-01 15:12:47 +01:00
parent 4ae6c97904
commit cfc130b890

View file

@ -88,6 +88,69 @@ private:
} }
write("\n"); write("\n");
} }
else if (auto *for_stmt = dynamic_cast<const For *>(&stmt))
{
write_indent();
output << "for " << for_stmt->target << " in ";
generate_expr(*for_stmt->iter);
output << "\n";
indent_level++;
for (const auto &s : for_stmt->body)
{
generate_stmt(*s);
}
indent_level--;
write("end\n");
}
else if (auto *while_stmt = dynamic_cast<const While *>(&stmt))
{
write_indent();
output << "while ";
generate_expr(*while_stmt->condition);
output << "\n";
indent_level++;
for (const auto &s : while_stmt->body)
{
generate_stmt(*s);
}
indent_level--;
write("end\n");
}
else if (auto *pass_stmt = dynamic_cast<const Pass *>(&stmt))
{
// #todo
}
else if (auto *break_stmt = dynamic_cast<const Break *>(&stmt))
{
write_indent();
output << "break\n";
}
else if (auto *continue_stmt = dynamic_cast<const Continue *>(&stmt))
{
write_indent();
output << "continue\n";
}
else if (auto *function_def = dynamic_cast<const FunctionDef *>(&stmt))
{
write_indent();
output << "def " << function_def->name << "(";
if (function_def->params.size() > 0)
{
output << function_def->params[0];
}
for (int i = 1; i < function_def->params.size(); i++)
{
output << ", " << function_def->params[i];
}
output << ")\n";
indent_level++;
for (const auto &s : function_def->body)
{
generate_stmt(*s);
}
indent_level--;
write("end\n");
}
} }
void generate_expr(const Expr &expr) void generate_expr(const Expr &expr)
{ {