Zero语言采用静态类型系统,提供编译期类型检查以提高代码安全性和性能。
int- 64位整数float- 64位浮点数string- 字符串bool- 布尔值null- 空类型
// 变量声明带类型注解
let x: int = 42;
let name: string = "Zero";
let flag: bool = true;
let price: float = 3.14;
// 函数参数类型注解
fn add(a: int, b: int) {
return a + b;
}
fn greet(name: string) {
return "Hello, " + name;
}
// 函数可以省略类型注解
fn print_message(msg) {
print(msg);
}
支持简单的类型推导(局部类型推导):
// 从初始值推导类型
let x = 42; // 推导为 int
let y = 3.14; // 推导为 float
let name = "Zero"; // 推导为 string
let flag = true; // 推导为 bool
// 函数参数可以有类型注解
fn add(a: int, b: int) {
return a + b;
}
- 必须有类型注解或可推导的初始值
- 类型注解和初始值类型必须匹配
- 赋值表达式两侧类型必须匹配
- 不支持隐式类型转换
- 参数数量必须匹配
- 参数类型必须匹配
- 返回值类型必须符合声明
-
算术运算符:
+,-,*,/,%- 操作数必须是
int或float - 结果类型由操作数决定
intopint→intfloatopfloat→floatintopfloat→float(自动提升)
- 操作数必须是
-
比较运算符:
==,!=,<,<=,>,>=- 操作数必须是同类型或可比较类型
- 结果类型为
bool
-
逻辑运算符:
&&,||,!- 操作数必须是
bool - 结果类型为
bool
- 操作数必须是
-
字符串连接:
+- 操作数必须都是
string - 结果类型为
string
- 操作数必须都是
if条件必须是bool类型while条件必须是bool类型
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
Int,
Float,
String,
Bool,
Null,
Void,
Function(FunctionType),
Unknown, // 用于类型推导
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FunctionType {
pub params: Vec<Type>,
pub return_type: Box<Type>,
}位置:src/type_checker/mod.rs
职责:
- 遍历AST进行类型检查
- 维护符号表(变量和函数的类型信息)
- 报告类型错误
- 执行类型推导
AST → Type Checker → Typed AST → Compiler → Bytecode
pub struct SymbolTable {
scopes: Vec<HashMap<String, Type>>,
}- 支持作用域嵌套
- 记录变量和函数的类型信息
- 支持变量查找和类型查询
// 基本类型
let x: int = 42;
let y: float = 3.14;
let name: string = "Zero";
let flag: bool = true;
// 函数定义
fn add(a: int, b: int) {
return a + b;
}
fn multiply(x: float, y: float) {
return x * y;
}
// 函数调用
let sum = add(10, 20);
let product = multiply(2.5, 4.0);
// 类型推导
let auto_int = 100; // int
let auto_float = 2.718; // float
let auto_string = "hello"; // string
// 错误:类型不匹配
let x: int = 3.14; // Error: Cannot assign float to int
// 错误:运算类型不匹配
let result = "hello" + 42; // Error: Cannot add string and int
// 错误:参数类型不匹配
fn add(a: int, b: int) {
return a + b;
}
let result = add(1.5, 2.5); // Error: Expected int, got float
// 错误:条件类型不匹配
if 42 { // Error: Expected bool, got int
print("error");
}
类型检查器将提供清晰的错误信息:
Type Error at line 5:
Expected type 'int', but got 'float'
Type Error: Argument count mismatch
Function expects 2 arguments but got 3
Type Error: Argument type mismatch
Function parameter 'x' expects type 'int', but got 'string'
Type Error at line 15:
Cannot apply operator '+' to types 'string' and 'int'
- 数组类型:
[int],[string] - 元组类型:
(int, string) - 结构体:
struct Point { x: int, y: int }
fn identity<T>(x: T) -> T {
return x;
}
type Age = int;
type Name = string;
fn divide(a: int, b: int) -> Result<int, string> {
if b == 0 {
return Err("Division by zero");
}
return Ok(a / b);
}
trait Printable {
fn to_string() -> string;
}
- 基本类型定义
- 变量类型注解语法
- 函数参数类型注解
- 基本类型检查
- 类型错误报告
- 局部变量类型推导
- 表达式类型推导
- Unknown类型处理(用于无类型注解的函数参数)
- 函数返回类型语法支持
- 返回类型检查
- 更严格的类型推导
- 泛型支持
- 复合类型
- 类型别名
- Option/Result