本頁面說明分身機器人簡易語言的語法規範、內置函數以及安全限制。
當前直譯器穩定版本為 v4.1.0+

核心結構 (Core Structure)

#load <lib> // 載入內建庫
begin; // 主程式開始
  try; // 嘗試區塊
    // 變數賦值
    val = 100;
    // 輸出結果 (必填)
    output {"結果是: " + val};
  except {error}; // 將錯誤訊息放進error變數
    output {"Error" + error}; // 用輸出而非錯誤訊息回傳以免意外終止程式
  // except; 不進行變數賦值
  endtry;
end; // 主程式結束
指令/關鍵字範例說明
#load#load <math>加載外部擴充庫(如 math, message)
begin / endbegin; ... end;定義程式執行的主區塊,需加分號
outputoutput {content};設定程式最終回傳內容,僅執行一次
//// 註解內容單行註解,執行時會被忽略
;res = a + b;指令結尾必須加上分號(結構關鍵字除外)
try / except / endtrytry; ... except; ... endtry;捕捉區塊內的錯誤且不使用意外終止程式

流程控制 (Control Flow)

支援條件判斷結構,讓腳本能根據不同情況做出反應。每個 if 區塊都必須以 endif 結尾。

if {條件1};
  output {"情況 A"};
elif {條件2};
  output {"情況 B"};
else;
  output {"其他情況"};
endif;
if {條件1 or 條件2}; // if {條件1 || 條件2};
  output {"情況 A"};
elif {條件3 and 條件4}; // elif {條件3 && 條件4};
  output {"情況 B"};
else;
  output {"其他情況"};
endif;
⚠️ 條件判斷注意事項:

函式 (Function Definition)

使用 func 封裝重複邏輯。所有自定義函式必須定義在 begin 區塊之前。

函式定義語法

func <名稱> {參數1 參數2};
  res = 參數1 + 參數2;
  sendback {res}; 
endfunc;

如何調用函式

begin;
  // 使用 call 關鍵字調用
  res = call <名稱>{10 20};
  output {res};
end;
⚠️ 函式規則與限制:

方法 (Method)

使用 method 封裝應用邏輯。不可由使用者自訂。

如何調用方法

begin;
  // 不須使用 call 關鍵字調用
  wait{0.2}; // 等待0.2秒
  val1 = min{1 2}; // 取兩者最小值
  val2 = max{1 2}; // 取兩者最大值
  val3 = abs{-5}; // 取數值之絕對值
  // output {};
end;
⚠️ 方法規則與限制:

內建庫與函數註冊

類別函數/屬性回傳型態類型說明
math lib math.pi / math.efloatvariable圓周率與自然常數
math.add{a b}floatfunction回傳 a + b 的結果
math.multiple{a b}floatfunction回傳 a * b 的結果
math.subtruct{a b}floatfunction回傳 a - b 的結果
message lib message.usernamestrvariable訊息作者名稱
message.useridintvariable訊息作者 ID
message.idintvariable訊息 ID
message.contentstrvariable觸發程式的訊息內容
message.createatfloatvariable訊息發送時間
message.prefixstrvariable指令前綴字
message.commandstrvariable指令名稱
requests lib requests.get{url headers params}datafunction進行get並附帶headers和params
requests.post{url headers json}datafunction進行post並附帶headers和json
time lib time.timestampfloatvariable取得目前的timestamp
time.asctimestrvariable取得目前的asctime
list list.add{list val}nonemethod在列表末尾新增一個元素
list.pop{list idx}nonemethod移除並回傳指定索引的元素
list.get{list idx}anymethod取得指定索引的元素內容
list.count{list str}intmethod取得指定元素的數量
list.length{list}intmethod取得列表總長度
list.random{list n}listmethod隨機抽取 n 個元素 (n 可省略)
dict dict.set{dict key item}nonemethod在字典設定 key: items
dict.clear{dict}nonemethod清除整個字典
dict.get{dict key}anymethod回傳指定key的item
dict.length{dict}intmethod回傳字典長度
dict.randomkeys{dict n}anymethod隨機從key抽取 n 個元素 (n 可省略)
dict.randomitems{dict n}anymethod隨機從item抽取 n 個元素 (n 可省略)
dict.del{dict key}anymethod刪除指定key
string str.upper{text}strmethod轉為全大寫
str.lower{text}strmethod轉為全小寫
str.replace{text a b}listmethod以指定文字a取代文字b
str.split{text sep}listmethod以指定字串分割文字
system turntype{val type}anymethod轉換型態 (str, int, float, list)
type{val}strmethod確認變數型態
wait{time}nonemethod暫停執行 (限制 0.1 ~ 0.5 秒)
min{val val}floatmethod取兩者最小值
max{val val}floatmethod取兩者最大值
abs{val}floatmethod取數值之絕對值
round{val val}floatmethod取數值val後五捨六入

運行安全性驗證 (Constraints)

⚠️ 資源與安全限制:

完整程式範例

#load <time>
#load <message>

func welcome {name};
  res = "你好, " + name;
  sendback {res};
endfunc;

begin;
  user = message.username;
  greet = call welcome{user};
      
  for i in 3;
    nowstamp = round{time.timestamp 0};
    output_msg = greet + " 第 " + i + " 次打招呼\n現在的timestamp是:" + nowtimestamp;
  endfor;
      
  output {output_msg};
end;

準備好開始使用了嗎?