#include "common.h" char *basbuf; extern CDefine *pobj_define; CSource CSource::obj; CToken::CToken(int pos, int length, TOKENTYPE type, int extended){ this->pos = pos; this->length = length; this->type = type; this->extended = extended; } CToken::~CToken(){ } CSource::CSource(){ } CSource::~CSource(){ Free(); } void CSource::Free(){ if(SourceCode){ char *base = SourceCode-2; free(base); SourceCode = 0; } if(ppTokens){ for(int i=0; i\n"); buffer+=lstrlen(buffer); //読み込み lstrcpy(buffer,source); //CRLFをLFに変換 ChangeReturnCode(buffer); //コメント削除 DeleteComment(buffer); //#ifdefディレクティブ DirectiveIfdef(buffer); //最終行には文字を含ませないようにする lstrcat(buffer,"\n"); //インクルードファイルを読み込む base=IncludeFiles(base); buffer = base + 2; //#define情報を破棄 delete pobj_define; pobj_define=0; SourceCode = buffer; } void CSource::AddSourceCode(char *source){ char *temp; temp=(char *)malloc(lstrlen(source)+8192); lstrcpy(temp,source); //エスケープシーケンス設定 SetEscapeSequenceFormat(temp); //コマンド対応 ChangeCommandToCode(temp); //新しいソースコードバッファの容量 extern char *basbuf; int NewSize; NewSize=lstrlen(SourceCode)+lstrlen(temp); NewSize*=2; NewSize+=255; //最後尾に貼り付け char *base; base = SourceCode - 2; base = (char *)realloc(base, NewSize); SourceCode = base + 2; int oldlength = lstrlen(SourceCode); lstrcat(SourceCode, temp); free(temp); //字句解析 CSource::obj.LexicalAnalysis(0); } //トークンを追加 void CSource::AddToken(int pos, int length, TOKENTYPE type, int extended){ CToken *ptoken = new CToken(pos, length, type, extended); ppTokens = (CToken **)realloc(ppTokens, (TokenNum + 1) * sizeof(CToken *)); ppTokens[TokenNum] = ptoken; TokenNum++; } //字句解析(トークンを生成) void CSource::LexicalAnalysis(int StartPos){ int i = StartPos; int i2; try{ while(true){ if(SourceCode[i] == '\0') break; //文字数 int length; //トークンの種類 TOKENTYPE type; //付加情報 int extended = 0; if(IsNumCalcMark(SourceCode, i)){ //演算子 type = TOKEN_OPERATOR; extended = GetCalcId(SourceCode + i, &i2); length = i2 + 1; } else if(SourceCode[i] == 1){ //特殊字句 type = TOKEN_ESCAPESEQUENCE; length = 2; } else if(IsNumberTopChar(SourceCode + i)){ //数値 type = TOKEN_NUMBER; if(SourceCode[i] == '&') length = 2; else length = 0; for(length=0; IsNumberChar(SourceCode[i+length]); length++){ } } else if(SourceCode[i] == '\"'){ //文字列 type = TOKEN_STRING; for(length=1;;length++){ if(SourceCode[i + length]=='\0') break; if(SourceCode[i + length] == '\"'){ length++; break; } } } else if(memicmp(SourceCode+i,"Ex",2) && SourceCode[i+3]=='\"'){ //拡張文字列 type = TOKEN_EXSTRING; for(length=3;;length++){ if(SourceCode[i + length]=='\0') break; if(SourceCode[i + length] == '\"'){ length++; break; } } } else if(IsVariableTopChar(SourceCode[i])){ //識別子 type = TOKEN_IDENTIFIER; for(length=0; IsVariableChar(SourceCode[i+length]); length++){ } } else if(SourceCode[i] == 0x10 || SourceCode[i] == 0x11){ //コマンド type = TOKEN_COMMAND; length = 2; } else{ throw "字句解析に失敗\n"; } if(length == 0){ throw "字句解析に失敗\n"; } //トークンを追加 AddToken(i, length, type, extended); //次の字句の位置へ i += length; //空白を除去 while(IsBlank(SourceCode[i])) i++; } } catch(const char *msg){ OutputDebugString(msg); } }