From cfc130b890a8708d44b3662879cf6d42ff2f9edc Mon Sep 17 00:00:00 2001 From: bronku Date: Mon, 1 Dec 2025 15:12:47 +0100 Subject: [PATCH] function def --- src/codegen.hpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/codegen.hpp b/src/codegen.hpp index 8ca1dce..f0c1dba 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -88,6 +88,69 @@ private: } write("\n"); } + else if (auto *for_stmt = dynamic_cast(&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(&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(&stmt)) + { + // #todo + } + else if (auto *break_stmt = dynamic_cast(&stmt)) + { + write_indent(); + output << "break\n"; + } + else if (auto *continue_stmt = dynamic_cast(&stmt)) + { + write_indent(); + output << "continue\n"; + } + else if (auto *function_def = dynamic_cast(&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) {