| 1 | #include "stdafx.h" | 
|---|
| 2 |  | 
|---|
| 3 | using namespace ActiveBasic::Compiler; | 
|---|
| 4 |  | 
|---|
| 5 | void LexicalAnalyzer::CollectDelegates( const char *source, Delegates &delegates ) | 
|---|
| 6 | { | 
|---|
| 7 | int i2; | 
|---|
| 8 | char temporary[VN_SIZE]; | 
|---|
| 9 |  | 
|---|
| 10 | // 名前空間管理 | 
|---|
| 11 | NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes(); | 
|---|
| 12 | namespaceScopes.clear(); | 
|---|
| 13 |  | 
|---|
| 14 | // Importsされた名前空間の管理 | 
|---|
| 15 | NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces(); | 
|---|
| 16 | importedNamespaces.clear(); | 
|---|
| 17 |  | 
|---|
| 18 | int length = lstrlen( source ); | 
|---|
| 19 | for( int i=0; i<length; i++ ) | 
|---|
| 20 | { | 
|---|
| 21 | if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){ | 
|---|
| 22 | for(i+=2,i2=0;;i2++,i++){ | 
|---|
| 23 | if( IsCommandDelimitation( source[i] ) ){ | 
|---|
| 24 | temporary[i2]=0; | 
|---|
| 25 | break; | 
|---|
| 26 | } | 
|---|
| 27 | temporary[i2]=source[i]; | 
|---|
| 28 | } | 
|---|
| 29 | namespaceScopes.push_back( temporary ); | 
|---|
| 30 |  | 
|---|
| 31 | continue; | 
|---|
| 32 | } | 
|---|
| 33 | else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){ | 
|---|
| 34 | if( namespaceScopes.size() <= 0 ){ | 
|---|
| 35 | compiler.errorMessenger.Output(12, "End Namespace", i ); | 
|---|
| 36 | } | 
|---|
| 37 | else{ | 
|---|
| 38 | namespaceScopes.pop_back(); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | i += 2; | 
|---|
| 42 | continue; | 
|---|
| 43 | } | 
|---|
| 44 | else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){ | 
|---|
| 45 | for(i+=2,i2=0;;i2++,i++){ | 
|---|
| 46 | if( IsCommandDelimitation( source[i] ) ){ | 
|---|
| 47 | temporary[i2]=0; | 
|---|
| 48 | break; | 
|---|
| 49 | } | 
|---|
| 50 | temporary[i2]=source[i]; | 
|---|
| 51 | } | 
|---|
| 52 | if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) ) | 
|---|
| 53 | { | 
|---|
| 54 | compiler.errorMessenger.Output(64,temporary,i ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | continue; | 
|---|
| 58 | } | 
|---|
| 59 | else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){ | 
|---|
| 60 | importedNamespaces.clear(); | 
|---|
| 61 | continue; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | else if( source[i] == 1 && source[i+1] == ESC_DELEGATE ) | 
|---|
| 65 | { | 
|---|
| 66 | int nowLine = i; | 
|---|
| 67 |  | 
|---|
| 68 | i += 2; | 
|---|
| 69 | if( !( source[i] == 1 && ( source[i+1] == ESC_SUB || source[i+1] == ESC_FUNCTION ) ) ) | 
|---|
| 70 | { | 
|---|
| 71 | compiler.errorMessenger.Output(1,NULL,i); | 
|---|
| 72 | continue; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | Procedure::Kind procKind = Procedure::Sub; | 
|---|
| 76 | if( source[i+1] == ESC_FUNCTION ) | 
|---|
| 77 | { | 
|---|
| 78 | procKind = Procedure::Function; | 
|---|
| 79 | } | 
|---|
| 80 | i += 2; | 
|---|
| 81 |  | 
|---|
| 82 | // 名前 | 
|---|
| 83 | char name[VN_SIZE]; | 
|---|
| 84 | GetIdentifierToken( name, source, i ); | 
|---|
| 85 |  | 
|---|
| 86 | if( source[i] != '(' ) | 
|---|
| 87 | { | 
|---|
| 88 | compiler.errorMessenger.Output(1,NULL,nowLine); | 
|---|
| 89 | continue; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | // パラメータ文字列 | 
|---|
| 93 | char paramStr[8192]; | 
|---|
| 94 | i += GetStringInPare( paramStr, source + i, true ); | 
|---|
| 95 |  | 
|---|
| 96 | // 戻り値の型の文字列 | 
|---|
| 97 | char returnTypeName[VN_SIZE] = ""; | 
|---|
| 98 | if( source[i] == 1 && source[i+1] == ESC_AS ) | 
|---|
| 99 | { | 
|---|
| 100 | i += 2; | 
|---|
| 101 | GetCommandToken( returnTypeName, source, i ); | 
|---|
| 102 |  | 
|---|
| 103 | if( procKind != Procedure::Function ) | 
|---|
| 104 | { | 
|---|
| 105 | compiler.errorMessenger.Output(38,name,nowLine); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 | else | 
|---|
| 109 | { | 
|---|
| 110 | if( procKind == Procedure::Function ) | 
|---|
| 111 | { | 
|---|
| 112 | compiler.errorMessenger.Output(-104,name,nowLine); | 
|---|
| 113 | lstrcpy( returnTypeName, "Double" ); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | delegates.Put( new Delegate( namespaceScopes, importedNamespaces, name, procKind, paramStr, returnTypeName, nowLine ) ); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | std::string LexicalAnalyzer::GenerateDelegatesSourceCode( const Delegates &delegates ) | 
|---|
| 123 | { | 
|---|
| 124 | std::string destSource = ""; | 
|---|
| 125 |  | 
|---|
| 126 | SourceTemplate sourceTemplate( ActiveBasic::Common::Environment::GetAbdevSystemDirPath() + "\\templates\\delegate_class.tab" ); | 
|---|
| 127 |  | 
|---|
| 128 | delegates.Iterator_Reset(); | 
|---|
| 129 | while( delegates.Iterator_HasNext() ) | 
|---|
| 130 | { | 
|---|
| 131 | const Delegate &dg = *delegates.Iterator_GetNext(); | 
|---|
| 132 |  | 
|---|
| 133 | if( !dg.isTargetObjectModule ) | 
|---|
| 134 | { | 
|---|
| 135 | // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため) | 
|---|
| 136 | continue; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | std::map<std::string,std::string> values; | 
|---|
| 140 |  | 
|---|
| 141 | if( dg.GetNamespaceScopes().size() ) | 
|---|
| 142 | { | 
|---|
| 143 | std::string namespaceScopesCommandStr = ""; | 
|---|
| 144 | std::string endNamespaceScopesCommandStr = ""; | 
|---|
| 145 | BOOST_FOREACH( const std::string &namespaceStr, dg.GetNamespaceScopes() ) | 
|---|
| 146 | { | 
|---|
| 147 | if( namespaceScopesCommandStr.size() ) | 
|---|
| 148 | { | 
|---|
| 149 | namespaceScopesCommandStr += ":"; | 
|---|
| 150 | endNamespaceScopesCommandStr += ":"; | 
|---|
| 151 | } | 
|---|
| 152 | namespaceScopesCommandStr += "Namespace " + namespaceStr; | 
|---|
| 153 | endNamespaceScopesCommandStr += "End Namespace"; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 157 | "#namespace_begin#", | 
|---|
| 158 | namespaceScopesCommandStr | 
|---|
| 159 | ) ); | 
|---|
| 160 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 161 | "#namespace_end#", | 
|---|
| 162 | endNamespaceScopesCommandStr | 
|---|
| 163 | ) ); | 
|---|
| 164 | } | 
|---|
| 165 | else | 
|---|
| 166 | { | 
|---|
| 167 | values.insert( std::map<std::string,std::string>::value_type( "#namespace_begin#", "" ) ); | 
|---|
| 168 | values.insert( std::map<std::string,std::string>::value_type( "#namespace_end#", "" ) ); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | values.insert( std::map<std::string,std::string>::value_type( "#name#", dg.GetName() ) ); | 
|---|
| 172 |  | 
|---|
| 173 | if( dg.IsFunction() ) | 
|---|
| 174 | { | 
|---|
| 175 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 176 | "#call_method_begin#", | 
|---|
| 177 | (std::string)"Function Call(" + dg.GetParamStr() + ") As " + dg.GetReturnTypeName() | 
|---|
| 178 | ) ); | 
|---|
| 179 |  | 
|---|
| 180 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 181 | "#call_method_end#", | 
|---|
| 182 | "End Function" | 
|---|
| 183 | ) ); | 
|---|
| 184 |  | 
|---|
| 185 | values.insert( std::map<std::string,std::string>::value_type( "#result#", "Call=" ) ); | 
|---|
| 186 | } | 
|---|
| 187 | else | 
|---|
| 188 | { | 
|---|
| 189 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 190 | "#call_method_begin#", | 
|---|
| 191 | (std::string)"Sub Call(" + dg.GetParamStr() + ")" | 
|---|
| 192 | ) ); | 
|---|
| 193 |  | 
|---|
| 194 | values.insert( std::map<std::string,std::string>::value_type( | 
|---|
| 195 | "#call_method_end#", | 
|---|
| 196 | "End Sub" | 
|---|
| 197 | ) ); | 
|---|
| 198 |  | 
|---|
| 199 | values.insert( std::map<std::string,std::string>::value_type( "#result#", "" ) ); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | values.insert( std::map<std::string,std::string>::value_type( "#params#", dg.GetParamStr() ) ); | 
|---|
| 203 |  | 
|---|
| 204 | destSource += sourceTemplate.GetResult( values ); | 
|---|
| 205 | } | 
|---|
| 206 | /* | 
|---|
| 207 | std::ofstream ofs( ( Jenga::Common::Environment::GetAppDir() + "\\generated_delegate_code.log" ).c_str() ); | 
|---|
| 208 | ofs << destSource; | 
|---|
| 209 | ofs.close(); | 
|---|
| 210 | */ | 
|---|
| 211 |  | 
|---|
| 212 | return destSource; | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | void LexicalAnalyzer::RefleshDelegateParameterAndReturnType( Delegate &dg ) | 
|---|
| 216 | { | 
|---|
| 217 | compiler.GetNamespaceSupporter().SetImportedNamespaces( dg.GetImportedNamespaces() ); | 
|---|
| 218 | compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( dg.GetNamespaceScopes() ); | 
|---|
| 219 |  | 
|---|
| 220 | // パラメータを解析 | 
|---|
| 221 | Jenga::Common::Strings parameterStrings; | 
|---|
| 222 | SplitParameter( dg.GetParamStr(), parameterStrings ); | 
|---|
| 223 | int sourceIndex; | 
|---|
| 224 | ActiveBasic::Compiler::LexicalAnalyzer::AnalyzeParameter( dg.GetParameters(), parameterStrings, sourceIndex ); | 
|---|
| 225 | dg.SetSourceIndex( sourceIndex ); | 
|---|
| 226 |  | 
|---|
| 227 | // 動的パラメータを作る | 
|---|
| 228 | dg.GetDynamicParams() = dg.GetParameters(); | 
|---|
| 229 | dg.GetDynamicParams().insert( dg.GetDynamicParams().begin(), new Parameter( "_System_LocalThis", Type( DEF_PTR_VOID ) ) ); | 
|---|
| 230 |  | 
|---|
| 231 | if( dg.IsFunction() ) | 
|---|
| 232 | { | 
|---|
| 233 | // 戻り値を取得 | 
|---|
| 234 | Type returnType; | 
|---|
| 235 | if( !compiler.StringToType( dg.GetReturnTypeName(), returnType ) ) | 
|---|
| 236 | { | 
|---|
| 237 | compiler.errorMessenger.Output(3,dg.GetReturnTypeName(),sourceIndex); | 
|---|
| 238 | } | 
|---|
| 239 | else | 
|---|
| 240 | { | 
|---|
| 241 | dg.SetReturnType( returnType ); | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void LexicalAnalyzer::RefleshDelegatesParameterAndReturnType( Delegates &delegates ) | 
|---|
| 247 | { | 
|---|
| 248 | delegates.Iterator_Reset(); | 
|---|
| 249 | while( delegates.Iterator_HasNext() ) | 
|---|
| 250 | { | 
|---|
| 251 | Delegate &dg = *delegates.Iterator_GetNext(); | 
|---|
| 252 | RefleshDelegateParameterAndReturnType( dg ); | 
|---|
| 253 | } | 
|---|
| 254 | } | 
|---|