研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
代码生成
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
代码生成是将工作区中的代码块转换为可执行的代码字符串的过程。
代码生成非常重要,因为它让代码块能够实际执行操作,例如求解算术表达式、让角色在迷宫中移动或配置网店!
您无法直接“运行”代码块。而是生成代码字符串,然后执行这些字符串。
语言代码生成器
如需生成代码,您必须选择要生成哪种基于文本的语言。这是因为每种语言都有自己的代码生成器。
语言代码生成器(通常称为代码生成器)是一个类,用于处理生成特定于给定语言(但不特定于单个代码块)的代码的规则。例如,它会处理格式设置注释、缩进语句和引用字符串等操作。
Blockly 提供了 5 个内置代码生成器:
- JavaScript ES5
- Python 3
- Lua 5.1
- Dart 2
- PHP 7
如果此列表中未包含您要为其生成代码的语言,您可以创建自定义语言代码生成器。如需查看一个简单示例,请参阅构建自定义生成器 Codelab,该 Codelab 会创建一个 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 的范围。
如需了解详情,请参阅生成和运行代码。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-14。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-04-14。"],[[["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"]]