Lua1.1 輸入準備

來源:互聯網
上載者:User

標籤:io   os   ar   檔案   sp   cti   代碼   on   c   

接著看 main 調用,在庫開啟之後,會調用 lua_dostring 或 lua_dofile。

lua_dostring 是從標準輸入讀取 lua 代碼。
lua_dofile 是從檔案讀取 lua 代碼,我們來看下這兩種有什麼區別。

lua_dostring
調用 lua_openstring,
opcode.c:

/*** Generate opcode stored on string and execute global statement. Return 0 on** success or 1 on error.*/int lua_dostring (char *string){ if (lua_openstring (string)) return 1; if (lua_parse ()) return 1; lua_closestring(); return 0;}

開啟輸入字串,文法分析,之後關閉輸入。

我們看下 lua_openstring 做了什麼
inout.c 檔案中

/*** Function to open a string to be input unit*/int lua_openstring (char *s){ lua_linenumber = 1; lua_setinput (stringinput); st = s; {  char sn[64];  sprintf (sn, "String: %10.10s...", s);  if (lua_addfile (sn)) return 1; } return 0;}

設定開始行號為 1
給詞法分析器設定輸入 lua_setinput (lex.c 中)。
把輸入的字串地址儲存在 st
把檔案名稱儲存到檔案名稱數組中去 lua_addfile (table.c 中)。

void lua_setinput (Input fn){  current = ‘ ‘;  input = fn;}

設定當前字元,和 input 回調。這裡是 stringinput
inout.c 檔案中:

/*** Function to get the next character from the input string*/static int stringinput (void){ st++; return (*(st-1));}

注釋裡已經說的很清楚了,從輸入的字串中取得一個字元。

lua_addfile 把檔案添加到檔案數組中。
table.c 檔案

/*** Add a file name at file table, checking overflow. This function also set** the external variable "lua_filename" with the function filename set.** Return 0 on success or 1 on error.*/int lua_addfile (char *fn){ if (lua_nfile >= MAXFILE-1) {  lua_error ("too many files");  return 1; } if ((lua_file[lua_nfile++] = strdup (fn)) == NULL) {  lua_error ("not enough memory");  return 1; } return 0;}

再看看 lua_dofile 是做什麼的。
opcode.c 檔案中:

/*** Open file, generate opcode and execute global statement. Return 0 on** success or 1 on error.*/int lua_dofile (char *filename){ if (lua_openfile (filename)) return 1; if (lua_parse ()) { lua_closefile (); return 1; } lua_closefile (); return 0;}

開啟檔案,文法分析,關閉檔案。

inout.c 檔案中

/*** Function to open a file to be input unit. ** Return 0 on success or 1 on error.*/int lua_openfile (char *fn){ lua_linenumber = 1; lua_setinput (fileinput); fp = fopen (fn, "r"); if (fp == NULL) return 1; if (lua_addfile (fn)) return 1; return 0;}

設定行號,設定詞法分析的輸入,
這裡調用的還是 lex.c 中的 lua_setinput。經過這樣的處理,詞法分析的時候已經沒有標準輸入或者檔案輸入的概念,詞法分析只管在需要的時候從函數輸入指標取字元,不同的輸入在這裡已經是透明的了。

開啟檔案,之後 lua_addfile , 把檔案名稱儲存到檔案名稱數組中。

inout.c

/*** Function to get the next character from the input file*/static int fileinput (void){ int c = fgetc (fp); return (c == EOF ? 0 : c);}

從檔案中讀出一個字元。如果已經到檔案結束,返回 0 。
到這裡,詞法分析的輸入已經準備好了。

通過分析代碼我們可以看到,不管是 lua_dostring 或者是 lua_dofile,都調用了 lua_parse。

Lua1.1 輸入準備

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.