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