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

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

デリゲートのベースがほぼ実装できた

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