- 新增 Main.snow 文件,定义了 Address 和 Person 两个结构体- 在 Person 结构体中使用 Address 类型的字段,实现类型嵌套 - 添加 OS.snow 文件,提供基本的打印功能- 此示例展示了如何在 SNOBOL 中使用自定义类型进行字段嵌套
46 lines
1.2 KiB
Plaintext
46 lines
1.2 KiB
Plaintext
module: Main
|
||
|
||
// 一个很简单的结构体,只有内建类型字段
|
||
struct: Address
|
||
fields:
|
||
declare country: string
|
||
declare city: string
|
||
// 构造函数(可要可不要,不影响触发点)
|
||
init:
|
||
params:
|
||
country: string
|
||
city: string
|
||
body:
|
||
this.country = country
|
||
this.city = city
|
||
end body
|
||
end init
|
||
end struct
|
||
|
||
// 在这里做“嵌套字段”:Person.addr 的类型是 Address
|
||
struct: Person
|
||
fields:
|
||
declare name: string
|
||
declare addr: Address // ← 触发点:自定义类型出现在字段类型位置
|
||
// 可选构造(同样不影响触发点)
|
||
init:
|
||
params:
|
||
name: string
|
||
addr: Address
|
||
body:
|
||
this.name = name
|
||
this.addr = addr
|
||
end body
|
||
end init
|
||
end struct
|
||
|
||
// 主函数可以是空体,避免其它解析干扰
|
||
function: main
|
||
returns: void
|
||
body:
|
||
// 什么都不做;关键在于上面的“嵌套字段”声明
|
||
end body
|
||
end function
|
||
|
||
end module
|