打开主菜单
首页
随机
登录
设置
关于DOLLARS
免责声明
DOLLARS
搜索
查看“浪語”的源代码
←
浪語
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您必须确认您的电子邮件地址才能编辑页面。请通过
参数设置
设置并确认您的电子邮件地址。
您可以查看和复制此页面的源代码。
一種 JavaScript 的方言,因為由浪打所創因而得名。 目前你可以在浪打所作的[https://chrome.google.com/webstore/detail/drrr-chatbot-extension/fkmpnkcjocenkliehpdhlfbmdmdnokgm 聊天插件]上使用他。 [[File:Lambda language.png|缩略图|每三十秒使用一言 API 換一次部屋描述。]] 資料型態大概以下這些: # Boolean (true/false) # Number (-1/0/3.14) # String ("hello, lambda!") # Array ([1,2,3,4]) # Object ({x: 1.34, y: 4.5}) # Function ((a, b) => a + b) 因為這個語言會直譯成 JavaScript, 所以 JS 裡的物件其實都能在這個語言裡面進行構造、使用。 由於參考到 JS 的 globalThis,所以你也可以直接使用 global 裡面的東西。 <syntaxhighlight lang="javascript" line="1"> console.log("hello world"); alert("it's a alert!"); </syntaxhighlight> 和 JS 不同的地方在於,浪語裡面所有東西都是 Expression,都一定有回傳值。 這個語言通常不會出現 undefined,取而代之的是,他回傳一個空物件 {}。 分號 (;) 基本上都可以省略,或者你可以使用分號回傳一個空物件。 所有第一次參考到的變數,如果不存在,便會以空物件創建。 <syntaxhighlight lang="javascript" line="1"> print(x) // not defined, show {} </syntaxhighlight> 這個語言也可以發 ajax,可以實作一些有趣的功能。 <syntaxhighlight lang="javascript" line="1"> fetch("https://v1.hitokoto.cn") .then(response => response.json()) .then(result => { print(result.hitokoto); }); </syntaxhighlight> 基本的 +, -, *, / 運算子都有支援,+=, ++, -- 之類的也有,但目前不支援三元運算子。 目前支援 if else 語法,但 switch 沒有支援。 迴圈語法支援 while, for loop, for in, for of,基本上和 JS 一樣,但沒有 do while。 迴圈的小括弧可以省略。 <syntaxhighlight lang="javascript" line="1"> // for(i = 0; i < 10; i++) is also good for i = 0 i < 10 i++ print(i) j = 0 while(j < 3){ print(j); j++; } for(i of [1,2,3,4]) print(i); for(j in {tom: 1, allen: 2}) print(j); </syntaxhighlight> 沒有支援 function 語法,僅支援 arrow function,預設回傳最後一個 expression。 <syntaxhighlight lang="javascript" line="1"> f = (x) => if(x <= 0) 0 else if(x == 1) 1 else f(x - 1) + f(x - 2) [0, 1, 2, 3, 4, 5, 6].map(f) // => [0,1,1,2,3,5,8] </syntaxhighlight> Scope ({}) 如果被當作參數,或者是 right value 綁定時,會被 lift 成沒有參數的 function。 <syntaxhighlight lang="javascript" line="1"> f = (a, b) => a + b; print(f(1, 4)) // 5 g = { args[0] + args[1] }; print(g(1, 2)) // 3 </syntaxhighlight> 跟 js 的 arguments 一樣,在 function 或是 lifted scope 中,浪語的 args 可以拿到參數列。 我提供了一些和 drrr 有關的 function,綁定在 drrr 這個物件中。 <syntaxhighlight lang="javascript" line="1"> drrr.title("設定部屋名稱") drrr.descr("設定部屋描述") drrr.print("發送訊息") drrr.dm("人名", "私訊訊息") drrr.chown("人名") // 更換房主 drrr.leave() // 離開部屋 drrr.play("關鍵字", "音源", "數字") // 後兩個參數是選擇性,和插件的 play 一樣。 drrr.join("房間 ID") drrr.create("房間名稱", "房間描述", 房間人數:數字, "語系") // 基本上都有預設參數,所以要傳幾個參數都行 // 還有一些幫你抓好的變數 loc // 現在位置(大廳或房間 "lounge" / "room") profile // 個人訊息 room // 房間訊息 users // 房間成員 info // 跟個人訊息有點像 rooms // 所有房間,大廳狀態 // 有時你會需要更新,可以透過以下函數去更新他們 updateLounge(callback); updateProfile(callback); updateLoc(callback); </syntaxhighlight> 目前浪語裡,除了先前提到的保留字外,還有一些特殊的關鍵字: - state 宣告一個 state,與 going 搭配使用,有自己的 scope。 但 going 是直接跳去那個地方不會回來。 如果你要回來的話,請使用 visit。 <syntaxhighlight lang="javascript" line="1"> state welcome { print("hello world"); going bye } state bye { print("bye"); // done. } going welcome </syntaxhighlight> <syntaxhighlight lang="javascript" line="1"> state welcome { print("hello world"); going bye } state bye { print("bye"); // because "visit welcome", so back to visit } visit welcome // back from bye print("done"); // done. </syntaxhighlight> - event 處理相關事件,event 種類和 event action 裡的說明一樣。 <syntaxhighlight lang="javascript" line="1"> // 冒號 (:) 後面是 RegExp,如果匹配才呼叫。 // 適用於 user 和 content (第一和第二個參數) event msg (user: "lambda", content, url, tripcode, req){ drrr.print(user + " 叫了一下"); } // 參數個數和名稱都可以任意,看你需求 event join (user){ drrr.print("welcome " + user); } </syntaxhighlight> - timer 用於定時執行 function。 <syntaxhighlight lang="javascript" line="1"> // 單位是 ms,所以 10000 是十秒 timer 10000 print("hello world"); // print 會被自動 lift 成 function // 三十秒使用一言換一次部屋描述 timer 30000 fetch("https://v1.hitokoto.cn") .then(response => response.json()) .then(result => { drrr.descr(result.hitokoto); }); // 等價於第一個範例 timer 10000 { print("hello world"); } // 等價於第一個範例 timer 10000 () => { print("hello world"); } </syntaxhighlight> - later 用於延遲執行 function,和 timer 很像,但是只執行一次。 <syntaxhighlight lang="javascript" line="1"> later 10000 print("hello world") </syntaxhighlight> 一些功能實作: 歡迎回來功能: <syntaxhighlight lang="javascript" line="1"> guests = users.map((x)=>x.name); event join (user) => { if guests.includes(user) drrr.print("welcome back, " + user) else guests.push(user) } print(guests) </syntaxhighlight> 猜數字遊戲: <syntaxhighlight lang="javascript" line="1"> valid = (digits) => (new Set(digits.split(""))).size === 4 generate = () => { while(!valid(digits = String(Math.floor(1000 + Math.random() * 9000)))); digits } gnjdg = (guess, callback) => { if(valid(guess)){ d = theNumber.split("") g = guess.split("") c = g.map((v)=>d.includes(v)).reduce((a, b)=>a+b) a = g.map((v, idx)=>d[idx] === g[idx]).reduce((a, b)=>a+b) b = c - a callback( if(a === 4) "Your Number is Correct" else guess + ":" + String(a) + "A" + String(b) + "B" ) } else callback("guess number must be 4 non-repeat digits" + guess); } theNumber = generate() event msg (user, cont: "^\\d\\d\\d\\d$") => gnjdg(cont, drrr.print) event msg (user, cont: "^new$") => theNumber = generate() event msg (user, cont: "^ans$") => drrr.print(theNumber) </syntaxhighlight> 歡迎貴賓: <syntaxhighlight lang="javascript" line="1"> // 房間上限 9.0001 人 event join (user) => { if users.length == 10 drrr.print("/me恭喜成為 0.0001 人,你終於不做人類了嗎!"); else drrr.print("/me歡迎第 " + String(users.length) + " 個貴賓!"); } </syntaxhighlight>
返回
浪語
。