test: 添加自定义类型字段嵌套示例
- 新增 Main.snow 文件,定义了 Address 和 Person 两个结构体- 在 Person 结构体中使用 Address 类型的字段,实现类型嵌套 - 添加 OS.snow 文件,提供基本的打印功能- 此示例展示了如何在 SNOBOL 中使用自定义类型进行字段嵌套
This commit is contained in:
parent
cbd8b24c4c
commit
4b90f15371
45
playground/Demo/Demo29/Main.snow
Normal file
45
playground/Demo/Demo29/Main.snow
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
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
|
||||||
18
playground/Demo/Demo29/OS.snow
Normal file
18
playground/Demo/Demo29/OS.snow
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module: os
|
||||||
|
function: print
|
||||||
|
params:
|
||||||
|
declare i1: int
|
||||||
|
returns: void
|
||||||
|
body:
|
||||||
|
syscall("PRINT", i1)
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
function: println
|
||||||
|
params:
|
||||||
|
declare i1: int
|
||||||
|
returns: void
|
||||||
|
body:
|
||||||
|
syscall("PRINTLN", i1)
|
||||||
|
end body
|
||||||
|
end function
|
||||||
|
end module
|
||||||
Loading…
x
Reference in New Issue
Block a user