← 都来码← 文档索引D原文English

D

C 风格的语法,写起来比 C 宽松一些。

在手机上怎么跑

vml run examples/d/test/sysinfo.d

编译要等一会儿(C 那种要一两分钟,脚本类语言几秒)。程序跑起来后屏幕底部就是手柄,方向键 + 四个动作键都在;点右上角返回可以回到命令行。

写法要点

示例

文件演示什么
test/catch.d接方块
test/sysinfo.d设备信息

同目录还有:demo/demo_std.d、test/libs.d、tty/demo_tty.d、ui/demo_ui.d


下面的内容是从 VML 源码里直接带的(third_party/vml/VMLPrepares/DCompiler/): README 讲这个前端支持什么、怎么编;语言规范 讲语法本身。上游一改,这里重新生成就是最新的。

语言规范

D 语言编译器规范说明

版本:v1.0 | 日期:2026-07-06 | 修订者:深圳市探索智能科技有限公司

元数据

属性值
语言D
标准D 2.0 子集
年份2001 年首次发布
完成度~97%
源文件扩展名.d
文件编码UTF-8 (默认)

概述

D 是一门系统编程语言,采用 C-like 语法与模块化设计。本编译器实现的是 D 2.0 规范的功能子集,面向 VML 虚拟机的 MCU 安全子集环境运行。编译流程为:

source.d → [预处理器] → [Lexer] → [Parser] → [CodeGenerator] → VML汇编 (.vml)
编译器入口
调用约定

采用类 C 调用约定:参数从右到左压栈,调用者负责栈清理。R0 寄存器存放返回值(整数/布尔/指针),浮点返回值使用 F0 寄存器。


支持的特性

1. 数据类型
类型关键字VML IR 宽度说明
空类型void—函数无返回值
整数int32-bit signed支持十进制和十六进制 (0x) 字面量
单精度浮点float32-bitIEEE 754 单精度
双精度浮点double64-bit当前降级为 float 存储
布尔bool32-bittrue = 1, false = 0
字符串string指针 (32-bit)不可变字符数组,字面量存入 data section
字符char8-bit单引号字面量,存储为 int
类class引用类型成员通过 R12-0 (this) 访问
结构体struct值类型语法上支持,解析与 class 相同
接口interface占位解析跳过内部声明
枚举enum32-bit支持显式赋值 = expr

字面量语法:

2. 模块系统
module mymodule;                    // 声明当前模块名
import std.stdio;                   // 导入模块
import foo.bar.baz;                 // 点分导入路径

import 语句仅支持静态导入语法,解析时提取模块名用于库链接。module 声明语法上接受但不产生实际语义作用。

3. 变量声明
int x = 10;                         // 声明 + 初始化
float pi;                           // 声明(默认初始化 0)
string name = "hello";              // 字符串变量
bool flag = true;                   // 布尔变量
char c = 'A';                       // 字符变量
ClassName obj;                      // 类类型变量(默认初始化为 0/null)

语义:

4. 运算符
4.1 算术运算符
运算符操作示例
+加法a + b
-減法a - b
*乘法a * b
/除法a / b
%取模a % b
^^幂运算a ^^ b

注意: ^^ 幂运算在 VML 层面不支持原生幂指令,编译器将 ^^ 降级为乘法 * 作为近似实现。

4.2 比较运算符
运算符操作说明
==等于返回 1/0
!=不等于返回 1/0
<小于返回 1/0
>大于返回 1/0
<=小于等于返回 1/0
>=大于等于返回 1/0

比较运算返回整数值:1 为 true,0 为 false。

4.3 逻辑运算符
运算符操作说明
&&逻辑与CMP + JE 短路求值
||逻辑或短路求值
!逻辑非一元取反

逻辑运算使用短路求值策略:&& 在左操作数为假时跳过右操作数;|| 在左操作数为真时跳过右操作数。

4.4 赋值运算符
运算符操作
=直接赋值
+=加法赋值
-=減法赋值
*=乘法赋值
/=除法赋值

复合赋值运算符 (+=, -=, *=, /=) 在内部展开为 a = a op value 形式:

a += b;    // 等价于 a = a + b;
a -= 5;    // 等价于 a = a - 5;
a *= 2;    // 等价于 a = a * 2;
a /= 3;    // 等价于 a = a / 3;
5. 控制流
5.1 if / else 语句
if (condition) {
    // then body
}

if (condition) {
    // then body
} else {
    // else body
}

if (x > 0) {
    return 1;
} else if (x < 0) {
    return -1;
} else {
    return 0;
}

条件求值后与 0 比较,为假时跳转到 else 标签或 end 标签。

5.2 while 语句
while (condition) {
    // loop body
}
5.3 do-while 语句
do {
    // loop body
} while (condition);

实现方式: do-while 在编译时展开为 { body; while (cond) { body } }。

5.4 for 语句 (C 风格)
for (init; condition; increment) {
    // loop body
}

// 示例
for (int i = 0; i < 10; i = i + 1) {
    x = x + i;
}

三子句均为可选项:

5.5 break / continue

break 和 continue 关键字当前未实现。在 Lexer 的关键字表中不存在这两个关键字,因此会被当作普通标识符处理。

5.6 return 语句
return;              // void 返回
return expression;   // 带值返回

返回值存入 R0 寄存器,恢复 R12 后执行 RET。

6. 函数
// 完整声明格式
int add(int a, int b) {
    return a + b;
}

// void 返回类型
void greet(string name) {
    // ...
}

// 无参数
int getAnswer() {
    return 42;
}

语法: returnType functionName(paramType paramName, ...) { body }

内部实现:

7. 类与结构体
7.1 class
class Animal {
    void speak() {
        // member function
    }

    int getAge() {
        return this.age;   // this 关键字号
    }
}
7.2 struct
struct Point {
    int x;
    int y;
}

语法上与 class 相同,共用 ClassDeclNode AST 节点。区别仅在语义层面(值类型 vs 引用类型),当前实现不做区分。

7.3 interface
interface Drawable {
    void draw();
}

interface 声明进行解析,跳过内部声明体,返回占位 AST 节点。接口方法不生成代码。

7.4 enum
enum Color {
    Red,           // 隐式值 0
    Green,         // 隐式值 1
    Blue = 5,      // 显式赋值
    Yellow         // 隐式值 6
}
8. 注释
类型语法说明
单行注释// text从 // 到行尾
块注释/* text */多行,不可嵌套
嵌套块注释/+ text +/D 语言原生嵌套注释,当前未专门支持

编译器内部的 LexerBase.SkipBlockComment 处理 /* ... */,SkipLineComment 处理 // ...。/+ ... +/ 嵌套注释语法当前未在 Lexer 中实现。

注意: 注释 token 在 Tokenize 阶段产生但不参与语法分析 (被 SkipComments() 跳过)。

9. 字符串字面量
"hello world"                       // 普通字符串
"line1\nline2"                      // 含转义字符
"quote: \"inside\""                 // 转义双引号
10. 字符字面量
'a'                                 // 字符 'a'
'\n'                                // 换行符
'\t'                                // 制表符
'\\'                                // 反斜杠
11. 表达式语法
Expression := Assignment
Assignment := LogicalOr ('=' | '+=' | '-=' | '*=' | '/=') Assignment | LogicalOr
LogicalOr  := LogicalAnd ('||' LogicalAnd)*
LogicalAnd := Comparison ('&&' Comparison)*
Comparison := Additive (('==' | '!=' | '<' | '>' | '<=' | '>=') Additive)*
Additive   := Multiplicative (('+' | '-') Multiplicative)*
Multiplicative := Unary (('*' | '/' | '%' | '^^') Unary)*
Unary      := ('-' | '!') Unary | Postfix
Postfix    := Primary ('(' Arguments? ')')*
Primary    := Integer | Float | String | Char | true | false | null
            | this | Identifier | '(' Expression ')'
12. 表达式优先级

按优先级从低到高排列:

优先级运算符结合性
1 (最低)= += -= *= /=右结合
2||左结合
3&&左结合
4== != < > <= >=左结合
5+ -左结合
6* / % ^^左结合
7- (一元) ! (一元)右结合
8 (最高)() (函数调用)左结合
13. 预处理器支持

编译器集成了 C 风格预处理器 (Preprocessor),在词法分析前执行:

预定义宏
宏名值说明
__VML__1VML 目标平台标识
__VML_VERSION__"1.65.32"VML 工具链版本
__D__1D 语言源文件标识
__DATE__"MMM dd yyyy"编译日期
__TIME__"HH:mm:ss"编译时间
预处理指令

支持标准 C 预处理指令,仅当源文件包含 # 字符时才激活预处理器:

14. 入口点与库链接

限制与未实现特性

以下特性属于 D 语言完整规范但目前不支持:

元编程
契约编程
内存管理
控制流
类型系统
复合类型
其他

编译选项

通过 CLI 工具 VMLTool 调用时支持的编译选项:

选项含义
-o <file>输出文件路径
-mode mcuMCU 安全子集模式 (默认)
-mode os完整 OS 模式
--std=<std>指定语言标准版本
-D <macro>定义预处理宏
-U <macro>取消预处理宏定义
-I <path>添加 include 搜索路径
-L <path>添加库搜索路径
-static静态链接
-shared动态链接
-target <arch>指定目标架构

完整语法参考 (EBNF)

Program       := { TopLevelDecl }

TopLevelDecl  := ModuleDecl | ImportDecl | FuncDef | VarDecl
               | ClassDecl | StructDecl | InterfaceDecl | EnumDecl

ModuleDecl    := "module" Identifier ";"
ImportDecl    := "import" Identifier { "." Identifier } ";"

Type          := "void" | "int" | "float" | "double" | "bool"
               | "string" | "char" | Identifier

VarDecl       := Type Identifier [ "=" Expression ] ";"

FuncDef       := Type Identifier "(" [ ParamList ] ")" Block
ParamList     := Type Identifier { "," Type Identifier }

ClassDecl     := "class" Identifier "{" { Declaration } "}" ";"
StructDecl    := "struct" Identifier "{" { Declaration } "}" ";"
InterfaceDecl := "interface" Identifier "{" { Declaration } "}" ";"
EnumDecl      := "enum" Identifier "{" EnumMember { "," EnumMember } [ "," ] "}" ";"
EnumMember    := Identifier [ "=" Expression ]

Block         := "{" { Statement } "}"
Statement     := ReturnStmt | IfStmt | WhileStmt | DoWhileStmt
               | ForStmt | VarDecl | ExpressionStmt

ReturnStmt    := "return" [ Expression ] ";"
IfStmt        := "if" "(" Expression ")" Block [ "else" (IfStmt | Block) ]
WhileStmt     := "while" "(" Expression ")" Block
DoWhileStmt   := "do" Block "while" "(" Expression ")" ";"
ForStmt       := "for" "(" [ForInit] ";" [ Expression ] ";" [ Expression ] ")" Block
ForInit       := VarDecl | Expression
ExpressionStmt:= Expression ";"

Expression    := Assignment
Assignment    := LogicalOr [ ("=" | "+=" | "-=" | "*=" | "/=") Assignment ]
LogicalOr     := LogicalAnd { "||" LogicalAnd }
LogicalAnd    := Comparison { "&&" Comparison }
Comparison    := Additive [ ("==" | "!=" | "<" | ">" | "<=" | ">=") Additive ]
Additive      := Multiplicative { ("+" | "-") Multiplicative }
Multiplicative:= Unary { ("*" | "/" | "%" | "^^") Unary }
Unary         := ["-" | "!"] Postfix
Postfix       := Primary { "(" [ Expression { "," Expression } ] ")" }

Primary       := Integer | FloatLiteral | StringLiteral | CharLiteral
               | "true" | "false" | "null" | "this"
               | Identifier
               | "(" Expression ")"

Identifier    := Letter { Letter | Digit | "_" }
Integer       := Digit { Digit } | "0x" HexDigit { HexDigit }
FloatLiteral  := Digit { Digit } "." Digit { Digit }
StringLiteral := '"' { Character } '"'
CharLiteral   := "'" Character "'"

Comment       := "//" { Character }
BlockComment  := "/*" { Character } "*/"

🆕 字符串编码 (v1.65.19)

该语言编译器通过共享库 (Lib/shared/) 间接使用 VML 字符串体系。

伪指令宽度编码C 类型
.string8-bitUTF-8char*
.wstring16-bitUTF-16LEwchar_t*
.ustring32-bitUTF-32LEchar32_t*

MCU 模式 (默认): 字符串输出为 UTF-8 (.string) OS 模式: 可通过 VML_WSTRING 宏判断编码

共享库已提供宽字符串转换函数 (wchar.h/uchar.h),各语言编译器可按需使用。

编译器 README

D 编译器

路径: VMLPrepares/DCompiler/ 完成度: ~97% | 🟢 生产可用 标准库: Lib/d/

功能

编译模式

MCU 模式(默认 --mode mcu)

MCU 模式针对单片机/裸机环境优化。

跳过(MCU 不支持):

保留:

OS 模式(--mode os)

OS 模式支持全部语言特性。

RAM 级别

使用

dotnet run --project VMLTool -- input.d -o output.vml

测试