Repository navigation
Basic Syntax
一份够用的语法地图:从「程序长什么样」到「流、行为、方法、类、约束」都覆盖,配可运行示例。 A usable syntax map: from "what a program looks like" to "flows, behaviors, methods, classes, constraints", with runnable examples.
程序就是 $Program 类的构造过程。入口是 @::(构造行为),没有全局作用域。
A program is the construction of its $Program class. The entry is @:: (construct behavior) — no global scope.
&io; # 导入标准库 / import a stdlib
$Program { # 程序类 / the program class
@::[{ # 构造行为 = 程序入口 / construct behavior = entry point
io::out.push_line("hi");
}];
}
-
&io;导入库并随带成品对象io::out/io::in。 -
[{ … }]是空签名行为[() -> () { … }]的块糖。
语法:- 类型名 变量名。圆括号里必须写「类型名 + 变量名」两项,不能只写变量名。
Syntax: - type name. The parentheses must hold both a type and a name — never the name alone.
-(std::Number a); # 仅声明,自动零值 / declare, auto zero-value
-(std::String s) << "Yahoo!"; # 声明并赋值 / declare and bind
-(std::Number! frozen) << 7; # 常数:类型后加 ! / const: ! after the type
-
零值法则:新对象自带缺省值(
Number→0、String→""、Boolean→false)。 Zero-value law: a fresh object carries a default (Number→0). -
赋值用
<<:a << b是把 b 流进 a,等价于显式调用a.=(b)。中缀a = b是非法的(没有=运算符)。 Assign with<<:a << bflows b into a, equal toa.=(b). Infixa = bis illegal. - 实例化语句本身是表达式,值就是新对象,可嵌进别的表达式(
out << (-(std::String t) << "x"))。
值通过显式的「流」在对象之间移动: Values move between objects through explicit flows:
A >> B; # A 流向 B / A flows to B
B << A; # 与上式完全等价 / exactly equal to the above
-
<<左向流、>>右向流,语义相同。 - 发送方先「公布」(
=:),接收方再「接收」(:=);同类型间a << b就是赋值。 - 向
io::OStream写入(out << v)是输出动作,不修改out自身,所以out可声明为常数。 - 没有标量值的对象(
out << p)会显示其身份<类型 "变量名">(如<Point "p">);若定义了零参@to_string,则显示其返回内容。
可执行逻辑的基本单元,用方括号书写。函数、闭包、方法、控制流分支——底层都是行为。 The basic unit of executable logic, written with brackets. Functions, closures, methods, branches — all are behaviors underneath.
[(输入参数) 行为模式 (输出) { 函数体 }]
三级行为模式箭头(由严到宽): Three behavior-mode arrows (strict → loose):
| 箭头 | 模式 | 能做什么 |
|---|---|---|
=> |
零副作用 | 连外部都不能访问,纯计算 |
~> |
常数 | 可读外部环境,不可改 |
-> |
非常数 | 可读也可改外部环境 |
[(a) => (sum) { sum << a.+(1); }] # 纯计算,绝不碰外界 / pure, touches nothing outside
[(a) ~> (sum) { sum << a.+(1); }] # 只读外界 / read-only
[() -> () { io::out << "x"; }] # 要写外界,用 -> / needs -> to write out
- 行为也是对象:可传递、可延迟执行、可绑定成方法。
- 空输出
()表示「不公布任何值」(取代已退役的void)。
方法用 @方法名 [行为] 声明(注意:行为用方括号 [...] 包住,不再是 <<),三形态:
Declare methods with @name [behavior] (the behavior is wrapped in [...], not <<); three shapes:
@get[() ~> (result) { result << value; }]; # 读 / getter
@inc[() -> () { value << value.+(1); }]; # 写,用 self / setter, uses self
@name[() -> () { … }]; # 普通 / plain
- 实例方法经
self.NAME(...)调用(self指当前对象)。 - 四个保留方法名:
@::(构造)、@~(析构)、@=:(公布)、@:=(接收)。 - 约束检查见第 8 节。
$Counter {
-(std::Number value);
@get[() ~> (result) { result << value; }];
@inc[() -> () { value << value.+(1); }];
}
- 类名以
$开头;实例用-(Counter c)创建。 - 父类可以是类,也可以是实例(从实例派生类)。
- 类内自调用必须经
self。
条件分支与循环是流式控制流对象(std::If / std::While / std::Repeat),不再挂在布尔 / 数字上。用构造函数喂入「条件闭包」或「循环次数」,再用 .then(...) / .else(...) 挂上分支或循环体;每个方法都返回对象自身,可写成一条链。
Control flow is expressed by streaming control-flow objects (std::If / std::While / std::Repeat), not as methods on Booleans/Numbers. Feed a condition closure or a count to the constructor, then attach branches / loop bodies with .then(...) / .else(...); each call returns the object itself so you can chain.
&io;
$Program {
@::[() -> () {
-(io::OStream out);
// std::If:条件闭包返回布尔,then/else 各最多命中其一
std::If([() -> (b) { b << true; }])
.then([() -> () { out << "yes\n"; }])
.else([() -> () { out << "no\n"; }]);
// std::While:每次迭代前求值条件闭包,为真则执行体
-(std::Number i) << 0;
std::While([() -> (c) { c << (i.<(3)); }])
.then([() -> () { out << i; out << " "; i << i.+(1); }]);
out << "\n";
// std::Repeat:按计数执行体 n 次
std::Repeat(5).then([() -> () { out << "x"; }]); # 打印 xxxxx
}];
}
分支 / 循环体都是行为;其返回的值经
.value()取回。条件闭包必须返回布尔。 Branches / loop bodies are behaviors; their published value is retrieved with.value(). The condition closure must return a Boolean.
约束 = 一张「必须有哪些方法」的清单(鸭子类型的强类型盔甲)。用 # 前缀声明:
A constraint is a checklist of "which methods an object must have". Declared with #:
#Addable {
@+[(other) -> (result)]; # 只写签名,以 ; 结束,不能带 {} 函数体 / signature only, ends with ;
}
[(a[Addable]) -> (r) { r << a.+(1); }] # 参数必须满足 Addable / param must satisfy Addable
- 约束方法签名不能带
{}函数体——写@+[(other) -> (result) {}];是语法错误。约束只是一张「方法签名清单」,方法体对它没有意义。 -
#Name [父约束]支持约束继承(#Comparable [Addable] { … })。 - 直接填类名当约束:
[(s[std::String]) -> ()]要求方法签名与std::String一致。 -
@表示「必须是行为」:[(h[@]) -> ()];@后可跟行为签名做精确匹配。 - 约束在运行期核对(按方法签名集合比对,不看参数名)。不满足立即抛
ConstraintException。
元组是语言内建的不可变值序列(std::Tuple),常用来承载多返回值。
A tuple is the language's built-in immutable value sequence; it commonly carries multiple return values.
结构化解耦(接收多返回值):方法返回多个值时,可一次性拆给多个变量,按位置对应。 Structural decomposition (receiving multiple returns): unpack a multi-value return into several variables by position.
$Math {
@divide[(a, b) -> (q, r) { q << a./(b); r << a.%(b); }];
}
-(Math m);
-(std::Number q, std::Number r) << m.divide(10, 3); # 全要 / take both
-(std::Number q, _) << m.divide(10, 3); # 只要第一个 / first only
- 目标少于返回值项:尾部多余项被忽略(
-(q, _) << …等价于忽略后面的项)。 - 目标多于返回值项:报错(
too few values to destructure into 'x')——值不够填。 - Fewer targets than items → trailing items silently ignored. More targets than items → an error.
单元组实参自动展开:多参数方法若只收到一个 std::Tuple 实参,解释器会在调用边界把它按位置展开成多个实参。
A single tuple argument auto-expands: if a multi-parameter method is called with exactly one std::Tuple, it is expanded into several arguments by position.
$Calc {
@sum[(a, b) -> (result) { result << a.+(b); }];
}
-(Calc c);
-(std::Number s) << c.sum((3, 4)); # 等价于 c.sum(3, 4),s = 7
- 元组项少于形参 → 报错;多于形参 → 尾部忽略。与解构的「忽略尾部、多则报错」一致。
- Fewer tuple items than parameters → error; more → trailing items ignored.
对象构造后仍是活的: Objects stay alive after construction:
obj:@m[{ … }]; # 注入新方法 / inject a new method
obj:-(T v) << init; # 加私有属性(仅可初始化,只有自身方法能访问)/ private attr
obj.#(); # 永久冻结 / freeze forever
- 向已有名字注入 = 重复声明错误;重绑常数方法 = 更改常量错误。
- 库预置对象:库的
.synl可在顶层创建常数实例(-(io::OStream! io::out);),随导入到来。
- 故障在源头抛出,以类 g++ 格式报告:
文件:行:列: error: 类型: 消息+^~~~插入符 + 完整执行栈。 - 致命错误退出码为
1,所以synth bad.syn && cmd绝不会执行cmd。 - 算术遵循 IEEE 754:
1/0→Infinity/NaN,不中断。
| 符号 | 含义 |
|---|---|
-(T v) |
实例化对象 / instantiate |
<< >>
|
左/右向流(值流动 / 赋值,不再用于绑定闭包) |
=> ~> ->
|
行为模式:零副作用 / 常数 / 非常数 |
[ … ] |
行为界定 / behavior |
@名[闭包] |
方法声明 / method decl(把行为绑定到名字) |
!(类型后) |
常数变量 / const var |
# |
约束前缀 / constraint |
&模块; |
导入库 / import |
:: |
构造/命名空间 / construct / namespace |
self |
当前对象 / current object |
=: :=
|
公布 / 接收函数 |
👉 标准库怎么用:标准库对象使用 / Using StdLib Objects