OneScript 语法介绍
OneScript 使用标准 Lua 5.5 语法。本页只列写程序必用的部分。
注释
-- 单行注释
--[[ 多行注释 ]]
注意:注释和字符串之外只允许 ASCII 字符(全角标点、BOM 会导致校验失败)。
变量
local a = 1.5 -- 局部变量(推荐)
local name = "X" -- 字符串
local points = {10, 20, 30} -- 表(数组,下标从 1 开始)
counter = counter + 1 -- 不带 local 的是全局变量(仅本脚本内有效)
类型:number(双精度浮点)、string、boolean、table、nil。
运算符
算术:+ - * / ^ %(注意:^ 是幂运算,不是异或)
比较:== ~= < <= > >=
逻辑:and or not
字符串连接:"pos=" .. value
程序流程
-- if
if err ~= 0 then
Print("error " .. err)
elseif err == 0 then
Print("ok")
else
Print("unknown")
end
-- while
while GetGlobal(0) < 100 do
Dwell(10)
end
-- for(数值)
for i = 1, 10 do -- i = 1..10,步长 1
Print(i)
end
for i = 10, 1, -2 do -- 步长 -2
Print(i)
end
-- for(遍历表)
for i, p in ipairs(points) do
MoveAbs(X, p, F, 100)
WaitMotion(X)
end
函数(子程序)
-- 必须先定义后调用
local function moveTo(pos, speed)
MoveAbs(X, pos, F, speed)
WaitMotion(X)
end
moveTo(50, 200)
可用标准库
沙箱只开放:table、string、math、utf8 和基础函数(print 不可用,请用 Print;pcall、tostring、ipairs 等可用)。
不开放:io、os、package、coroutine、debug(无文件、无系统调用、无网络)。
轴名与关键字常量
- 每个已配置轴名都是全局常量,值为轴号:
X=0、Y=1…。指令一律用轴名(Enable(X)),不要用数字。 - 关键字常量:
F(速度)、I/J(圆心偏移)、E(内部使用)。另有字符串写法"F"、"R"、"I"、"J"、"K"、"E"等效("K"/"R"只能字符串写法)。