<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lua 语法速查：这套脚本到底是不是 Lua]]></title><description><![CDATA[<p dir="auto">先给结论：<strong>不是标准 Lua。</strong></p>
<p dir="auto">键鼠助手里的「Lua」是一个封闭的小语言——解析器只实现了一个子集，能用的命令和函数是两张写死的表（15 条命令 + 9 个函数）。它的语法长得像 Lua，是因为设计时刻意对齐了 Lua 5.3 的书写习惯；但内核、类型系统、标准库都不在。</p>
<p dir="auto">这意味着：<strong>别拿网上任何一篇「Lua 入门教程」直接往里套。</strong> 这篇把「像什么」和「哪里不一样」分开写清楚，写完的脚本至少不会卡在编译上。</p>
<h2>一、两套方言并存</h2>
<p dir="auto">解析器同时认两种写法：</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>方言</th>
<th>写法</th>
<th>用不用</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lua 风格</td>
<td><code>local x = v</code>、<code>if..then..elseif..else..end</code>、<code>while..do..end</code>、<code>for i=a,b,step..do..end</code>、<code>function f(p)..end</code>、<code>do..end</code>、<code>return</code>、<code>break</code></td>
<td><strong>推荐</strong>，文档化的就是这一套</td>
</tr>
<tr>
<td>遗留 BASIC 风格</td>
<td><code>If/Then/EndIf</code>、<code>While/Wend</code>、<code>For/Next</code>、<code>Sub/End Sub</code>、<code>Dim</code>、<code>Goto</code>、<code>Rem</code>、<code>Call</code>、<code>Exit Sub</code></td>
<td>老脚本兼容用，新脚本别写</td>
</tr>
</tbody>
</table>
<p dir="auto">两套不能混着嵌套。看到别人的脚本里出现 <code>EndIf</code>，那是老资产，不是你语法错。</p>
<h2>二、数据类型</h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>类型</th>
<th>字面量</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>integer</td>
<td><code>10</code>、<code>0x04</code>、<code>0b0100</code></td>
<td>十六进制和<strong>二进制</strong>都认，<code>0b0100</code> 等于 4</td>
</tr>
<tr>
<td>float</td>
<td><code>2.5</code>、<code>1e3</code></td>
<td><code>1e3</code> 是浮点 1000，不是整数</td>
</tr>
<tr>
<td>string</td>
<td><code>"a"</code></td>
<td>文档里只出现过双引号写法</td>
</tr>
<tr>
<td>boolean</td>
<td><code>true</code> / <code>false</code></td>
<td></td>
</tr>
<tr>
<td>array</td>
<td><code>Array(n)</code>、<code>StringArray(n, len)</code></td>
<td>容量在声明时定死</td>
</tr>
<tr>
<td>nil</td>
<td>—</td>
<td>见下面第 3 条差异，行为需要实测</td>
</tr>
<tr>
<td>字符串变量</td>
<td><code>StringVar(n)</code></td>
<td>固定容量的字符串</td>
</tr>
</tbody>
</table>
<p dir="auto">数值遵循 Lua 5.3 的整数 / 浮点双类型规则，别的地方不一定这么讲究：</p>
<pre><code class="language-lua">local half  = 7 / 2      -- 3.5，/ 永远是浮点除法
local whole = 7 // 2     -- 3，向下整除
local down  = -7 // 2    -- -4，注意是向下取整，不是向零
local mod   = -7 % 3     -- 2，floor 取模
local rem   = 7 % 3      -- 1
</code></pre>
<p dir="auto"><code>-7 // 2</code> 得 <code>-4</code> 而不是 <code>-3</code>，这是最容易算错的一行。</p>
<h2>三、与标准 Lua 的六处差异</h2>
<p dir="auto">这六条是实际写脚本时会被绊住的地方，按踩坑概率排序。</p>
<h3>1. 数组下标从 0 开始</h3>
<p dir="auto">标准 Lua 的数组从 1 开始，<strong>这里从 0 开始</strong>。</p>
<pre><code class="language-lua">local nums = Array(5)
nums[0] = 1        -- 第一个元素
nums[1] = 2        -- 第二个
nums[2] = nums[0] + nums[1]   -- 3
</code></pre>
<p dir="auto">把从网上抄来的 <code>for i = 1, 10</code> 套在长度 10 的数组上，会多读一个不存在的槽位。写循环时先想清楚边界是 <code>0..n-1</code> 还是 <code>1..n</code>。</p>
<h3>2. 没有通用 table</h3>
<p dir="auto">标准 Lua 的 <code>{}</code> 构造器、<code>#t</code> 取长度、<code>pairs</code> / <code>ipairs</code> 遍历、<code>table.insert</code> 这些，文档里一条都没有，按不可用处理。</p>
<p dir="auto">能用的容器只有三种：</p>
<pre><code class="language-lua">local nums  = Array(5)              -- 整数数组，5 个槽
local name  = StringVar(16)         -- 单个字符串变量
local names = StringArray(4, 16)    -- 4 个字符串，每个 16
</code></pre>
<p dir="auto">容量是写死的，运行时不会自动扩容。循环里往 <code>Array(5)</code> 写第 6 个元素，结果不可预期——按越界处理。</p>
<h3>3. <code>math.tointeger</code> 失败返回 0，不是 nil</h3>
<p dir="auto">文档在数据类型表里列了 <code>nil</code>，但在 <code>math.tointeger</code> 的说明里又写「无 nil 类型，转换失败返回 0」。</p>
<pre><code class="language-lua">local i1 = math.tointeger(7.0)   -- 7
local i2 = math.tointeger(1.5)   -- 0（不是 nil）
</code></pre>
<p dir="auto">两处说法不一致，写代码时<strong>按「返回 0」处理</strong>更安全：转换失败和真的等于 0 无法区分，所以别把 <code>math.tointeger</code> 的返回值当判断依据。确定要判整数时用 <code>math.type(v)</code>，它返回 <code>"integer"</code> 或 <code>"float"</code>。</p>
<h3>4. 没有字符串库</h3>
<p dir="auto"><code>string.format</code>、<code>string.sub</code>、<code>string.find</code>、<code>#s</code> 取长度，文档里都没有。字符串能做的只有拼接：</p>
<pre><code class="language-lua">local text = "键鼠" .. "助手" .. " v1.0"
</code></pre>
<p dir="auto">需要拼接数字时注意整数会变成十进制字面，小数部分按浮点规则；要控制格式，只能靠 <code>Log</code> 的 printf 风格参数输出到日志里看：</p>
<pre><code class="language-lua">Log("count=%d name=%s ratio=%g", 3, "键鼠助手", 1000 * 1.5)
</code></pre>
<p dir="auto">但 <code>Log</code> 是输出，不返回字符串——不能拿它拼出格式化后的文本再 <code>TypeText</code> 出去。需要「输入格式化文本」时，只能把定值写死或分段 <code>TypeText</code>。</p>
<h3>5. <code>0b</code> 二进制字面量</h3>
<p dir="auto"><code>0b0100</code> = 4。标准 Lua 5.3 不支持这个写法。写键码时用 <code>0b</code> 更直观，但换算成十进制或十六进制也一样，看个人习惯。</p>
<h3>6. <code>math.random</code> 只有双参形式</h3>
<pre><code class="language-lua">local rnd = math.random(50, 100)   -- [50, 100] 闭区间随机整数
</code></pre>
<p dir="auto">文档只给了这一种形式，没有无参和单参版本。要模拟 0/1 就用 <code>math.random(0, 1)</code>。</p>
<h2>四、控制语句</h2>
<pre><code class="language-lua">local count = 0

while count &lt; 5 do
    count = count + 1
    if count == 1 then
        -- 第一次
    elseif count == 3 then
        -- 第三次
    else
        -- 其它
    end
end

for i = 1, 3 do
    if i == 2 then
        -- 跳过处理
    end
end

function add(a, b)
    return a + b
end

local sum = add(2, 3)
</code></pre>
<p dir="auto">注释用 <code>--</code>，只支持单行。</p>
<p dir="auto"><code>break</code> 只能跳出最内层循环；嵌套循环要跳两层，得自己加标志变量逐层判断。</p>
<h2>五、踩坑速查</h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>现象</th>
<th>大概率原因</th>
</tr>
</thead>
<tbody>
<tr>
<td>编译报 <code>end</code> 相关错误</td>
<td>容器块没闭合；<code>for</code> 后面漏 <code>do</code>，<code>while</code> 后面漏 <code>do</code></td>
</tr>
<tr>
<td>数组读出来是 0 或者乱值</td>
<td>下标从 1 开始写了，或者越界</td>
</tr>
<tr>
<td><code>math.tointeger</code> 结果不对</td>
<td>转换失败返回 0，被当成有效值用了</td>
</tr>
<tr>
<td>想 <code>string.format</code> 拼文本却编译不过</td>
<td>没有字符串库，改用 <code>..</code> 拼接</td>
</tr>
<tr>
<td>循环次数比预期多一轮</td>
<td><code>0..n-1</code> 与 <code>1..n</code> 混用</td>
</tr>
<tr>
<td><code>Log</code> 输出看不懂</td>
<td>首个参数含 <code>%d</code>/<code>%s</code>/<code>%f</code> 时按 printf 走，不含则按空格分隔；两种模式混用容易错位</td>
</tr>
</tbody>
</table>
<h2>六、不确定的行为怎么确认</h2>
<p dir="auto">这套语言的文档只覆盖了「已实现且稳定」的部分，边界行为（比如 <code>nil</code> 到底能不能赋值、字符串容量超了会怎样）没有成文说明。</p>
<p dir="auto">确认方法只有一条，但很有效：</p>
<pre><code class="language-lua">local v = math.type(nil)
Log("type=%s", v)
</code></pre>
<p dir="auto">编译 → 运行 → 看输出窗口。<strong>编译器通过 + 运行输出符合预期</strong>，才算确认；只有编译通过不算。</p>
<p dir="auto">实测出新结论欢迎发到版块里，这类帖子比任何教程都有用——文档没写的地方就是靠这样一条条补出来的。</p>
]]></description><link>https://www.byteohm.com/topic/60/lua-语法速查-这套脚本到底是不是-lua</link><generator>RSS for Node</generator><lastBuildDate>Thu, 19 Nov 2026 06:23:57 GMT</lastBuildDate><atom:link href="https://www.byteohm.com/topic/60.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Nov 2026 16:46:09 GMT</pubDate><ttl>60</ttl></channel></rss>