~ 斐波那契数列alias fibonacci <Number n> -> { if n <= 1 { return n } let prev = 0 let curr = 1 let index = 2 while index <= n { let next = prev + curr prev = curr curr = next index ++ } return curr}
print fibonacci 10Alia 区别于传统编程语言的根本——这些系统共同构筑了语法可定义、可组合、可翻译的语言内核。
通过 alias 定义任意语法结构,从简单打印到完整控制流,语法的边界由你决定。
~ 定义自定义控制结构alias repeat <Number times> times <Stack stack> -> { for { let i = 0 } i < times { i ++ } { stack.run() }}
~ 使用自定义关键字repeat 3 times { print 'Hello Alia!'}栈是 Alia 的执行核心。新建语境或沿用原语境,精确掌控代码的作用域与生命周期。
~ *{} 新建独立语境let x = 0const stack = *{ let x = 10 x + 5}
const result = stack.run() ~ 15print x ~ 0
~ {} 沿用原语境{ let y = 20}.run()print x + y ~ 能访问 y将语言关键字翻译为任何自然语言或符号系统,让代码使用你的母语编写。
~ 定义中文翻译@translate '中文(简体)' print <Value val> -> 打印 <Value val>
@translate '中文(简体)' let <Key n> = <Value v> -> 设 <Key n> 为 <Value v>
export this
~ 使用中文编程@language '中文(简体)'设 问候 为 '你好,世界!'打印 问候内置 test、assert、inspect、breakpoint 关键字,无需外部框架即可编写和调试测试。
test 'math-operations' { assert 1 + 1 == 2 '加法失败' assert 10 > 5 '比较失败' assert 3 * 4 == 12 '乘法失败'}
test 'inspect-demo' { const a = 42 inspect a '值检查失败'
const arr = [1, 2, 3] inspect arr '数组检查'}
breakpoint ~ 进入交互式 Aliaẞhellref、load、store 提供对变量的间接引用,在封闭语境中灵活交换与修改状态。
let x = 42let r = ref x
print load r ~ 42
store r 99print x ~ 99
~ 交换变量值let a = 'hello'let b = 'world'let ra = ref alet rb = ref blet tmp = load rastore ra load rbstore rb tmpprint a ~ 'world'print b ~ 'hello'通过 @compileEntry、@compileNode、@compileAlias 覆写 AST 节点与关键字,定义任意目标的编译方式。
~ 声明编译入口@compileEntry js { 'use strict;\ncall @compileStart'}
~ 覆写 AST 节点@compileNode "BinaryOp" -> js { '(' .. _bound.left .. ' ' .. _bound.op .. ' ' .. _bound.right .. ')'}
~ 覆写关键字@compileAlias print <Value val> -> js { 'console.log(' .. val .. ')'}在可扩展的语法内核之上,Alia 提供了面向对象、错误处理、类型系统等现代编程范式所需的一切。
class 定义实现,extends 实现继承,new 实例化对象。
class Person *[ name: '', age: 0, greet: alias -> { 'Hi, I am ' .. self.name }, new: alias <String n> <Number a> -> { self.name = n self.age = a }]
const p = new Person('Alice', 30)print p.greet()try-catch-throw 三关键字构造完善的异常捕获与恢复机制。
const divide = alias <Number a> <Number b> -> { if b == 0 { throw '除数不能为零' } a / b}
try { print divide(10, 0)} catch (e) { print '捕获错误: ' .. e}
~ 自定义错误类class MyError * [msg: '', code: 0]throw new MyError('超时', 500)编译时类型注解与 typeOf 运行时类型检查,兼顾安全与性能。
~ 类型注解(编译时)const name: String = 'Alia'const count: Number = 42const items: Array = [1, 2, 3]const flag: Boolean = true
~ 运行时类型检查if typeOf name == 'String' { print 'name 是字符串'}
~ 泛型用法const list: Array[Number] = [0]list.push(count)*[] 定义对象,[] 定义数组,支持灵活的属性访问。
~ 对象定义const car = *[ brand: 'Tesla', model: 'Model 3', features: ['autopilot', 'electric']]
~ 嵌套对象const company = *[ name: 'Alia Corp', depts: *[ eng: 50, sales: 30 ]]
print company.depts.englet 声明变量,const 声明常量。栈创建局部作用域,*{} 隔离新语境。
let x = 10const PI = 3.14159
x = 20 ~ 重新赋值x += 5 ~ 增量赋值x ++ ~ 自增
~ 局部作用域{ let inner = '局部变量' print x ~ 可访问外层}.run()print inner ~ 报错run/await 管理独立线程,Channel 实现线程间通信,select 提供多路信道选择。
~ 启动独立线程const task = run Number { print 'Hello from thread!' 42}
print await task ~ 42
~ 线程间通信const ch = new Channel[3]ch.send 'hello'ch.send 42
print ch.receive ~ 'hello'print ch.receive ~ 42已经支持的编译目标