{"id":506,"date":"2022-01-15T20:23:24","date_gmt":"2022-01-15T12:23:24","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=506"},"modified":"2022-07-06T10:27:42","modified_gmt":"2022-07-06T02:27:42","slug":"lua%e4%b8%ad%e7%9a%84statement%e7%b1%bb%e5%88%abif%e3%80%81while%e3%80%81do","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=506","title":{"rendered":"lua\u4e2d\u7684statement\u7c7b\u522bif\u3001while\u3001do"},"content":{"rendered":"<h1>1. chunk\u662f\u4ec0\u4e48\u5462?<\/h1>\n<pre><code>- Lua \u6267\u884c\u7684\u6bcf\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f8b\u5982\u4e00\u4e2a\u6587\u4ef6\u6216\u4ea4\u4e92\u6a21\u5f0f\u4e0b\u7684\u4e00\u884c\u4ee3\u7801\uff0c\u90fd\u662f\u4e00\u4e2a\u5757\u3002\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u4e00\u4e2a\u5757\u53ea\u662f\u4e00\u4e2a\u8bed\u53e5\u5e8f\u5217\u3002\n- \u5206\u53f7\u53ef\u4ee5\u53ef\u9009\u5730\u8ddf\u5728\u4efb\u4f55\u8bed\u53e5\u4e4b\u540e\u3002\u901a\u5e38\uff0c\u6211\u53ea\u4f7f\u7528\u5206\u53f7\u6765\u5206\u9694\u5199\u5728\u540c\u4e00\u884c\u4e2d\u7684\u4e24\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\uff0c\u4f46\u8fd9\u53ea\u662f\u4e00\u4e2a\u7ea6\u5b9a\u3002\n- \u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b\uff0cLua \u901a\u5e38\u5c06\u60a8\u952e\u5165\u7684\u6bcf\u4e00\u884c\u89e3\u91ca\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u5757\u3002\u7136\u800c\uff0c\u5982\u679c\u5b83\u68c0\u6d4b\u5230\u8be5\u884c\u4e0d\u80fd\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u5757\uff0c\u5b83\u4f1a\u7b49\u5f85\u66f4\u591a\u7684\u8f93\u5165\uff0c\u76f4\u5230\u5b83\u6709\u4e00\u4e2a\u5b8c\u6574\u7684\u5757\u3002\u5f53 Lua \u7b49\u5f85\u4e00\u884c\u7ee7\u7eed\u65f6\uff0c\u5b83\u4f1a\u663e\u793a\u4e0d\u540c\u7684\u63d0\u793a\uff08\u901a\u5e38\u662f&gt;&gt;\uff09\u3002factorial\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b\u8f93\u5165\u591a\u884c\u5b9a\u4e49\uff0c\u4f8b\u5982\u51fd\u6570\u3002\u7136\u800c\uff0c\u6709\u65f6\u5c06\u8fd9\u4e9b\u5b9a\u4e49\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u8c03\u7528 Lua \u6765\u8fd0\u884c\u8be5\u6587\u4ef6\u4f1a\u66f4\u65b9\u4fbf\u3002\n- \u7531chunk\u7684\u5b9a\u4e49\u6211\u4eec\u53ef\u4ee5\u770b\u51fachunk\u7684\u89e3\u6790\u5e94\u8be5\u662f\n```\nwhile(\u6761\u4ef6){\n    statment(ls);\n    testnext(ls, &#039;;&#039;); \/\/ \u5224\u65ad\u4e0b\u4e00\u4e2a\u662f\u4e0d\u662f&#039;;&#039;\u5982\u679c\u662f\u5c31\u8df3\u8fc7\n}\n```\n<\/code><\/pre>\n<h1>2. \u4ec0\u4e48\u662fstatement?<\/h1>\n<pre><code>- statement\u662f\u6784\u6210lua\u7684\u4ee3\u7801\u5355\u5143\u5757,lua\u811a\u672c\u7531\u4e00\u7cfb\u5217\u7684statement\u6784\u6210\u7684\u5e8f\u5217\u7ec4\u5408\u800c\u6210\u53ef\u4ee5\u662f\u7531if\u3001while\u3001do\u3001for\u3001repeat\u3001func\u3001local\u3001return\u3001break\u3001expr\u7b49\u7c7b\u522b\n\n```\n\/\/ lua5.1 lparser.c\nstatic int statement(LexState *ls)\n{\n    switch (ls-&gt;t.token)\n    {\n    case TK_IF:\n        &#039;&#039;&#039;\n    case TK_WHILE:\n        &#039;&#039;&#039;\n    case TK_DO:\n        &#039;&#039;&#039;\n    case TK_FOR:\n        &#039;&#039;&#039;\n    case TK_REPEAT:\n        &#039;&#039;&#039;\n    case TK_FUNCTION:\n        &#039;&#039;&#039;\n    case TK_LOCAL:\n        &#039;&#039;&#039;\n    case TK_RETURN:\n        &#039;&#039;&#039;\n    case TK_BREAK:\n        &#039;&#039;&#039;\n    default:\n    {\n        exprstat(ls);\n    }\n    }\n}\n```\n<\/code><\/pre>\n<ul>\n<li>## 2.1 statement\u4e2d\u7684if\u7c7b\u522b<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">static void ifstat(LexState *ls, int line)\n{\n    FuncState *fs = ls-&gt;fs;\n    int flist;\n    int escapelist = NO_JUMP;\n    flist = test_then_block(ls); \/\/ \u89e3\u6790if \u4e4b\u540e\u5230\u4e0b\u4e00\u4e2aelseif\/else\u4e4b\u95f4\u7684\u4e1c\u897f\n    while (ls-&gt;t.token == TK_ELSEIF)\n    {\n        flist = test_then_block(ls); \/\/ \u89e3\u6790elseif \u4e4b\u540e\u5230\u4e0b\u4e00\u4e2aelseif\u4e4b\u95f4\u7684\u4e1c\u897f\n    }\n    if (ls-&gt;t.token == TK_ELSE)\n    {\n        block(ls);      \/* `else' part *\/\n    }\n}\n<\/code><\/pre>\n<ol>\n<li>if \u8bed\u53e5\u7684\u89e3\u6790\n<ul>\n<li>if\u7684\u6784\u6210\u4e00\u822c\u7531\n<pre><code class=\"line-numbers\">if cond then\nelseif cond then\nelse\nend\n<\/code><\/pre>\n<p>\u6784\u6210\n<\/li>\n<\/ul>\n<\/li>\n<li>\u5728test_then_block\u4e2d \u4f1a\u5c1d\u8bd5\u89e3\u6790 if\/elseif \u4e4b\u540e\u7684\u5185\u5bb9\n<pre><code class=\"line-numbers\">static int test_then_block(LexState *ls)\n{\n    int condexit;\n    condexit = cond(ls);\n    block(ls); \/* `then' part *\/\n    return condexit;\n}\n<\/code><\/pre>\n<\/li>\n<li>\u4e00\u822c\u5728if \/elseif \u4e4b\u540e,\u6211\u4eec\u4f1a\u5f97\u5230\u4e00\u4e2a\u8868\u8fbe\u5f0f \u5bf9\u5e94 \u662f\u5426\u7b26\u5408\u6761\u4ef6,\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u731c\u60f3 cond(ls)\u7684\u6784\u6210\u5e94\u8be5\u662f\n<pre><code class=\"line-numbers\">expr(ls, &amp;v)\n\u770b\u770b\u89e3\u6790\u7684\u503c\u662ftrue\u8fd8\u662ffalse\n\n<\/code><\/pre>\n<\/li>\n<li>\u89e3\u6790\u5b8c\u6761\u4ef6\u4e4b\u540e,\u5c31\u662f\u6761\u4ef6\u4e4b\u540e\u7684block\u4e86,\u6bcf\u4e2ablock\u91cc\u9762\u53c8\u53ef\u80fd\u5305\u542b\u5404\u79cd\u5404\u6837\u7684statement,\u90a3\u4e48\u5c31\u8ba4\u4e3a\u4ed6\u4eec\u53c8\u662f\u4e00\u4e2achunk\u91cd\u65b0\u89e3\u6790\n<pre><code class=\"line-numbers\">static void block(LexState *ls)\n{\n    '''\n    chunk(ls);\n    '''\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5230\u6b64\u4e00\u4e2a\u7b80\u5355\u7684ifstat\u7684\u89e3\u6790\u8fc7\u7a0b\u6211\u4eec\u5c31\u770b\u5b8c\u4e86<\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>## 2.1 statement\u4e2d\u7684while\u7c7b\u522b<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">static void whilestat(LexState *ls, int line)\n{\n\n    condexit = cond(ls); \/\/ \u6761\u4ef6\u9000\u51fa\n    checknext(ls, TK_DO); \/\/ \u68c0\u67e5\u4e4b\u540e\u662f\u4e0d\u662fdo\n    block(ls); \/\/ \u8fdb\u5165block\u5757\n    check_match(ls, TK_END, TK_WHILE, line); \/\/ \u68c0\u67e5\u4e4b\u540e\u662f\u4e0d\u662fend\n\n}\n\n<\/code><\/pre>\n<ol>\n<li>while \u8bed\u53e5\u7684\u89e3\u6790\n<ul>\n<li>while\u7684\u6784\u6210\u4e00\u822c\u7531\n<pre><code class=\"line-numbers\">while (\u6761\u4ef6) do\n'''\nend\n<\/code><\/pre>\n<p>\u6784\u6210\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<ul>\n<li>## 2.1 statement\u4e2d\u7684do\u7c7b\u522b<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">static void dostat(LexState *ls, int line)\n{\n    gafqX_next(ls);\n    block(ls); \/\/ \u8fdb\u5165block\u5757\n    check_match(ls, TK_END, TK_DO, line);\n\n}\n\n<\/code><\/pre>\n<ol>\n<li>do \u8bed\u53e5\u7684\u89e3\u6790\n<ul>\n<li>do\u7684\u6784\u6210\u4e00\u822c\u7531\n<pre><code class=\"line-numbers\">do\n'''\nend\n<\/code><\/pre>\n<p>\u6784\u6210\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1. chunk\u662f\u4ec0\u4e48\u5462? &#8211; Lua \u6267\u884c\u7684\u6bcf\u4e00\u6bb5\u4ee3\u7801\uff0c\u4f8b\u5982\u4e00\u4e2a\u6587\u4ef6\u6216\u4ea4\u4e92\u6a21\u5f0f\u4e0b\u7684\u4e00\u884c\u4ee3\u7801\uff0c\u90fd\u662f\u4e00\u4e2a\u5757\u3002\u66f4\u5177<a href=\"https:\/\/www.ccagml.com\/?p=506\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">lua\u4e2d\u7684statement\u7c7b\u522bif\u3001while\u3001do<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/506"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=506"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/506\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/506\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}