[526] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
| 3 | using namespace ActiveBasic::Compiler;
|
---|
| 4 |
|
---|
| 5 | void LexicalAnalyzer::CollectDelegates( const BasicSource &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 | for( int i=0; i<source.GetLength(); i++ )
|
---|
| 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 | }
|
---|
| 58 | else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
|
---|
| 59 | importedNamespaces.clear();
|
---|
| 60 | continue;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | else if( source[i] == 1 && source[i+1] == ESC_DELEGATE )
|
---|
| 64 | {
|
---|
| 65 | int nowLine = i;
|
---|
| 66 |
|
---|
| 67 | i += 2;
|
---|
| 68 | if( !( source[i] == 1 && ( source[i+1] == ESC_SUB || source[i+1] == ESC_FUNCTION ) ) )
|
---|
| 69 | {
|
---|
| 70 | compiler.errorMessenger.Output(1,NULL,i);
|
---|
| 71 | continue;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | Procedure::Kind procKind = Procedure::Sub;
|
---|
| 75 | if( source[i+1] == ESC_FUNCTION )
|
---|
| 76 | {
|
---|
| 77 | procKind = Procedure::Function;
|
---|
| 78 | }
|
---|
| 79 | i += 2;
|
---|
| 80 |
|
---|
| 81 | // 名前
|
---|
| 82 | char name[VN_SIZE];
|
---|
| 83 | GetIdentifierToken( name, source.GetBuffer(), i );
|
---|
| 84 |
|
---|
| 85 | if( source[i] != '(' )
|
---|
| 86 | {
|
---|
| 87 | compiler.errorMessenger.Output(1,NULL,nowLine);
|
---|
| 88 | continue;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // パラメータ文字列
|
---|
| 92 | char paramStr[8192];
|
---|
| 93 | i += GetStringInPare( paramStr, source.GetBuffer() + i, true );
|
---|
| 94 |
|
---|
| 95 | // 戻り値の型の文字列
|
---|
| 96 | char returnTypeName[VN_SIZE] = "";
|
---|
| 97 | if( source[i] == 1 && source[i+1] == ESC_AS )
|
---|
| 98 | {
|
---|
| 99 | i += 2;
|
---|
| 100 | GetCommandToken( returnTypeName, source.GetBuffer(), i );
|
---|
| 101 |
|
---|
| 102 | if( procKind != Procedure::Function )
|
---|
| 103 | {
|
---|
| 104 | compiler.errorMessenger.Output(38,name,nowLine);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | else
|
---|
| 108 | {
|
---|
| 109 | if( procKind == Procedure::Function )
|
---|
| 110 | {
|
---|
| 111 | compiler.errorMessenger.Output(-104,name,nowLine);
|
---|
| 112 | lstrcpy( returnTypeName, "Double" );
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | delegates.Put( new Delegate( namespaceScopes, importedNamespaces, name, procKind, paramStr, returnTypeName, nowLine ) );
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | std::string LexicalAnalyzer::GenerateDelegatesSourceCode( const Delegates &delegates )
|
---|
| 122 | {
|
---|
| 123 | std::string destSource = "";
|
---|
| 124 |
|
---|
| 125 | SourceTemplate sourceTemplate( ActiveBasic::Common::Environment::GetAbdevSystemDirPath() + "\\templates\\delegate_class.tab" );
|
---|
| 126 |
|
---|
| 127 | delegates.Iterator_Reset();
|
---|
| 128 | while( delegates.Iterator_HasNext() )
|
---|
| 129 | {
|
---|
| 130 | const Delegate &dg = *delegates.Iterator_GetNext();
|
---|
| 131 |
|
---|
| 132 | if( !dg.isTargetObjectModule )
|
---|
| 133 | {
|
---|
| 134 | // 静的リンクライブラリの場合は飛ばす(既にインスタンスが定義済みであるため)
|
---|
| 135 | continue;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | std::map<std::string,std::string> values;
|
---|
| 139 |
|
---|
| 140 | if( dg.GetNamespaceScopes().size() )
|
---|
| 141 | {
|
---|
| 142 | std::string namespaceScopesCommandStr = "";
|
---|
| 143 | std::string endNamespaceScopesCommandStr = "";
|
---|
| 144 | BOOST_FOREACH( const std::string &namespaceStr, dg.GetNamespaceScopes() )
|
---|
| 145 | {
|
---|
| 146 | if( namespaceScopesCommandStr.size() )
|
---|
| 147 | {
|
---|
| 148 | namespaceScopesCommandStr += ":";
|
---|
| 149 | endNamespaceScopesCommandStr += ":";
|
---|
| 150 | }
|
---|
| 151 | namespaceScopesCommandStr += "Namespace " + namespaceStr;
|
---|
| 152 | endNamespaceScopesCommandStr += "End Namespace";
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 156 | "#namespace_begin#",
|
---|
| 157 | namespaceScopesCommandStr
|
---|
| 158 | ) );
|
---|
| 159 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 160 | "#namespace_end#",
|
---|
| 161 | endNamespaceScopesCommandStr
|
---|
| 162 | ) );
|
---|
| 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
| 166 | values.insert( std::map<std::string,std::string>::value_type( "#namespace_begin#", "" ) );
|
---|
| 167 | values.insert( std::map<std::string,std::string>::value_type( "#namespace_end#", "" ) );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | values.insert( std::map<std::string,std::string>::value_type( "#name#", dg.GetName() ) );
|
---|
| 171 |
|
---|
| 172 | if( dg.IsFunction() )
|
---|
| 173 | {
|
---|
| 174 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 175 | "#call_method_begin#",
|
---|
| 176 | (std::string)"Function Call(" + dg.GetParamStr() + ") As " + dg.GetReturnTypeName()
|
---|
| 177 | ) );
|
---|
| 178 |
|
---|
| 179 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 180 | "#call_method_end#",
|
---|
| 181 | "End Function"
|
---|
| 182 | ) );
|
---|
| 183 |
|
---|
| 184 | values.insert( std::map<std::string,std::string>::value_type( "#result#", "Call=" ) );
|
---|
| 185 | }
|
---|
| 186 | else
|
---|
| 187 | {
|
---|
| 188 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 189 | "#call_method_begin#",
|
---|
| 190 | (std::string)"Sub Call(" + dg.GetParamStr() + ")"
|
---|
| 191 | ) );
|
---|
| 192 |
|
---|
| 193 | values.insert( std::map<std::string,std::string>::value_type(
|
---|
| 194 | "#call_method_end#",
|
---|
| 195 | "End Sub"
|
---|
| 196 | ) );
|
---|
| 197 |
|
---|
| 198 | values.insert( std::map<std::string,std::string>::value_type( "#result#", "" ) );
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | values.insert( std::map<std::string,std::string>::value_type( "#params#", dg.GetParamStr() ) );
|
---|
| 202 |
|
---|
| 203 | destSource += sourceTemplate.GetResult( values );
|
---|
| 204 | }
|
---|
| 205 | /*
|
---|
| 206 | std::ofstream ofs( ( Jenga::Common::Environment::GetAppDir() + "\\generated_delegate_code.log" ).c_str() );
|
---|
| 207 | ofs << destSource;
|
---|
| 208 | ofs.close();
|
---|
| 209 | */
|
---|
| 210 |
|
---|
| 211 | return destSource;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | void LexicalAnalyzer::RefleshDelegatesParameterAndReturnType( Delegates &delegates )
|
---|
| 215 | {
|
---|
| 216 | delegates.Iterator_Reset();
|
---|
| 217 | while( delegates.Iterator_HasNext() )
|
---|
| 218 | {
|
---|
| 219 | Delegate &dg = *delegates.Iterator_GetNext();
|
---|
| 220 | dg.RefleshParameterAndReturnType();
|
---|
| 221 | }
|
---|
| 222 | }
|
---|