source: dev/trunk/abdev/BasicCompiler_Common/src/Delegate.cpp@ 343

Last change on this file since 343 was 339, checked in by dai_9181, 17 years ago

64ビットコンパイラもデリゲートに対応させた

File size: 5.3 KB
Line 
1#include "stdafx.h"
2
3#include <Delegate.h>
4
5Delegate::Delegate( const NamespaceScopes &namespaceScopes, const std::string &name, Procedure::Kind procKind, const char *paramStr, const Type &returnType, int nowLine )
6 : Procedure( namespaceScopes, name, procKind, false )
7{
8 this->returnType = returnType;
9 params.Analyze( paramStr, nowLine );
10
11 dynamicParams = params;
12 dynamicParams.insert( dynamicParams.begin(), new Parameter( "_System_LocalThis", Type( DEF_PTR_VOID ) ) );
13}
14
15void Delegates::Collect( const BasicSource &source )
16{
17 int i2;
18 char temporary[VN_SIZE];
19
20 // 名前空間管理
21 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
22 namespaceScopes.clear();
23
24 // Importsされた名前空間の管理
25 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
26 importedNamespaces.clear();
27
28 for( int i=0; i<source.GetLength(); i++ )
29 {
30 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
31 for(i+=2,i2=0;;i2++,i++){
32 if( IsCommandDelimitation( source[i] ) ){
33 temporary[i2]=0;
34 break;
35 }
36 temporary[i2]=source[i];
37 }
38 namespaceScopes.push_back( temporary );
39
40 continue;
41 }
42 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
43 if( namespaceScopes.size() <= 0 ){
44 SetError(12, "End Namespace", i );
45 }
46 else{
47 namespaceScopes.pop_back();
48 }
49
50 i += 2;
51 continue;
52 }
53 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
54 for(i+=2,i2=0;;i2++,i++){
55 if( IsCommandDelimitation( source[i] ) ){
56 temporary[i2]=0;
57 break;
58 }
59 temporary[i2]=source[i];
60 }
61 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
62 {
63 SetError(64,temporary,i );
64 }
65
66 continue;
67 }
68 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
69 importedNamespaces.clear();
70 continue;
71 }
72
73 else if( source[i] == 1 && source[i+1] == ESC_DELEGATE )
74 {
75 int nowLine = i;
76
77 i += 2;
78 if( !( source[i] == 1 && ( source[i+1] == ESC_SUB || source[i+1] == ESC_FUNCTION ) ) )
79 {
80 SetError(1,NULL,i);
81 continue;
82 }
83
84 Procedure::Kind procKind = Procedure::Sub;
85 if( source[i+1] == ESC_FUNCTION )
86 {
87 procKind = Procedure::Function;
88 }
89 i += 2;
90
91 // 名前
92 char name[VN_SIZE];
93 GetIdentifierToken( name, source.GetBuffer(), i );
94
95 char paramStr[8192];
96 i += GetStringInPare( paramStr, source.GetBuffer() + i );
97
98 // 戻り値
99 Type returnType;
100 if( source[i] == 1 && source[i+1] == ESC_AS )
101 {
102 i += 2;
103 char typeName[VN_SIZE];
104 GetCommandToken( typeName, source.GetBuffer(), i );
105 compiler.StringToType( typeName, returnType );
106 }
107
108 this->Put( new Delegate( namespaceScopes, name, procKind, paramStr, returnType, nowLine ) );
109 }
110 }
111}
112
113void Delegates::GenerateSourceCode( std::string &destSource )
114{
115 destSource = "";
116
117 SourceTemplate sourceTemplate( "\\SubOperation\\templates\\delegate_class.tab" );
118
119 this->Iterator_Reset();
120 while( this->Iterator_HasNext() )
121 {
122 const Delegate &dg = *this->Iterator_GetNext();
123
124 std::map<std::string,std::string> values;
125
126 if( dg.GetNamespaceScopes().size() )
127 {
128 std::string namespaceScopesCommandStr = "";
129 std::string endNamespaceScopesCommandStr = "";
130 BOOST_FOREACH( const std::string &namespaceStr, dg.GetNamespaceScopes() )
131 {
132 if( namespaceScopesCommandStr.size() )
133 {
134 namespaceScopesCommandStr += ":";
135 endNamespaceScopesCommandStr += ":";
136 }
137 namespaceScopesCommandStr += "Namespace " + namespaceStr;
138 endNamespaceScopesCommandStr += "End Namespace";
139 }
140
141 values.insert( std::map<std::string,std::string>::value_type(
142 "#namespace_begin#",
143 namespaceScopesCommandStr
144 ) );
145 values.insert( std::map<std::string,std::string>::value_type(
146 "#namespace_end#",
147 endNamespaceScopesCommandStr
148 ) );
149 }
150 else
151 {
152 values.insert( std::map<std::string,std::string>::value_type( "#namespace_begin#", "" ) );
153 values.insert( std::map<std::string,std::string>::value_type( "#namespace_end#", "" ) );
154 }
155
156 values.insert( std::map<std::string,std::string>::value_type( "#name#", dg.GetName() ) );
157
158 std::string paramsStr = dg.Params().GetString();
159
160 if( dg.IsFunction() )
161 {
162 values.insert( std::map<std::string,std::string>::value_type(
163 "#call_method_begin#",
164 (string)"Function Call(" + paramsStr + ") As " + compiler.TypeToString( dg.ReturnType() )
165 ) );
166
167 values.insert( std::map<std::string,std::string>::value_type(
168 "#call_method_end#",
169 "End Function"
170 ) );
171
172 values.insert( std::map<std::string,std::string>::value_type( "#result#", "Call=" ) );
173 }
174 else
175 {
176 values.insert( std::map<std::string,std::string>::value_type(
177 "#call_method_begin#",
178 (string)"Sub Call(" + paramsStr + ")"
179 ) );
180
181 values.insert( std::map<std::string,std::string>::value_type(
182 "#call_method_end#",
183 "End Sub"
184 ) );
185
186 values.insert( std::map<std::string,std::string>::value_type( "#result#", "" ) );
187 }
188
189 values.insert( std::map<std::string,std::string>::value_type( "#params#", paramsStr ) );
190
191 destSource += sourceTemplate.GetResult( values );
192 }
193/*
194 ofstream ofs( ( Jenga::Common::Environment::GetAppDir() + "\\generated_delegate_code.log" ).c_str() );
195 ofs << destSource;
196 ofs.close();
197 */
198}
Note: See TracBrowser for help on using the repository browser.