Skip to content

About

Ayanami 语言标准库(Ayanami-language 子仓)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

307 Commits

Folders and files

Repository files navigation

Ayanami 标准库

标准库预编译为 .lcl,存放在编译器同目录的 std/ 文件夹中。

使用 import "模块名" 自动搜索并链接。

分模块教程式文档:docs/。

内存模型(v0.5+):默认所有权(非 Copy 值赋值即移动)、[T]/[T; n] 拥有堆数组、 ref / ref mut 借用;不再有 unique / shared / weak / 引用计数。

io — 输入输出

文件:std/io.lcl

import "io";

fn main() -> int {
    println("hello")            // 打印字符串(自动借用)
    println(42.to_string())     // 打印整数
    putchar(65);                // 输出字符(ASCII 码)
    c = getchar();              // 读取一个字符
    return 0
}
函数 说明
getchar() -> int 读取一个字符
putchar(int c) 输出一个字符(ASCII 码)
print(ref String n) 输出字符串(不消费)
print(int n) / print(float f) / print(bool b) / print(char c) 输出常用类型(重载)
println() 输出换行
println(ref String s) 输出字符串并换行
println(int/float/bool/char) 输出常用类型并换行(重载)
println(42)       // 整数
println(3.5)      // 浮点
println(true)     // 布尔
println('x')      // 字符

math — 数学函数

文件:std/math.lcl

函数 说明
abs(int x) 绝对值
min(int a, int b) 最小值
max(int a, int b) 最大值
clamp(int x, int lo, int hi) 限制范围
pow(int base, int exp) 整数幂
abs/min/max/clamp(float, ...) 浮点重载(同名)
pow(float base, int exp) 浮点幂
sqrt(float x) 平方根
floor(float x) / ceil(float x) 向下/向上取整

string — 字符串

文件:std/string.lcl

struct String {
    [char] data
    int len
}
方法 说明
s.len() -> usize 长度
s.index(usize i) -> char 按索引访问字符(也可写 s.data[i])
s.add(ref String other) -> String 拼接(+ 运算符)
s.eq(ref String other) -> bool 相等比较(==)
s.copy() -> String 深拷贝
s.to_string() -> String 消费 self 并返回(ToString 接口)
42.to_string() int/float/char/bool → String
s.is_empty() -> bool 是否空串
s.contains(ref String) -> bool 是否包含子串
s.index_of(ref String) -> int 子串位置(未找到 -1)
s.starts_with(ref String) -> bool / s.ends_with(ref String) -> bool 前缀/后缀
s.substring(usize start, usize end) -> String 区间子串(越界自动夹取)
s.trim() -> String 去首尾空白
s.to_upper() -> String / s.to_lower() -> String 大小写转换
s.repeat(usize n) -> String 重复拼接
s.parse_int() -> int 十进制解析(非法输入返回 0)
s.is_int() -> bool 是否为合法十进制整数

字符(char)工具:

方法 说明
c.is_digit() / c.is_alpha() / c.is_alnum() / c.is_space() 分类判断
c.is_upper() / c.is_lower() 大小写判断
c.to_upper() -> char / c.to_lower() -> char 大小写转换
c.to_digit() -> int 数字字符 → 数值

ToString 接口签名:fn to_string(self) -> String(消费)。

std — 主入口

文件:std/std.lcl,等价于导入 io、string、math,并包含 Error 接口、Result[T, E](unwrap(self) / expect(self, msg))与 Option[T] (unwrap(self) / unwrap_or(self, default) / is_some(self))枚举。

构造函数(显式泛型调用)

命名空间构造函数用 类型::new[T](...) 形式(显式泛型实参;省略时需能从上下文推导 T):

构造 说明
ArrayList::new[T]() 空表
ArrayList::with_capacity[T](n) 预分配容量
LinkedList::new[T]() 空链表
String::empty() / String::new() 空字符串
String::new([char] data, int len) 从拥有所有权的字符缓冲构造
a = ArrayList::new[int]()
b = ArrayList::with_capacity[String](8)
c = LinkedList::new[int]()
s = String::new(['h', 'i'], 2)

list / arraylist / arena / linkedlist — 集合

模块 说明
list List[T] 接口(元素无约束):push(ref mut self, T) / index(ref self, usize) -> T / len(ref self) -> usize / iter(ref self, fn(T))
arraylist ArrayList[T] 顺序表:[T] 缓冲,自动扩容;set(i, v) / pop() / is_empty() / clear()
arena Arena[T] 稳定地址对象池 + 非拥有 Idx:alloc(v) / get(i) / get_mut(i) / len()
linkedlist LinkedList[T]:新所有权模型下改为数组缓冲实现,接口与 List 一致

集合对元素类型无约束,任意类型(含未实现 ToString 的枚举/结构体)都可放入; 仅 to_string 要求元素实现 ToString(在带约束的 impl 块中提供)。

import "arraylist";

fn main() -> int {
    a = ArrayList::new[int]()
    a.push(1)
    a.push(2)
    return a.index(1)     // 2
}

运行时 panic(#panic)

import "panic";

fn main() -> int {
    #panic("boom");        // runtime error: boom --> file.aya:4:5,退出码 101
}
  • 函数宏在调用点展开,自动带 __line/__col/__file
  • 集合越界/空表 pop 会在运行时 panic,并指向你的调用行: thread 'main' panicked at main.aya:6:13: index out of bounds: the len is 1 but the index is 5
  • panic 输出 stderr,程序退出码 101

测试

用例用 #[test] / #[should_panic] 标注 + #assert 断言宏(import "test"); #[should_panic] { ... } 可捕获单个语句块的预期 panic, 详见 docs/dev/testing.md:

AYANAMI_BIN=<主仓>/target/debug/ayanami ./scripts/test.sh

脚本先重建并安装 .lcl,再运行 tests/unit(标注用例)、tests/compile_fail(负例)与 tests/golden(stdout 黄金输出)。已知编译器 bug:泛型枚举 Result 方法与 match(#68/#69)。

开发

本仓库是 Ayanami-language 的 std/ 子模块。 标准库源码在 src/(按领域分目录:core/ collections/ system/ meta/ dev/); 构建产物(*.lcl)不入库。runtime.c 是标准库的 C 运行时(编译器链接程序时编译), 见 docs/dev/runtime.md。符号地图:SYMBOLS.md(rg "关键词" SYMBOLS.md);改完源码跑 ./scripts/gen_symbols.sh 刷新,提交前 ./scripts/gen_symbols.sh --check 需通过。

开发态编译器(cargo run / target/debug/ayanami)读取的是二进制同目录的 std/*.lcl (即主仓 target/debug/std/)。改完 src/ 后用脚本重建并安装:

# 在主仓根目录构建编译器后
AYANAMI_BIN=<主仓>/target/debug/ayanami ./packaging/build.sh --install <主仓>/target/debug/std

之后运行任意 import 标准库的程序即可看到改动;./packaging/build.sh 不带 --install 只生成 src/*.lcl。发布时主仓 ./scripts/package_release.sh 会逐模块构建并组装 install/std/。

hir — HIR transform helper

文件:std/hir.lcl(源码 src/meta/hir.aya)

import "hir"

#[transform]
#[alloc]
#[state]
pub fn bump(String input) -> String {
    doc = parse_hir(ref input).unwrap()
    body = single_body(make_return(make_int(8)))
    result = replace_function_body(ref doc, ref "main", body).unwrap()
    return render_hir(ref result)
}
函数 说明
parse_hir(ref String) -> Option[Json] 解析 HIR JSON
render_hir(ref Json) -> String 序列化 HIR JSON
items(ref Json) -> Option[Json] 程序 items
field(ref Json, ref String) -> Option[Json] 读取对象字段
find_function(ref Json, ref String) -> Option[Json] 查找函数 item
replace_function_body(ref Json, ref String, ArrayList[Json]) -> Option[Json] 替换函数 body
append_item(ref Json, Json) -> Json 追加 item
clone_function(ref Json, ref String, int) -> Option[Json] 克隆函数并改名字/FnId
max_fn_id(ref Json) -> int 当前程序最大 FnId
function_id(ref Json, ref String) -> Option[int] 按函数名查 FnId
replace_call_targets(ref Json, int, int) -> Json 递归重定向 call FnId
make_int(int) -> Json / make_string(ref String) -> Json / make_return(Json) -> Json 构造常用 HIR 节点
single_body(Json) -> ArrayList[Json] 单语句 body
function_signature(ref Json, ref String) -> Option[Json] 函数 params/return_type
struct_fields(ref Json, ref String) -> Option[Json] struct 字段数组

About

Ayanami 语言标准库(Ayanami-language 子仓)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages