程式碼生成
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
程式碼產生是指將工作區中的區塊轉換為可執行的程式碼字串的程序。
程式碼產生功能非常重要,因為它可讓區塊實際執行操作,例如評估算術運算式、在迷宮中移動角色,或設定網路商店!
您無法直接「執行」區塊。而是產生程式碼字串,然後執行這些字串。
語言代碼產生器
如要產生程式碼,您必須選擇要產生的文字語言。這是因為每種語言都有專屬的程式碼產生器。
語言程式碼產生器 (通常稱為程式碼產生器) 是一種類別,可處理產生特定語言程式碼的規則,但不針對個別區塊。舉例來說,它會處理格式化註解、縮排陳述式和引號字串等事項。
Blockly 提供 5 個內建程式碼產生器:
- JavaScript ES5
- Python 3
- Lua 5.1
- Dart 2
- PHP 7
如果這個清單未包含您要產生程式碼的語言,您可以建立自訂語言程式碼產生器。如需簡單範例,請參閱「建構自訂產生器」程式碼研究室,該研究室會建立 JSON 語言程式碼產生器。如需更複雜的範例,請參閱 JavaScript 程式碼產生器。請注意,您也需要為要使用的任何內建區塊編寫區塊程式碼產生器。
區塊程式碼產生器
每個區塊都會負責產生自己的程式碼。建立區塊時,您需要為每種要支援的語言編寫個別的區塊程式碼產生器。
區塊程式碼產生器是會將該區塊的程式碼以字串形式傳回的函式。舉例來說,比較兩個數字的區塊會傳回 'a < b'
格式的字串,而代表 if 陳述式的區塊會傳回 'if (...) {\n...\n};\n'
格式的字串。
import {javascriptGenerator} from 'blockly/javascript';
import {pythonGenerator} from 'blockly/python';
// Write block-code generators for JavaScript and Python.
javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) { /* ... */ };
pythonGenerator.forBlock['my_custom_block'] = function(block, generator) { /* ... */ };
語言代碼產生器會呼叫區塊程式碼產生器。
詳情請參閱「區塊程式碼產生器」。
產生及執行程式碼
應用程式隨時可以產生代碼。舉例來說,當使用者點選按鈕或每次變更時,系統就可能會產生程式碼。
產生程式碼後,您需要瞭解如何執行程式碼。決定如何執行這項操作的做法非常特定於應用程式,且超出 Blockly 的範圍。
詳情請參閱「產生及執行程式碼」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-25 (世界標準時間)。"],[[["Code generation transforms Blockly blocks into executable code strings in various programming languages like JavaScript, Python, Lua, Dart, and PHP."],["Blockly offers built-in code generators for these languages, accessible through modules, Unpkg, or local scripts, along with the option to create custom generators."],["Generating code involves defining how individual blocks translate to code in each target language, including handling fields, inner blocks, and code concatenation."],["Code generation can be triggered on demand or continuously as the user interacts with the blocks, allowing for dynamic updates and code execution."],["Developers can add preamble or postscript code to the generated output for including external definitions or initiating specific behaviors."]]],["Code generation transforms visual blocks into executable code strings. A code generator handles language-specific formatting. Blockly offers built-in generators for JavaScript, Python, Lua, Dart, and PHP, accessible via modules, Unpkg, or local scripts. Custom generators are also supported. Each block's code generation rules must be defined per language. Generation occurs on demand or continuously via event listeners. Optional preamble/postscript code can be added. Execution of generated code is application-specific, and for JavaScript, JSInterpreter is suggested.\n"]]