OSDN Git Service

elccコンパイラの最適化機能を向上。
[chnosproject/AI004.git] / ai.js
1 //AI004
2
3 //開発時クロスドメイン許可で起動するには、
4 // /Applications/Google\ Chrome.app/ --allow-file-access-from-files --disable-web-security
5
6 var DebugModeEnabled = true;
7
8 function AI(messageBoxDOMObject, debugBoxDOMObject){
9         //ブラウザチェック
10         this.checkBrowser();
11         //サブクラス
12         this.input = new AI_Input(this);
13         this.wordRecognition = new AI_WordRecognition(this);
14         this.IOManager = new AI_IOManager(this);
15         this.networkManager = new AI_NetworkManager(this);
16         this.memory = new AI_Memory(this);
17         this.think = new AI_Think(this);
18         //出力関連
19         var that = this;
20         this.tickTimer = window.setInterval(function(){that.tick();}, 100);;
21         this.messageBox = null;
22         this.messageBoxBuffer = "";
23         this.maxMessageStringLength = 0xffff;
24         this.debugBox = null;
25         this.debugBoxBuffer = "";
26         this.maxDebugStringLength = 0xffff;
27         this.downloadBox = null;
28         //
29         this.setMessageBoxDOMObject(messageBoxDOMObject);
30         this.setDebugBoxDOMObject(debugBoxDOMObject);
31         this.modeList = [
32                 this.UUID_Mode_Standard,
33                 this.UUID_Mode_ReadMemory,
34                 this.UUID_Mode_SaveMemory,
35                 this.UUID_Mode_InternalConsole,
36                 this.UUID_Mode_CompileELCHNOS_OSECPU,
37         ];
38         this.modeProcessList = [
39                 this.inputProcess_Standard,
40                 this.inputProcess_ReadMemory,
41                 this.inputProcess_SaveMemory,
42                 this.inputProcess_InternalConsole,
43                 this.inputProcess_CompileELCHNOS_OSECPU,
44         ];
45         if(!DebugModeEnabled){
46                 this.mode = this.UUID_Mode_Standard;
47                 this.processByMode = this.inputProcess_Standard;
48         } else{
49                 this.mode = this.UUID_Mode_InternalConsole;
50                 this.processByMode = this.inputProcess_InternalConsole;
51         }
52
53         AI_Bootstrap(this);
54
55         this.debug("AI system initialized.\n");
56         this.debug("To enter internal console mode,\n  type '#4ca6ed1a-e62e-470b-9d7b-e332f709e48f'.\n");
57 }
58 AI.prototype = {
59         UUIDStrLen: 36,
60         UUID_Mode_Standard:                                     "1186f6f2-c7c4-4532-8f8f-c7dea883825f",
61         UUID_Mode_ReadMemory:                           "42e11880-62b8-46ea-a1c4-481264d4440d",
62         UUID_Mode_SaveMemory:                           "52360c62-6a8a-4f6e-8bdd-43381996e996",
63         UUID_Mode_InternalConsole:                      "4ca6ed1a-e62e-470b-9d7b-e332f709e48f",
64         UUID_Mode_CompileELCHNOS_OSECPU:        "17ddde48-7d4c-498f-98d8-3e73f8845028",
65         UUID_Meaning_UndefinedString :          "f9080ed9-1fd4-4982-a979-092d1852298a",
66         UUID_Meaning_UndefinedStrings :         "24393cc6-e6c6-4da2-ae19-8e74ff71d390",
67         sendToAI: function(str, srctype){
68                 var p, strbaseindex;
69                 
70                 this.debug("**** Start inputting ****\n");
71                 
72                 //まず入力文字を#で分割したリストを取得
73                 var ary = str.split("#");
74                 //モードを確認しつつ実行
75                 p = -(this.UUIDStrLen + 1);
76                 for(;;){
77                         strbaseindex = p + 1 + this.UUIDStrLen;
78                         p = str.indexOf("#", strbaseindex);
79                         if(p == -1){
80                                 //終端まで到達
81                                 //それ以前の文字列を入力
82                                 this.processByMode(str.substring(strbaseindex), srctype);
83                                 break;
84                         }
85                         //まずはモード変更の直前までの文字列を入力
86                         this.processByMode(str.substring(strbaseindex, p), srctype);
87                         //モード変更要求
88                         this.changeMode(str.substring(p + 1, p + 1 + this.UUIDStrLen));
89                 }
90                 
91                 this.debug("**** End inputting ****\n");
92         },
93         changeMode: function(modeUUIDStr){
94                 for(var i = 0, iLen = this.modeList.length; i < iLen; i++){
95                         if(this.modeList[i] == modeUUIDStr){
96                                 break;
97                         }
98                 }
99                 if(this.modeList.length <= i){
100                         this.debug("Unknown mode-UUID. Mode was NOT changed.\n");
101                         return;
102                 }
103                 if(this.modeList[i] === this.UUID_Mode_SaveMemory){
104                         this.debug("Mode exectute for this once.\n    uuid:" + this.modeList[i] + "\n");
105                         this.modeProcessList[i].call(this);
106                 } else{
107                         this.debug("Mode changed\n    from:" + this.mode + "\n    to  :" + this.modeList[i] + "\n");
108                         this.mode = this.modeList[i];
109                         this.processByMode = this.modeProcessList[i];
110                 }
111                 
112         },
113         sendTextFromFileToAI: function(str, name, modDate, srctype){
114                 //ファイルからの読み込み時は、読み込み終了後に読み込み以前のモードに戻る。
115                 this.debug("sendTextFromFileToAI:");
116                 if(modDate){
117                         this.debug(modDate.toLocaleString());
118                 }
119                 this.debug(" [" + name + "]\n");
120                 var oldmode = this.mode;
121                 this.sendToAI(str, srctype);
122                 this.changeMode(oldmode);
123         },
124         setMessageBoxDOMObject: function(mBoxObj){
125                 this.messageBox = mBoxObj;
126         },
127         setDebugBoxDOMObject: function(dBoxObj){
128                 this.debugBox = dBoxObj;
129         },
130         message: function(str, noPrefix){
131                 if(this.messageBox){
132                         if(!noPrefix){
133                                 this.messageBoxBuffer += "AI> " + str;
134                         } else{
135                                 this.messageBoxBuffer += str;
136                         }
137                 }
138         },
139         debug: function(str){
140                 if(this.debugBox){
141                         this.debugBoxBuffer += str;
142                 }
143         },
144         tick: function(){
145                 //思考処理
146                 this.think.tick();
147                 //表示処理
148                 if(this.messageBox && this.messageBoxBuffer != ""){
149                         //messageBox
150                         var str = this.messageBox.value + this.messageBoxBuffer;
151                         this.messageBoxBuffer = "";
152                         if(str.length > this.maxMessageStringLength){
153                                 str = str.slice(str.length - (this.maxMessageStringLength >> 1));
154                         }
155                         this.messageBox.value = str;
156                         this.messageBox.scrollTop = this.messageBox.scrollHeight;
157                 }
158                 if(this.debugBox && this.debugBoxBuffer != ""){
159                         //debugBox
160                         var str = this.debugBox.value + this.debugBoxBuffer;
161                         this.debugBoxBuffer = "";
162                         if(str.length > this.maxDebugStringLength){
163                                 str = str.slice(str.length - (this.maxDebugStringLength >> 1));
164                         }
165                         this.debugBox.value = str;
166                         this.debugBox.scrollTop = this.debugBox.scrollHeight;
167                 }
168         },
169         checkBrowser: function(){
170                 if(!window.File){
171                         this.message("System> このブラウザは記憶保存(HTML5FileAPI)に対応していません。", true);
172                 }
173         },
174         inputProcess_Standard: function(str, srctype){
175                 this.debug("**** Start Processing (Standard) ****\n");
176                 
177                 this.debug("input:[" + str + "]\n");
178                 this.input.appendInput(str, srctype);
179                 
180                 this.debug("**** End Processing (Standard) ****\n");
181         },
182         inputProcess_ReadMemory: function(str, srctype){
183                 this.debug("**** Start Processing (ReadMemory) ****\n");
184                 
185                 this.memory.loadMemory(str);
186                 
187                 this.debug("**** End Processing (ReadMemory) ****\n");
188         },
189         inputProcess_SaveMemory: function(str, srctype){
190                 this.debug("**** Start Processing (SaveMemory) ****\n");
191                 
192                 this.memory.saveMemory();
193                 
194                 this.debug("**** End Processing (SaveMemory) ****\n");
195         },
196         inputProcess_InternalConsole: function(str, srctype){
197                 var that = this;
198                 this.debug("**** Start Processing (InternalConsole) ****\n");
199                 if(str == "exit"){
200                         this.debug("Exit InternalConsole.\n");
201                         this.changeMode(this.UUID_Mode_Standard);
202                 } else if(str == "show cwl"){
203                         //show candidateWordList
204                         this.wordRecognition.debugShowCandidateWordList();
205                 } else if(str == "show wl"){
206                         //show wordList
207                         this.debug("wordList:" + this.memory.wordList.length + "\n" );
208                         this.memory.wordList.logEachPropertyNamed("str", function(s){ that.debug(s); });
209                 } else if(str.indexOf("inputFromURL ") == 0){
210                         //webページを読み込む
211                         //inputFromURL http://www.aozora.gr.jp/cards/000148/files/773_14560.html
212                         //inputFromURL http://www.aozora.gr.jp/cards/000035/files/1567_14913.html
213                         //inputFromURL http://www.aozora.gr.jp/cards/000148/files/752_14964.html
214                         //inputFromURL http://www.aozora.gr.jp/cards/000035/files/301_14912.html
215                         //inputFromURL http://www.aozora.gr.jp/cards/000160/files/3368_25725.html
216                         //inputFromURL http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8
217                         //inputFromURL http://ja.wikipedia.org/wiki/%E6%9D%B1%E4%BA%AC%E5%AD%A6%E8%8A%B8%E5%A4%A7%E5%AD%A6%E9%99%84%E5%B1%9E%E9%AB%98%E7%AD%89%E5%AD%A6%E6%A0%A1
218                         //inputFromURL http://osecpu.osask.jp/wiki/?FrontPage
219                         //inputFromURL http://osecpu.osask.jp/wiki/?impressions
220                         
221                         var url = str.substring(13);
222                         this.debug("[" + url + "]\n");
223                         var res = this.networkManager.sendRequestThroughPHPSync("GET", url, null);
224                         //this.debug("[" + res + "]\n");
225                         var parser = new AI_HTMLParser(this);
226                         parser.loadText(res);
227                         var mainString = parser.mainString;
228                         this.debug(mainString);
229                         console.log(parser.linkList);
230                         
231                         this.changeMode(this.UUID_Mode_Standard);
232                         this.sendTextFromFileToAI(mainString, url, null, "Web");
233                         this.changeMode(this.UUID_Mode_InternalConsole);
234                         
235                 } else if(str == "savemem"){
236                         this.memory.saveMemory();
237                 } else{
238                         this.debug("Unknown command [" + str + "].\n");
239                         this.debug("Command list:\n");
240                         this.debug("Implemented command list:\n");
241                         this.debug("  show cwl\n");
242                         this.debug("  show wl\n");
243                         this.debug("  exit\n");
244                 }
245                 
246                 this.debug("**** End Processing (InternalConsole) ****\n");
247         },
248         inputProcess_CompileELCHNOS_OSECPU: function(str, srctype){
249                 this.debug("**** Start Processing (CompileELCHNOS_OSECPU) ****\n");
250                 
251                 var that = this;
252                 var cc = new ELCHNOSCompiler(function(s){ that.debug(s); }, this.downloadBox);
253                 if(cc.compile(str) != null){
254                         cc.saveBinary();
255                 }
256                 
257                 this.debug("**** End Processing (CompileELCHNOS_OSECPU) ****\n");
258         },
259 }