经过1个小时的奋斗,修了3个bug,终于使得Kernel FP能运行的代码渐渐多了起来。现在可以看看纯函数式语言简洁的代码及运行结果啦!
下面是很多用于测试的main函数:
1 module startup
2 import list
3
4 def main0 = length "vczh"
5 def main1 = head "vczh"
6 def main2 = tail "vczh"
7 def main3 = concat "genius" " vczh!"
8 def main4 = isempty ""
9 def main5 = isempty "vczh"
10 def main6 = transform (iadd 1) (list 1 (list 2 (list 3 empty)))
11 def main7 = reverse "abcde"
12 def main8 = intersperse '|' "vczh"
13 def main9 = flatten (list "vczh" (list " library" (list " ++" empty)))
14 def main10 = pairlist "genius" "vczh"
15 def main11 = fold 0 iadd (list 1 (list 2 (list 3 empty)))
16 def main12 = all (iequ 0) (list 0 (list 1 (list 2 empty)))
17 def main13 = any (iequ 0) (list 0 (list 1 (list 2 empty)))
18 def main14 = take 5 (iterate (iadd 1) 0)
19 def main15 = take 5 (repeat 99)
20 def main16 = take 5 (cycle (list 1 (list 2 empty)))
21 def main17 = drop 5 "genius vczh!"
通过类型推导、模板实例化和运行时assembly的生成之后,就可以得到运行结果了:
1 CTOR : system.empty=0,0
2 CTOR : system.false=1,0
3 CTOR : system.list=2,2
4 CTOR : system.true=3,0
5 CTOR : sysutils.pair=4,2
6 FUNC : startup.main0, 8
7 FUNC : startup.main1, 7
8 FUNC : startup.main10, 6
9 FUNC : startup.main11, 16
10 FUNC : startup.main12, 17
11 FUNC : startup.main13, 18
12 FUNC : startup.main14, 19
13 FUNC : startup.main15, 27
14 FUNC : startup.main16, 26
15 FUNC : startup.main17, 24
16 FUNC : startup.main2, 36
17 FUNC : startup.main3, 29
18 FUNC : startup.main4, 39
19 FUNC : startup.main5, 38
20 FUNC : startup.main6, 14
21 FUNC : startup.main7, 41
22 FUNC : startup.main8, 45
23 FUNC : startup.main9, 10
24 FUNC : sysutils.and, 5
25 FUNC : sysutils.ineg, 1
26 FUNC : sysutils.not, 3
27 FUNC : sysutils.or, 2
28 FUNC : sysutils.xor, 0
29 EXTR : kernelfp::iadd=1
30 EXTR : kernelfp::iequ=2
31 EXTR : kernelfp::isub=0
32 main0返回值:4
33 main1返回值:v
34 main2返回值:[c , z , h]
35 main3返回值:[g , e , n , i , u , s , , v , c , z , h , !]
36 main4返回值:system.true
37 main5返回值:system.false
38 main6返回值:[2 , 3 , 4]
39 main7返回值:[e , d , c , b , a]
40 main8返回值:[v , | , c , | , z , | , h]
41 main9返回值:[v , c , z , h , , l , i , b , r , a , r , y , , + , +]
42 main10返回值:[(sysutils.pair g v) , (sysutils.pair e c) , (sysutils.pair n z) ,
43 (sysutils.pair i h)]
44 main11返回值:6
45 main12返回值:system.false
46 main13返回值:system.true
47 main14返回值:[0 , 1 , 2 , 3 , 4]
48 main15返回值:[99 , 99 , 99 , 99 , 99]
49 main16返回值:[1 , 2 , 1 , 2 , 1]
50 main17返回值:[s , , v , c , z , h , !]
51
调用这些函数的C++代码如下:
1 #include "..\..\..\..\VL++\Library\Platform\VL_Console.h"
2 #include "..\..\..\..\VL++\Library\Script\KernelFP\VL_KFPScript.h"
3 #include "..\..\..\..\VL++\Library\Data\VL_Stream.h"
4 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
5 #include "..\..\..\..\VL++\Library\Data\VL_Uniop.h"
6
7 using namespace vl;
8 using namespace vl::platform;
9 using namespace vl::kernalfp;
10 using namespace vl::stream;
11 using namespace vl::system;
12 using namespace vl::uniop;
13
14 VUnicodeString ToString(VL_KfpError::List& Errors)
15 {
16 VUnicodeString Result=L"";
17 for(VInt i=0;i<Errors.GetCount();i++)
18 {
19 Result+=L"错误["+VUnicodeString(i+1)+L"]\t模块:"+Errors[i]->Module+L"\t行号:"+VUnicodeString(Errors[i]->Token.LineInFile+1)+L"\r\n";
20 Result+=L"信息:"+Errors[i]->Message+L"\r\n";
21 }
22 return Result;
23 }
24
25 void RunProgram(VL_KfpMachine::Ptr Machine)
26 {
27 MyPlugin Plugin;
28 Machine->AddPlugin(&Plugin,false);
29
30 VInt Index=0;
31 while(true)
32 {
33 VInt ID=Machine->GetFunctionFirstIdByName(L"startup.main"+VUnicodeString(Index));
34 if(ID==-1)
35 {
36 break;
37 }
38 else
39 {
40 GetConsole()->Write(L"main"+VUnicodeString(Index)+L"返回值:");
41 VL_KfpValue MainFunction=Machine->CreateFunction(ID);
42 GetConsole()->Write(ValueToString(MainFunction)+L"\r\n");
43 }
44 Index++;
45 }
46 }
47
48 void vlmain()
49 {
50 GetConsole()->SetTitle(L"Vczh Kernal FP");
51 GetConsole()->SetPauseOnExit(true);
52 GetConsole()->SetTestMemoryLeaks(true);
53
54 VUnicodeString TestDataPath=VFileName(GetConsole()->GetAppPath()).MakeAbsolute(L"..\\TestData\\").GetStrW();
55 VUnicodeString TestOutput;
56 VL_UniStrings CodeFiles;
57 {
58 VL_FileStream Stream(TestDataPath+L"Project.txt",VL_FileStream::vfomRead);
59 VUnicodeString Project=ReadText(&Stream);
60 CodeFiles.SetText(Project);
61 }
62
63 VL_KfpSymbol Symbol;
64 VBool ErrorOccurred=false;
65
66 for(VInt i=0;i<CodeFiles.GetCount();i++)
67 {
68 VL_FileStream Stream(TestDataPath+CodeFiles[i],VL_FileStream::vfomRead);
69 VUnicodeString TestCode=ReadText(&Stream);
70
71 VL_KfpError::List Errors;
72 Symbol.AddUnit(TestCode,Errors);
73 if(Errors.GetCount())
74 {
75 TestOutput+=L"文件\""+VUnicodeString(CodeFiles[i])+L"\"含有语法错误:\r\n";
76 TestOutput+=ToString(Errors);
77 ErrorOccurred=true;
78 }
79 }
80 if(!ErrorOccurred)
81 {
82 VL_KfpError::List Errors;
83 Symbol.PreCompile(Errors);
84 if(Errors.GetCount())
85 {
86 TestOutput+=L"生成符号表时发生错误\r\n";
87 TestOutput+=ToString(Errors);
88 ErrorOccurred=true;
89 }
90 }
91 if(!ErrorOccurred)
92 {
93 VL_KfpMachine::Ptr Machine=Symbol.CreateMachine();
94 TestOutput=Symbol.GetReport();
95 for(VInt i=0;i<Machine->GetCtorCount();i++)
96 {
97 VUnicodeString Name=Machine->GetCtorNameByIndex(i);
98 VInt CtorID=Machine->GetCtorIDByIndex(i);
99 VInt ParamCount=Machine->GetCtorParameterCountByID(CtorID);
100 GetConsole()->Write(L"CTOR : "+Name+L"="+VUnicodeString(CtorID)+L","+VUnicodeString(ParamCount)+L"\r\n");
101 }
102 for(VInt i=0;i<Machine->GetFunctionNameCount();i++)
103 {
104 GetConsole()->Write(L"FUNC : "+Machine->GetFunctionNameByIndex(i));
105 VL_List<VInt , true> IDs;
106 Machine->GetFunctionIDsByIndex(i,IDs);
107 for(VInt j=0;j<IDs.GetCount();j++)
108 {
109 GetConsole()->Write(L", "+VUnicodeString(IDs[j]));
110 }
111 GetConsole()->Write(L"\r\n");
112 }
113 for(VInt i=0;i<Machine->GetExternalCount();i++)
114 {
115 VUnicodeString Name=Machine->GetExternalNameByIndex(i);
116 VInt ExternalID=Machine->GetExternalIDByIndex(i);
117 GetConsole()->Write(L"EXTR : "+Name+L"="+VUnicodeString(ExternalID)+L"\r\n");
118 }
119 RunProgram(Machine);
120 }
121 else
122 {
123 GetConsole()->Write(TestOutput);
124 }
125 {
126 VL_FileStream Stream(TestDataPath+L"Output.txt",VL_FileStream::vfomWrite);
127 WriteText(&Stream,vceUtf16,true,TestOutput);
128 }
129 {
130 VL_FileStream Stream(TestDataPath+L"Debug.txt",VL_FileStream::vfomWrite);
131 WriteText(&Stream,vceUtf16,true,Symbol.GetDebugInformation());
132 }
133 }
posted on 2008-12-12 10:03
陈梓瀚(vczh) 阅读(1443)
评论(1) 编辑 收藏 引用 所属分类:
脚本技术