今天写了Kernel FP模板函数实例化的代码。虽然还有一点小bug,不过还是说一下。
Kernel FP的Runtime Assembly是没有模板信息的,函数由签名、表达式引用以及函数引用构成。函数引用表记录了函数在一种类型的实例化下实际调用的其他函数的实例。譬如函数参数在类型A和类型B下函数def function param=print param,print有可能因为param的类型的变化而变化。因此这需要记录在一个函数的实例里。
在写完代码之后我粗略跑了一下,使用之前用来做类型推导的代码,以及写了一个main函数:
1 module main
2 import system
3 import sysutils
4 import list
5
6 def main score = if (flt score 60.0) "fail" "pass"
字符串被解释成字符列表,于是"fail"被翻译成list 'f'(list 'a'(list 'i'(list 'l'))),但是操作符作为一种函数的语法糖尚未引入,非完整模板函数也尚未引入,所以操作符就没有实现了。此处flt是小于的意思,main函数的作用就是判断score是否过了60并打出fail和pass作为结果。
编译器通过类型推导,得到函数的类型float->list char:
1 【Code Unit system】
2 module system::system
3 type bool
4 type char
5 type float
6 type int
7 type list T
8 type string = (system.list system.char)
9 type void
10 ctor empty :: type list T
11 ctor false :: type bool
12 ctor list :: <T> -> (system.list <T>) -> type list T
13 ctor true :: type bool
14 func ceil :: (system.float -> system.float) alias "kernelfp::ceil" codefrom -1
15 func chr :: (system.int -> system.char) alias "kernelfp::chr" codefrom -1
16 func fadd :: (system.float -> (system.float -> system.float)) alias "kernelfp::fadd" codefrom -1
17 func fdiv :: (system.float -> (system.float -> system.float)) alias "kernelfp::fdiv" codefrom -1
18 func fequ :: (system.float -> (system.float -> system.bool)) alias "kernelfp::fequ" codefrom -1
19 func fgt :: (system.float -> (system.float -> system.bool)) alias "kernelfp::fgt" codefrom -1
20 func floor :: (system.float -> system.float) alias "kernelfp::floor" codefrom -1
21 func flt :: (system.float -> (system.float -> system.bool)) alias "kernelfp::flt" codefrom -1
22 func fmul :: (system.float -> (system.float -> system.float)) alias "kernelfp::fmul" codefrom -1
23 func fsub :: (system.float -> (system.float -> system.float)) alias "kernelfp::fsub" codefrom -1
24 func iadd :: (system.int -> (system.int -> system.int)) alias "kernelfp::iadd" codefrom -1
25 func idiv :: (system.int -> (system.int -> system.int)) alias "kernelfp::idiv" codefrom -1
26 func iequ :: (system.int -> (system.int -> system.bool)) alias "kernelfp::iequ" codefrom -1
27 func igt :: (system.int -> (system.int -> system.bool)) alias "kernelfp::igt" codefrom -1
28 func ilt :: (system.int -> (system.int -> system.bool)) alias "kernelfp::ilt" codefrom -1
29 func imod :: (system.int -> (system.int -> system.int)) alias "kernelfp::imod" codefrom -1
30 func imul :: (system.int -> (system.int -> system.int)) alias "kernelfp::imul" codefrom -1
31 func isub :: (system.int -> (system.int -> system.int)) alias "kernelfp::isub" codefrom -1
32 func ord :: (system.char -> system.int) alias "kernelfp::ord" codefrom -1
33 func tofloat :: (system.int -> system.float) alias "kernelfp::tofloat" codefrom -1
34 func trunc :: (system.float -> system.int) alias "kernelfp::trunc" codefrom -1
35
36 【Code Unit sysutils】
37 module sysutils::sysutils
38 import system
39 type pair T1 T2
40 ctor pair :: <T1> -> <T2> -> type pair T1 T2
41 func and :: (system.bool -> (system.bool -> system.bool)) codefrom 4
42 func if T1 :: (system.bool -> (<T1> -> (<T1> -> <T1>))) codefrom 9
43 func ineg :: (system.int -> system.int) codefrom 11
44 func not :: (system.bool -> system.bool) codefrom 2
45 func or :: (system.bool -> (system.bool -> system.bool)) codefrom 6
46 func pairop T1 T2 T3 :: ((<T1> -> (<T2> -> <T3>)) -> ((sysutils.pair <T1> <T2>) -> <T3>)) codefrom 12
47 func xor :: (system.bool -> (system.bool -> system.bool)) codefrom 8
48
49 【Code Unit main】
50 module main::main
51 import list
52 import system
53 import sysutils
54 func main :: (system.float -> (system.list system.char)) codefrom 0
这个时候,只有模板函数if被实例化,所以我们可以在下面看到所有被实例化的函数(非模板函数一律实例化):
1 【Assembly Unit system】
2
3 [Information]
4 Name : system
5 Function Count : 1
6 Slot Count : 1
7
8 [Function]
9 Function Name : flt
10 Unique Name : system::flt::$kernelfp::flt
11 Owner Unit : system
12 Level : 0
13 External Command : kernelfp::flt
14 Instance : system::flt::$kernelfp::flt::(system.float -> (system.float -> system.bool))
15
16 [Slot Item]
17 Unique Name : system::flt::$kernelfp::flt::(system.float -> (system.float -> system.bool))
18 Parent Slot : <NULL>
19 Owner Function : system::flt::$kernelfp::flt
20
21 【Assembly Unit sysutils】
22
23 [Information]
24 Name : sysutils
25 Function Count : 7
26 Slot Count : 7
27
28 [Function]
29 Function Name : result
30 Unique Name : sysutils.5.1::result::#0
31 Owner Unit : sysutils
32 Level : 2
33 External Command :
34 Instance : <NULL>
35
36 [Function]
37 Function Name : and
38 Unique Name : sysutils::and::#4
39 Owner Unit : sysutils
40 Level : 0
41 External Command :
42 Instance : sysutils::and::#4::(system.bool -> (system.bool -> system.bool))
43
44 [Function]
45 Function Name : if
46 Unique Name : sysutils::if::#9
47 Owner Unit : sysutils
48 Level : 0
49 External Command :
50 Instance : <NULL>
51
52 [Function]
53 Function Name : ineg
54 Unique Name : sysutils::ineg::#11
55 Owner Unit : sysutils
56 Level : 0
57 External Command :
58 Instance : sysutils::ineg::#11::(system.int -> system.int)
59
60 [Function]
61 Function Name : not
62 Unique Name : sysutils::not::#2
63 Owner Unit : sysutils
64 Level : 0
65 External Command :
66 Instance : sysutils::not::#2::(system.bool -> system.bool)
67
68 [Function]
69 Function Name : or
70 Unique Name : sysutils::or::#6
71 Owner Unit : sysutils
72 Level : 0
73 External Command :
74 Instance : sysutils::or::#6::(system.bool -> (system.bool -> system.bool))
75
76 [Function]
77 Function Name : xor
78 Unique Name : sysutils::xor::#8
79 Owner Unit : sysutils
80 Level : 0
81 External Command :
82 Instance : sysutils::xor::#8::(system.bool -> (system.bool -> system.bool))
83
84 [Slot Item]
85 Unique Name : sysutils::and::#4::(system.bool -> (system.bool -> system.bool))
86 Parent Slot : <NULL>
87 Owner Function : sysutils::and::#4
88
89 [Slot Item]
90 Unique Name : sysutils::if::#9::(system.bool -> ((system.list system.char) -> ((system.list system.char) -> (system.list system.char))))
91 Parent Slot : <NULL>
92 Owner Function : sysutils::if::#9
93 0 = sysutils::if::#9::(system.bool -> ((system.list system.char) -> ((system.list system.char) -> (system.list system.char))))/sysutils.5.1::result::#0::(system.list system.char)
94
95 [Slot Item]
96 Unique Name : sysutils::if::#9::(system.bool -> ((system.list system.char) -> ((system.list system.char) -> (system.list system.char))))/sysutils.5.1::result::#0::(system.list system.char)
97 Parent Slot : sysutils::if::#9::(system.bool -> ((system.list system.char) -> ((system.list system.char) -> (system.list system.char))))
98 Owner Function : sysutils.5.1::result::#0
99
100 [Slot Item]
101 Unique Name : sysutils::ineg::#11::(system.int -> system.int)
102 Parent Slot : <NULL>
103 Owner Function : sysutils::ineg::#11
104
105 [Slot Item]
106 Unique Name : sysutils::not::#2::(system.bool -> system.bool)
107 Parent Slot : <NULL>
108 Owner Function : sysutils::not::#2
109
110 [Slot Item]
111 Unique Name : sysutils::or::#6::(system.bool -> (system.bool -> system.bool))
112 Parent Slot : <NULL>
113 Owner Function : sysutils::or::#6
114
115 [Slot Item]
116 Unique Name : sysutils::xor::#8::(system.bool -> (system.bool -> system.bool))
117 Parent Slot : <NULL>
118 Owner Function : sysutils::xor::#8
119
120 【Assembly Unit list】
121
122 [Information]
123 Name : list
124 Function Count : 0
125 Slot Count : 0
126
127 【Assembly Unit main】
128
129 [Information]
130 Name : main
131 Function Count : 1
132 Slot Count : 1
133
134 [Function]
135 Function Name : main
136 Unique Name : main::main::#0
137 Owner Unit : main
138 Level : 0
139 External Command :
140 Instance : main::main::#0::(system.float -> (system.list system.char))
141
142 [Slot Item]
143 Unique Name : main::main::#0::(system.float -> (system.list system.char))
144 Parent Slot : <NULL>
145 Owner Function : main::main::#0
146 0 = sysutils::if::#9::(system.bool -> ((system.list system.char) -> ((system.list system.char) -> (system.list system.char))))
147 1 = system::flt::$kernelfp::flt::(system.float -> (system.float -> system.bool))
明天要修bug了。
posted on 2008-11-01 07:05
陈梓瀚(vczh) 阅读(1387)
评论(0) 编辑 收藏 引用 所属分类:
脚本技术