{"id":508,"date":"2022-01-29T10:24:13","date_gmt":"2022-01-29T02:24:13","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=508"},"modified":"2022-07-06T10:24:32","modified_gmt":"2022-07-06T02:24:32","slug":"lua%e4%b8%ad%e7%9a%84statement%e7%b1%bb%e5%88%abfor","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=508","title":{"rendered":"lua\u4e2d\u7684statement\u7c7b\u522bfor"},"content":{"rendered":"<ul>\n<li>## 2.1 statement\u4e2d\u7684forstat\u7c7b\u522b<\/li>\n<\/ul>\n<ol>\n<li>for \u8bed\u53e5\u7684\u89e3\u6790\n<ul>\n<li>for\u7684\u6784\u6210\u4e00\u822c\u7531\u4ee5\u4e0b\u4f8b\u5b50\u6784\u6210<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">    for i = 1, 10 do\n    end\n    for i = 1, 10, 2 do\n    end\n\n    for a,b in ipairs({}) do\n\n    end\n    for a,b in pairs({}) do\n\n    end\n<\/code><\/pre>\n<\/li>\n<li>forstat\u89e3\u6790\n<pre><code class=\"line-numbers\">static void forstat(LexState *ls, int line)\n{\n\n\n    varname = str_checkname(ls); \/\/ \u7b2c\u4e00\u4e2a\u53d8\u91cf\u540d\n    switch (ls-&gt;t.token)\n    {\n    case '=':\n        fornum(ls, varname, line);\n        break;\n    case ',':\n    case TK_IN:\n        forlist(ls, varname);\n        break;\n    }\n    check_match(ls, TK_END, TK_FOR, line);\n    leaveblock(fs); \/* loop scope (`break' jumps to this point) *\/\n}\n\n<\/code><\/pre>\n<\/li>\n<li>\u5f53\u503c\u662f fornum \u7684\u65f6\u5019\n<pre><code class=\"line-numbers\">    static void fornum(LexState *ls, TString *varname, int line)\n{\n    FuncState *fs = ls-&gt;fs;\n    int base = fs-&gt;freereg;\n    new_localvarliteral(ls, \"(for index)\", 0);\n    new_localvarliteral(ls, \"(for limit)\", 1);\n    new_localvarliteral(ls, \"(for step)\", 2);\n    new_localvar(ls, varname, 3);\n    checknext(ls, '=');\n    exp1(ls); \/* initial value *\/\n    checknext(ls, ',');\n    exp1(ls); \/* limit *\/\n    if (testnext(ls, ','))\n        exp1(ls); \/* optional step *\/\n    else\n    { \/* default step = 1 *\/\n        gafqK_codeABx(fs, OP_LOADK, fs-&gt;freereg, gafqK_numberK(fs, 1));\n        gafqK_reserveregs(fs, 1);\n    }\n    forbody(ls, base, line, 1, 1);\n}\n<\/code><\/pre>\n<\/li>\n<li>\u89e3\u6790\u65b0\u7684local\u53d8\u91cf\n<ul>\n<li>\u6ce8\u518c\u4e00\u4e2a\u65b0\u7684local\u53d8\u91cf,\u4f1a\u5148\u68c0\u67e5local\u53d8\u91cf\u7684\u6570\u91cf,\u7136\u540e\u5c06\u53d8\u91cf\u653e\u5165proto\u7684locvars\u4e2d,\u7136\u540e\u8fd4\u56de \u5f53\u524d\u672c\u5730\u53d8\u91cf\u7684\u6570\u91cf<\/li>\n<\/ul>\n<pre><code class=\"line-numbers\">\/\/ \u89e3\u6790\u65b0\u7684local\u53d8\u91cf\n    static void new_localvar(LexState *ls, TString *name, int n)\n    {\n        FuncState *fs = ls-&gt;fs;\n        gafqY_checklimit(fs, fs-&gt;nactvar + n + 1, GAFQI_MAXVARS, \"local variables\");\n        fs-&gt;actvar[fs-&gt;nactvar + n] = cast(unsigned short, registerlocalvar(ls, name));\n    }\n\n    static int registerlocalvar(LexState *ls, TString *varname)\n{\n    FuncState *fs = ls-&gt;fs;\n    Proto *f = fs-&gt;f;\n    int oldsize = f-&gt;sizelocvars;\n    gafqM_growvector(ls-&gt;L, f-&gt;locvars, fs-&gt;nlocvars, f-&gt;sizelocvars, LocVar, SHRT_MAX, \"too many local variables\");\n    while (oldsize &lt; f-&gt;sizelocvars)\n        f-&gt;locvars[oldsize++].varname = NULL;\n    f-&gt;locvars[fs-&gt;nlocvars].varname = varname;\n    gafqC_objbarrier(ls-&gt;L, f, varname);\n    return fs-&gt;nlocvars++;\n}\n<\/code><\/pre>\n<\/li>\n<li>\u89e3\u6790\u8868\u8fbe\u5f0fexp1\n<pre><code class=\"line-numbers\">static int exp1(LexState *ls)\n{\n    expdesc e;\n    int k;\n    expr(ls, &amp;e);\n    k = e.k;\n    gafqK_exp2nextreg(ls-&gt;fs, &amp;e);\n    return k;\n}\n\nvoid gafqK_exp2nextreg(FuncState *fs, expdesc *e)\n{\n    gafqK_dischargevars(fs, e);\n    freeexp(fs, e);\n    gafqK_reserveregs(fs, 1);\n    exp2reg(fs, e, fs-&gt;freereg - 1);\n}\n\n\/\/ \u628a\u8868\u8fbe\u5f0f\u653e\u8fdb\u5bc4\u5b58\u5668\u4e2d\nstatic void exp2reg(FuncState *fs, expdesc *e, int reg)\n{\n    discharge2reg(fs, e, reg);\n    e-&gt;f = e-&gt;t = NO_JUMP;\n    e-&gt;u.s.info = reg;\n    e-&gt;k = VNONRELOC;\n}\n\n\/\/ reg\u662f\u8981\u88ab\u4f7f\u7528\u7684\u5bc4\u5b58\u5668\u7684\u4f4d\u7f6e\nstatic void discharge2reg(FuncState *fs, expdesc *e, int reg)\n{\n    gafqK_dischargevars(fs, e);\n    switch (e-&gt;k)\n    {\n    case VKNUM:\n    {\n        gafqK_codeABx(fs, OP_LOADK, reg, gafqK_numberK(fs, e-&gt;u.nval)); \/\/ \u8fd9\u91cc\u751f\u6210\u4e00\u4e2a\u64cd\u4f5c\u6307\u4ee4, \u628a\u503c\u5b58\u5230\u5e38\u91cfk\u91cc\u9762\u53bb\n        break;\n    }\n    }\n    e-&gt;u.s.info = reg;\n    e-&gt;k = VNONRELOC;\n}\n<\/code><\/pre>\n<\/li>\n<li>\u89e3\u6790\u5faa\u73af\u7684body\n<pre><code class=\"line-numbers\">\/\/ isnum \u662f\u5426\u662f\u6570\u5b57\u7c7b\u578b\u7684\u5faa\u73af for i = 1, 10 do\nstatic void forbody(LexState *ls, int base, int line, int nvars, int isnum)\n{\n\n    checknext(ls, TK_DO);\n    prep = isnum ? gafqK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : gafqK_jump(fs);\n    enterblock(fs, &amp;bl, 0);\n    adjustlocalvars(ls, nvars);\n    gafqK_reserveregs(fs, nvars);\n    block(ls);\n    leaveblock(fs); \/* end of scope for declared variables *\/\n    gafqK_patchtohere(fs, prep);\n    endfor = (isnum) ? gafqK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : gafqK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);\n    gafqK_fixline(fs, line); \/* pretend that `OP_FOR' starts the loop *\/\n    gafqK_patchlist(fs, (isnum ? endfor : gafqK_jump(fs)), prep + 1);\n}\n\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p><!-- \n \n- ## 2.1 statement\u4e2d\u7684expr\u7c7b\u522b\n \n1. exprstat \u5206\u4e3a\n     - func\n        - \u4f8b\u5982 test()\n     - assignment\n        - \u4f8b\u5982 a = test()\n1. primaryexp \u5206\u4e3a\n     - prefixexp\n1. prefixexp \u5206\u4e3a\n     - \u53d8\u91cf\u540dTK_NAME\n     - '('expr')'\n1. primaryexp \u5728\u5904\u7406\u5b8cprefixexp\u4e4b\u540e,\u4ed6\u7684\u524d\u9762\u53ef\u80fd\u662f\u4e00\u4e2a\u53d8\u91cf\u540d,\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u88ab()\u5305\u8d77\u6765\u7684\u8868\u8fbe\u5f0f,\u90a3\u4e48\u63a5\u4e0b\u53bb\u53ef\u80fd\u9047\u5230\u7684\u662f\n    - . \u51fd\u6570\u540d. \u53d6\u503c\n      - .\u4e4b\u540e \u53c8\u662f\u4e00\u4e2a\u540d\u79f0 \u7c7b\u4f3c a.b \u6216\u8005 a:b()\n    - [ \u8ba4\u4e3a\u662ftable\u8981\u53d6\u5185\u5bb9\n      - expr  \u7c7b\u4f3c \u8981\u53d6 a[expr] \u7684\u503c\n    - : \u51fd\u6570\u540d: \u8ba4\u4e3a\u63a5\u4e0b\u53bb\u662f\u51fd\u6570\n      - :\u4e4b\u540e \u53c8\u662f\u4e00\u4e2a\u540d\u79f0 a:b()\n    - ( \u3001\u5b57\u7b26\u4e32 \u3001{  \u51fd\u6570\u7684\u53c2\u6570\n1. assignment \u5206\u4e3a\n    - , \u5219\u53c8\u662f\u4e00\u4e2a primaryexp\n    - = \u5219\u662f\u4e00\u4e2a explist1\n1. explist1\n    - , \u89e3\u6790 {expr,expr,expr}\n1. expr\n    - unop\n        - '-'\n        - not\n        - '#'\n    - simpleexp\n        - \u6570\u5b57\n        - \u5b57\u7b26\u4e32\n        -  nil\n        -  true\n        - false\n        -  ...\n        -  {\n        -  \u51fd\u6570\u4f53\n        -  primaryexp\n    - binop\n        - \u6709'+' | '-' | '*' | '\/' | '%' | '^' | '&' | '|' | '~' | '<<' | '>>' | '..' | '>' | '<' | '=' | '>=' | '<=' | '==' | '~=' | and | or\n  --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>## 2.1 statement\u4e2d\u7684forstat\u7c7b\u522b for \u8bed\u53e5\u7684\u89e3\u6790 for\u7684\u6784\u6210\u4e00\u822c\u7531\u4ee5\u4e0b\u4f8b\u5b50\u6784\u6210 f<a href=\"https:\/\/www.ccagml.com\/?p=508\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">lua\u4e2d\u7684statement\u7c7b\u522bfor<\/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\/508"}],"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=508"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}