[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 |
|
---|
| 14 | // Importsされた名前空間の管理
|
---|
| 15 | NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
|
---|
| 16 | importedNamespaces.clear();
|
---|
| 17 |
|
---|
[552] | 18 | int length = lstrlen( source );
|
---|
| 19 | for( int i=0; i<length; i++ )
|
---|
[526] | 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];
|
---|
[552] | 84 | GetIdentifierToken( name, source, i );
|
---|
[526] | 85 |
|
---|
| 86 | if( source[i] != '(' )
|
---|
| 87 | {
|
---|
| 88 | compiler.errorMessenger.Output(1,NULL,nowLine);
|
---|
| 89 | continue;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | // パラメータ文字列
|
---|
| 93 | char paramStr[8192];
|
---|
[552] | 94 | i += GetStringInPare( paramStr, source + i, true );
|
---|
[526] | 95 |
|
---|
| 96 | // 戻り値の型の文字列
|
---|
| 97 | char returnTypeName[VN_SIZE] = "";
|
---|
| 98 | if( source[i] == 1 && source[i+1] == ESC_AS )
|
---|
| 99 | {
|
---|
| 100 | i += 2;
|
---|
[552] | 101 | GetCommandToken( returnTypeName, source, i );
|
---|
[526] | 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 |
|
---|
[637] | 117 | delegates.Put( new Delegate( Symbol( namespaceScopes, name ), importedNamespaces, procKind, paramStr, returnTypeName, nowLine ) );
|
---|
[526] | 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | std::string LexicalAnalyzer::GenerateDelegatesSourceCode( const Delegates &delegates )
|
---|
| 123 | {
|
---|
| 124 | std::string destSource = "";
|
---|
| 125 |
|
---|
[625] | 126 | Jenga::Common::SourceTemplate sourceTemplate( ActiveBasic::Common::Environment::GetAbdevSystemDirPath() + "\\templates\\delegate_class.tab" );
|
---|
[526] | 127 |
|
---|
| 128 | delegates.Iterator_Reset();
|
---|
| 129 | while( delegates.Iterator_HasNext() )
|
---|
| 130 | {
|
---|
| 131 | const Delegate &dg = *delegates.Iterator_GetNext();
|
---|
| 132 |
|
---|
[637] | 133 | if( dg.IsExternal() )
|
---|
[526] | 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 |
|
---|
[581] | 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 );
|
---|
[588] | 223 | ActiveBasic::Compiler::LexicalAnalyzer::AnalyzeParameter( dg.GetParameters(), parameterStrings, dg.GetSourceIndex() );
|
---|
[581] | 224 |
|
---|
| 225 | // 動的パラメータを作る
|
---|
| 226 | dg.GetDynamicParams() = dg.GetParameters();
|
---|
| 227 | dg.GetDynamicParams().insert( dg.GetDynamicParams().begin(), new Parameter( "_System_LocalThis", Type( DEF_PTR_VOID ) ) );
|
---|
| 228 |
|
---|
| 229 | if( dg.IsFunction() )
|
---|
| 230 | {
|
---|
| 231 | // 戻り値を取得
|
---|
| 232 | Type returnType;
|
---|
| 233 | if( !compiler.StringToType( dg.GetReturnTypeName(), returnType ) )
|
---|
| 234 | {
|
---|
[588] | 235 | compiler.errorMessenger.Output(3,dg.GetReturnTypeName(),dg.GetSourceIndex());
|
---|
[581] | 236 | }
|
---|
| 237 | else
|
---|
| 238 | {
|
---|
| 239 | dg.SetReturnType( returnType );
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[526] | 244 | void LexicalAnalyzer::RefleshDelegatesParameterAndReturnType( Delegates &delegates )
|
---|
| 245 | {
|
---|
| 246 | delegates.Iterator_Reset();
|
---|
| 247 | while( delegates.Iterator_HasNext() )
|
---|
| 248 | {
|
---|
| 249 | Delegate &dg = *delegates.Iterator_GetNext();
|
---|
[581] | 250 | RefleshDelegateParameterAndReturnType( dg );
|
---|
[526] | 251 | }
|
---|
| 252 | }
|
---|