Skip to content

Latest commit

 

History

History
184 lines (136 loc) · 9.69 KB

File metadata and controls

184 lines (136 loc) · 9.69 KB

13. Standard Library

solid-lang's standard library is organized in four layers with clear dependency relationships.

13.1 Library Layers

┌─────────────────────────────────────────┐
│                   std                    │  ← Standard Library
│  (collection, string, io, net, etc.)    │
├─────────────────────────────────────────┤
│                   sys                    │  ← Platform Abstraction
│     (win32, posix, linux, osx)          │
├─────────────────────────────────────────┤
│                  core                    │  ← Core Library
│  (types, math, memory, ops, etc.)       │
├─────────────────────────────────────────┤
│               predeclare                 │  ← Predeclared Types
│  (compiler intrinsic types)             │
└─────────────────────────────────────────┘

13.2 Predeclared Types (predeclare)

The predeclare layer is automatically available without explicit using. It contains compiler intrinsic types and fundamental types required for language syntax.

Intrinsic Primitive Types:

@intrinsic struct i8;
@intrinsic struct i16;
@intrinsic struct i32;
@intrinsic struct i64;
@intrinsic struct isize;

@intrinsic struct u8;
@intrinsic struct u16;
@intrinsic struct u32;
@intrinsic struct u64;
@intrinsic struct usize;

@intrinsic struct f32;
@intrinsic struct f64;

@intrinsic struct bool;

The @intrinsic declarations serve as documentation facades — they show that primitive types are structs with methods just like user-defined types, reflecting orthogonality. The compiler implements these types internally rather than linking against user-space definitions.

Fundamental Types:

Type Description
Rune Unicode code point (character literal type)
String String type (string literal type)
Slice<T> Read-only slice type
MutableSlice<T> Read-write slice type
RawPtr Raw pointer wrapper (for FFI interoperability)

Note: The predeclare layer does not require explicit using import.

13.3 Core Library (core)

The core library has no platform dependencies and provides fundamental types and operations.

Module Description
core::math Mathematical functions, constants
core::mem Memory operations, allocation traits
core::ops Operator interfaces
core::cmp Comparison interfaces
core::iter Iterator interfaces
core::option Optional type
core::result Result type for error handling

13.4 System Library (sys)

Platform abstraction layer with OS-specific functionality.

Module Description
sys::mem Virtual memory, page allocation
sys::fs File system operations
sys::thread Threading primitives
sys::sync Synchronization primitives
sys::time Time and timers
sys::simd SIMD intrinsics

Platform-specific modules:

Module Platform
sys::win32 Windows API bindings
sys::posix POSIX API bindings
sys::linux Linux-specific APIs
sys::osx macOS-specific APIs

13.5 Standard Library (std)

High-level abstractions built on core and sys.

Module Description
std::collection Vec, HashMap, HashSet, etc.
std::string String types and operations
std::io I/O streams, files, console
std::fmt Formatting and printing
std::net Networking (TCP, UDP)
std::path Path manipulation
std::process Process management
std::env Environment variables

13.6 Naming Conventions

Category Convention Example
Types PascalCase Vec, HashMap, Option
Functions snake_case push_back, from_utf8
Variables snake_case count, buffer_size
Constants SCREAMING_SNAKE_CASE MAX_SIZE, PI
Namespaces snake_case core::math
Interfaces IPascalCase IAdd, IIterator
Generic Types TPascalCase TValue, TError

Note on Generic Type Parameters: Using a bare T (such as T, TKey, TValue) as a generic type parameter name is legal and idiomatic. Similar to the I prefix convention for interfaces, the T prefix allows generic parameters to be immediately recognized at a glance. The TPascalCase form is a recommended convention, not a grammar requirement.

第一层:没有就无法写基本程序的(必选)

这些是语言能跑“Hello World”之外的任何逻辑程序的前提。

类别 最小内容 为什么必须
控制台 I/O print / printlninput / read_line 输出结果、读取用户输入,是程序与外界交互的最基本通道
字符串基本操作 拼接、长度、取字符/子串、比较、大小写转换、查找/替换 几乎所有程序都在处理文本
数值运算 算术、取整/取余、绝对值、最大/最小、随机数 基础计算;没有随机数很多演示程序都写不了
类型转换 字符串↔数字、数字之间互转、布尔转换 处理输入输出时必需
基本数据结构 数组/列表(动态长)、字典/映射(键值对)、集合(可选但推荐) 没有集合类型只能用变量,连斐波那契数列都麻烦
迭代与范围 生成数字范围的方法(如 range)、对集合的迭代器 循环方便性的基础
程序出口 正常/异常退出程序的方法(如 exit 控制程序终止状态

⚠️ 即使如此极简,也至少要能写一个“读取两个数字,输出它们的和”的程序,否则开发体验灾难。


第二层:让语言能做实际任务的(强烈推荐)

有了这些,就能写文件操作、系统交互和简单算法了。

类别 内容
文件读写 打开/关闭文件、读取全部文本、按行读取、写入文本、判断文件是否存在
目录操作 列出目录文件、创建/删除目录、路径拼接
命令行参数 获取程序启动参数
环境变量 读取/设置环境变量
错误处理基元 如果语言有异常机制,则提供基础异常类(Error, TypeError, ValueError 等)
数学函数 三角函数、对数、平方根、常量 π 和 e
日期时间 获取当前时间、格式化、时间差计算
正则表达式 字符串匹配/替换(可先简单实现)
JSON 解析/生成 现代程序必备的数据交换格式
排序与查找 对集合的排序、二分查找等

如果语言定位为教学或脚本语言,这一层已经能覆盖 80% 的常见任务。


第三层:让语言“自举”和接入生态的(按需)

类别 说明
网络基础 HTTP GET/POST、简单 TCP socket
进程/子进程 执行外部命令、获取输出
加密与哈希 MD5/SHA 哈希、Base64 编解码
序列化 除 JSON 外的二进制序列化
单元测试框架 标准化的 assert 和测试运行器
C/FFI 接口 调用外部动态库的函数,使得生态可以借用其他语言
反射/内省 类型查询、成员访问等,为高阶工具提供基础