锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品综合,午夜国产精品视频,午夜欧美大尺度福利影院在线看http://www.shnenglu.com/swo2006/zh-cnSun, 30 Nov 2025 14:02:03 GMTSun, 30 Nov 2025 14:02:03 GMT60SDL: 浣?Linux 鍙樺緱鏈夎叮 (zz)http://www.shnenglu.com/swo2006/archive/2007/03/23/20467.htmlswoswoFri, 23 Mar 2007 12:14:00 GMThttp://www.shnenglu.com/swo2006/archive/2007/03/23/20467.htmlhttp://www.shnenglu.com/swo2006/comments/20467.htmlhttp://www.shnenglu.com/swo2006/archive/2007/03/23/20467.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/20467.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/20467.html

鏈樉紺洪渶瑕?JavaScript 鐨勬枃妗i夐」





Sam Lantinga, 棣栧腑紼嬪簭鍛? Loki Entertainment Software

1999 騫? 9 鏈? 01 鏃?/p>

Sam Lantinga 鏄?Simple DirectMedia Layer (SDL) 搴撶殑浣滆呭拰 Loki Entertainment 鐨勯甯紑鍙戜漢鍛橈紝浠栧皢鍚戞偍浠嬬粛涓縐嶅皢娓告垙縐繪鍒?Linux涓婄殑浼樼宸ュ叿銆係DL 鏄竴涓法騫沖彴浠g爜縐繪鐨勭悊鎯沖伐鍏鳳紝瀹冩敮鎸佽澶氬鉤鍙幫紝濡侺inux銆丼olaris銆両RIX銆丗reeBSD 鍜?MacOS錛岃繖瀵逛簬閭d簺璁や負鍙互鍦? Linux涓婂紑鍙戝晢涓氳蔣浠剁殑 Linux 寮鍙戣呮潵璇存槸涓澶ц繘姝ャ備粬鍚戠ぞ鍖虹殑鍓嶈緢涔嬩竴璁ㄦ暀SDL 濡備綍浣?Linux 鐢ㄦ埛浜彈浠諱綍騫沖彴涓婃渶濂界殑娓告垙錛孲DL濡備綍甯姪寮鍙戣呰窡涓婁笅涓浠h綆楁満娓告垙榪風殑瑕佹眰銆?/blockquote> Sam Lantinga 鏄?Simple DirectMedia Layer (SDL) 搴撶殑浣滆呭拰 Loki Entertainment 鐨勯甯紑鍙戜漢鍛橈紝浠栧皢鍚戞偍浠嬬粛涓縐嶅皢娓告垙縐繪鍒?Linux 涓婄殑浼樼宸ュ叿銆係DL 鏄竴涓法騫沖彴浠g爜縐繪鐨勭悊鎯沖伐鍏鳳紝瀹冩敮鎸佽澶氬鉤鍙幫紝濡? Linux銆丼olaris銆両RIX銆丗reeBSD 鍜?MacOS錛岃繖瀵逛簬閭d簺璁や負鍙互鍦?Linux 涓婂紑鍙戝晢涓氳蔣浠剁殑 Linux 寮鍙戣呮潵璇存槸涓澶ц繘姝ャ備粬鍚戠ぞ鍖虹殑鍓嶈緢涔嬩竴璁ㄦ暀 SDL 濡備綍浣?Linux 鐢ㄦ埛浜彈浠諱綍騫沖彴涓婃渶濂界殑娓告垙錛孲DL 濡備綍甯姪寮鍙戣呰窡涓婁笅涓浠h綆楁満娓告垙榪風殑瑕佹眰銆?/i>

鑷粠 Linus 棣栧厛寮鍙戝嚭 Linux 鏃跺紑濮嬶紝鍒扮幇鍦?Linux 鎴愪負鎵鏈夐粦瀹㈢殑姊︽兂騫朵笖閬嶅強鍏ㄤ笘鐣岋紝Linux 寮鍙戞渶閲嶈鐨勫厓绱犱箣涓灝辨槸 OS 涓婃父鎴忕殑璐ㄩ噺鍜屽彲鐢ㄦс傛父鎴忔槸鎴戜滑鐢ㄦ潵濞變箰鍜屼紤闂茬殑銆傚畠浠彲浠ユ彁楂樺垱閫犲姏騫舵嫇灞曟濊礬銆傛父鎴忚繕鍙互鐢ㄦ潵嫻嬮噺鎿嶄綔緋葷粺鐨勬ц兘銆傜敱浜庢父鎴忚秺鏉ヨ秺澶嶆潅錛屽畠浠揩浣挎瘡 涓瓙緋葷粺閫艱繎鏋侀檺銆傛瘡褰撴垜瑁呴厤涓涓郴緇熸椂錛岄鍏堣鍋氱殑灝辨槸瑁呭叆涓涓父鎴忓茍璇曠帺錛屼互鈥滄祴璇曗濇瘡涓欏圭殑鎬ц兘銆?/a>

Linux 涓婄殑娓告垙宸茬粡瀛樺湪浜嗗緢闀挎椂闂淬備粠鏃╂湡鐨?NetTrek錛屽埌鍙楅珮搴﹁禐鎵殑 DOOM!鍜? 闆風 (Quake)錛屼漢浠凡緇忓彲浠ュ湪 Linux 涓婄帺娓告垙浜嗐備絾闂鏄病鏈夎凍澶熺殑娓告垙銆傛病鏈夊摢瀹跺ぇ鍏徃涓?Linux 鍒涗綔鑳戒駭鐢熻槳鍔ㄦ晥搴旂殑娓告垙銆備絾鏄紝鐢變簬璇ユ搷浣滅郴緇熷彉寰楁棩鐩婃祦琛岋紝榪欑鎯呭喌姝e紑濮嬫敼鍠勩?

Linux 涓婃渶鏃╂湡鐨勬父鎴忎嬌鐢?X11 鍗忚銆備絾鏄浜庢父鎴忔潵璇達紝X11 瀹炲湪澶參浜嗭紝鍥犱負瀹冩槸閽堝鍦ㄧ綉緇滀笂閫忔槑榪愯鐨勫熀浜庤彍鍗曠殑搴旂敤鑰岃璁$殑鍗忚銆備嬌鐢ㄥ畠鐨勬父鎴忛氬父娌℃湁緇氫附鐨勭敾闈紝鑰屼笖榪愯寰楃浉褰撴參銆? DOOM!鏄? 涓涓煎緱娉ㄦ剰鐨勪緥澶栵紝铏界劧瀹冧嬌鐢?X11錛屼絾鏄畠閫氳繃浣跨敤 MIT 鍏變韓鍐呭瓨鎵╁睍鍙互浣垮姩鐢繪洿嫻佺晠錛屽茍鎻愪緵浜嗛肩湡鐨勪笁緇存晥鏋溿傝繕鏈変竴浜涙父鎴忎嬌鐢? SVGA 鍥懼艦搴擄紝SVGAlib銆傛垜鏈鍠滄鐨勪竴涓佹父鎴忔槸閲嶅姏鎴樹簤 (Gravity Wars)錛屽畠瀵瑰叾妯℃嫙鐨勮?Amiga 娓告垙 Gravity Force 鍋氫簡閲嶅ぇ鏀瑰姩銆備絾浣跨敤 SVGAlib 鐨勭▼搴忓彧鑳介傜敤浜庡皯鏁板彈鏀寔鐨勬樉鍗°?

鏃╂湡 X11 娓告垙錛?/b> 浜夐湼 (Craft)鐨勫浘鐗囥? 紲炶瘽 2 (Myth 2)鐨勫浘鐗囷紝Loki 鍑哄搧
鍗曞嚮浠ユ斁澶у浘鐗? src= 鍗曞嚮浠ユ斁澶у浘鐗? src=

浠婂ぉ錛屾父鎴忓紑鍙戣呮湁浜嗘洿澶氱殑閫夋嫨銆備粛鐒跺彲浠ヤ嬌鐢? X 宸ュ叿綆辨垨鍏ㄥ睆 API錛屽 SVGAlib 鎴?fbcon錛屾潵緙栧啓娓告垙錛屼絾浠栦滑鐜板湪榪樻湁璁稿娓告垙搴撳彲浠ヤ嬌鐢ㄣ係imple DirectMedia Layer 搴撴槸 Linux 涓婃渶濂界殑浣庡眰娓告垙寮鍙?API 涔嬩竴銆?/a>

SDL 鏄粈涔堬紵
Simple DirectMedia Layer 搴擄紝綆縐? SDL錛屾槸涓烘暟涓嶅鐨勫晢涓氭父鎴忓紑鍙戝叕鍙鎬嬌鐢ㄧ殑鍏嶈垂杞歡搴撲箣涓銆傚畠鎻愪緵璺ㄥ鉤鍙扮殑浜岀淮甯х紦鍐插尯鍥懼艦鍜岄煶棰戞湇鍔★紝瀹冩敮鎸?Linux銆乄in32 鍜? BeOS銆備篃涓嶅悓紼嬪害鍦版敮鎸佸叾瀹冨鉤鍙幫紝鍖呮嫭 Solaris銆両RIX銆丗reeBSD 鍜? MacOS銆傞櫎浜嗗ぇ閲忕殑鏈嶅姟錛屽寘鎷嚎紼嬨佺嫭绔嬩簬瀛楄妭瀛樺偍嬈″簭鐨勫畯鍜?CD 闊抽錛孲DL 榪樻彁渚涗簡涓涓畝鍗曠殑 API錛屽畠鍏佽鎮ㄥ敖鍙兘鎺ヨ繎鏈満紜歡銆備嬌鐢?SDL 鏈変笁閲嶄紭鐐癸細紼沖畾銆佺畝鍗曞拰鐏墊椿銆?

  • 紼沖畾銆傚鏋?SDL 涓嶅悜 API 鎻愪緵鍙潬鐨勬敮鎸侊紝閭d箞閭d簺鐖卞ソ鑰呭拰鍟嗕笟鍏徃灝變笉鑳戒嬌鐢ㄥ畠銆傚洜涓轟嬌鐢ㄤ簡 SDL錛屽氨娣誨姞浜嗛敊璇慨姝e茍澧炲己浜嗘ц兘錛屼篃灝卞姞寮轟簡 API 鐨勫己鍋ユс傚氨鍍忓唴鏍稿紑鍙戞槸鍒嗘榪涜鐨勶紝SDL 鐨勫紑鍙戜篃鏄垎姝ヨ繘琛岀殑錛屽叾涓竴閮ㄥ垎鏄彲闈犵ǔ瀹氱殑 API錛屽叾瀹冮儴鍒嗘槸鏂板姛鑳藉拰鏋勬濈殑娌欑銆?
  • 綆鍗?/b>銆係DL 琚璁℃垚涓涓畝鍗曠殑 API錛屼互鏈灝戠殑浠g爜瀹炵幇鎮ㄧ殑鏋勬濄傛瘮濡傦紝鎴戞渶榪戜粠 Linux 婕旂ず緇? Optimum涓Щ妞嶄簡涓浜涙紨紺虹▼搴忥紝鎴戝皢瀹冧滑鐨?X11 浠g爜鏇挎崲鎴?SDL 浠g爜錛堣鍙傝涓嬮潰鐨勫垪琛級銆傛偍鍙互鐪嬪埌錛孲DL 浠g爜闈炲父鏄撲簬緙栧啓鍜岀悊瑙c?

    X11 浠g爜

    int init_x (int X, int Y,
    int W, int H, int bpp,
    const char *Name) {
    XPixmapFormatValues *formatList;
    int formatCount;
    int i;
    int formatOk;
    int scanlineLength;
    XGCValues gcVal;
    unsigned long gcMask;
    dis = XOpenDisplay ( NULL );
    if ( dis == NULL) {
    fprintf ( stderr , "Error :\n" );
    fprintf ( stderr , " Cannot connect to Display.\n");
    exit (1);
    }
    screen = DefaultScreen ( dis );
    depth = DefaultDepth ( dis , screen );
    width = DisplayWidth ( dis , screen );
    height = DisplayHeight ( dis , screen );

    winRoot = DefaultRootWindow ( dis );
    winAttr.border_pixel = BlackPixel ( dis , screen );
    winAttr.background_pixel = BlackPixel ( dis , screen );
    winMask = CWBackPixel | CWBorderPixel;

    formatList = XListPixmapFormats( dis, &formatCount);
    if (formatList == NULL){
    fprintf ( stderr , " Cannot get pixmap list\n");
    exit (1);
    }
    formatOk=-1;
    for (i=0; ibytes_per_line*xim->height,
    IPC_CREAT|0777);
    xim->data = SHMInfo.shmaddr = (char *)shmat(SHMInfo.shmid, 0, 0);
    SHMInfo.readOnly = False;
    XShmAttach(dis, &SHMInfo);
    XSync(dis, False);
    buffer=(unsigned char *)xim->data;
    #else
    buffer = (unsigned char *)calloc(W*H, pixmapFormat.bits_per_pixel/8);
    xim = XCreateImage ( dis , CopyFromParent , depth , ZPixmap , 0 ,
    (char *) buffer , W , H ,
    pixmapFormat.scanline_pad, scanlineLength);
    if (xim == NULL){
    fprintf(stderr, " Couldnt create Ximage..\n");
    exit(-1);
    }
    #endif
    gcVal.foreground = 0;
    gcVal.background = 0;
    gcMask = GCForeground | GCBackground;
    gc = XCreateGC ( dis , win , gcMask , &gcVal );
    if (depth==24)
    depth = pixmapFormat.bits_per_pixel;
    return (depth);
    }

    SDL 浠g爜

    int init_x (int X, int Y,
    int W, int H, int bpp,
    const char *Name) {
    int i;
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
    fprintf ( stderr , "Erreur :\n" );
    fprintf ( stderr , " Impossible de se connecter au Display\n");
    exit (1);
    }
    screen = SDL_SetVideoMode(W, H, bpp, SDL_SWSURFACE|SDL_HWPALETTE);
    if ( screen == NULL )
    {
    fprintf ( stderr , "Erreur :\n" );
    fprintf ( stderr , " Impossible de se connecter au Display\n");
    exit (1);
    }
    SDL_WM_SetCaption ( Name, Name );
    for ( i=SDL_NOEVENT; iformat->BitsPerPixel;
    width = screen->w;
    height = screen->h;
    buffer = (unsigned char *)screen->pixels;
    return (depth);
    }

  • 鐏墊椿銆? 榪斿洖鍒頒笂闈㈢殑 Optimum 婕旂ず浠g爜紺轟緥錛屽彧瑕佺Щ妞嶅埌 SDL錛屽茍紜畾涓浜涙暟鎹亣璁撅紝閭d箞鏍規湰涓嶅繀鏀瑰姩浠g爜錛屾紨紺哄氨鍙互鍦?Win32銆丅eOS 鍜?Linux 鎺у埗鍙頒笂榪愯浜嗐傜伒媧繪х殑鍙︿竴鏂歸潰浣撶幇鍦ㄥ敖綆′唬鐮佸畬鍏ㄦ槸璺ㄥ鉤鍙扮殑錛屼絾涓嶄細鎶婃偍鍜屽簳灞傚疄鐜伴殧寮銆係DL 鎻愪緵浜嗗嚱鏁? SDL_GetWMInfo()錛岃鍑芥暟鍙互璁╂偍璁塊棶搴曞眰椹卞姩紼嬪簭鐨勪笓鐢ㄧ獥鍙d俊鎭侺oki Entertainment Software 騫挎硾浣跨敤榪欎竴鎶鏈負瀹冧滑鐨勬父鎴忔櫤鑳界獥鍙g鐞嗗櫒浜や簰銆?

    Optimum 婕旂ず鐗堢殑灞忓箷蹇収

榪欑鍧氬紓愮煶鑸殑紼沖畾銆佺畝鍗曞拰寮哄ぇ鍔熻兘鐨勭粍鍚堝凡緇忕粰 Linux 甯︽潵浜嗕竴浜涙瀬鍏跺紩浜哄叆鑳滅殑娓告垙錛屽寘鎷? Hopkins F.B.I.銆? 鏂囨槑錛氬姏閲忕殑鍛煎敜 (Civilization: Call To Power)銆? 紲炶瘽 2錛歋oulblighter (MythII: Soulblighter)銆? 閾佽礬澶т酣 2 (Railroad Tycoon II)絳夌瓑銆傜紪紼嬬埍濂借呭拰鍟嗕笟鍏徃浣跨敤榪欎釜搴撶殑浜嬪疄琛ㄧず瀹冩鍦ㄦ棩鐩婃彁楂樺叾鍔熻兘鍜岀ǔ瀹氭с傝繖絎﹀悎瀹為檯娓告垙鐨勫疄闄呴渶瑕併?

鏂囨槑錛氬姏閲忕殑鍛煎敜 (Civilization: Call To Power)鐨勫浘鐗? 澶╂棆鍦拌漿 2 (Descent 2)鐨勫浘鐗?
鍗曞嚮浠ユ斁澶у浘鐗? src= 鍗曞嚮浠ユ斁澶у浘鐗? src=



swo 2007-03-23 20:14 鍙戣〃璇勮
]]>
Iterators and the Generic for(LUA)http://www.shnenglu.com/swo2006/archive/2006/11/28/15725.htmlswoswoTue, 28 Nov 2006 04:59:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/28/15725.htmlhttp://www.shnenglu.com/swo2006/comments/15725.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/28/15725.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/15725.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/15725.html鍦ㄨ繖涓绔犳垜浠璁轟負鑼冩or鍐欒凱浠e櫒,聽鎴戜滑浠庝竴涓畝鍗曠殑榪唬鍣ㄥ紑濮?鐒跺悗鎴戜滑瀛︿範濡備綍閫氳繃鍒╃敤鑼冩or鐨勫己澶т箣澶勫啓鍑烘洿楂樻晥鐨勮凱浠e櫒.
7.1聽榪唬鍣ㄤ笌闂寘
聽榪唬鍣ㄦ槸涓縐嶆敮鎸佹寚閽堢被鍨嬬殑緇撴瀯,瀹冨彲浠ヤ嬌閬嶅巻闆嗗悎鐨勬瘡涓涓厓绱?鍦↙ua涓垜浠父甯鎬嬌鐢ㄥ嚱鏁版潵鎻忚堪榪唬鍣?姣忔璋冪敤璇ュ嚱鏁板氨榪斿洖闆嗗悎鐨勪笅涓涓厓绱?
聽 榪唬鍣ㄩ渶瑕佷繚鐣欎笂涓嬈℃垚鍔熻皟鐢ㄧ殑鐘舵佸拰涓嬩竴嬈℃垚鍔熻皟鐢ㄧ殑鐘舵?涔熷氨鏄粬鐭ラ亾鏉ヨ嚜浜庡摢閲屽拰灝嗚鍓嶅線鍝噷.闂寘鎻愪緵鐨勬満鍒跺彲浠ュ緢瀹規槗瀹炵幇榪欎釜浠誨姟.璁頒綇:闂寘 鏄竴涓唴閮ㄥ嚱鏁?瀹冨彲浠ヨ闂竴涓垨鑰呭涓閮ㄥ嚱鏁扮殑澶栭儴灞閮ㄥ彉閲?姣忔闂寘鐨勬垚鍔熻皟鐢ㄥ悗榪欎簺澶栭儴灞閮ㄥ彉閲忛兘淇濆瓨浠栦滑鐨勫?鐘舵?.褰撶劧濡傛灉瑕佸垱寤轟竴涓棴鍖? 蹇呴』瑕佸垱寤哄叾澶栭儴灞閮ㄥ彉閲?鎵浠ヤ竴涓吀鍨嬬殑闂寘鐨勭粨鏋勫寘鍚袱涓嚱鏁?涓涓槸闂寘鑷繁;鍙︿竴涓槸宸ュ巶(鍒涘緩闂寘鐨勫嚱鏁?.
聽涓句竴涓畝鍗曠殑渚嬪瓙,鎴戜滑涓轟竴涓猯ist鍐欎竴涓畝鍗曠殑榪唬鍣?涓巌pairs()涓嶅悓鐨勬槸鎴戜滑瀹炵幇鐨勮繖涓凱浠e櫒榪斿洖鍏冪礌鐨勫艱屼笉鏄儲寮曚笅鏍?
聽聽function list_iter (t)
聽聽聽 聽聽 local i = 0
聽聽聽 聽聽 local n = table.getn(t)
聽聽聽 聽聽 return function ()
聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 i = i + 1
聽聽聽 聽聽聽聽聽聽聽聽聽聽聽 if i <= n then return t[i] end
聽聽聽 聽聽聽聽聽聽聽聽聽 end
聽聽聽 聽end
聽榪欎釜渚嬪瓙涓璴ist_iter 鏄竴涓伐鍘?姣忔璋冪敤浠栭兘浼氬垱寤轟竴涓柊鐨勯棴鍖?榪唬鍣ㄦ湰韜?.闂寘鍖呮潙鍐呴儴灞閮ㄥ彉閲?t,i,n),鍥犳姣忔璋冪敤浠栬繑鍥瀕ist涓殑涓嬩竴涓厓绱犲?褰搇ist涓病鏈夊兼椂,榪斿洖nil.鎴戜滑鍙互鍦╳hile璇彞涓嬌鐢ㄨ繖涓凱浠e櫒:
聽聽t = {10, 20, 30}
聽聽聽 聽iter = list_iter(t)聽聽聽 -- creates the iterator
聽聽聽 聽while true do
聽聽聽 聽聽 local element = iter()聽聽 -- calls the iterator
聽聽聽 聽聽 if element == nil then break end
聽聽聽 聽聽 print(element)
聽聽聽 聽end
聽鎴戜滑璁捐鐨勮繖涓凱浠e櫒涔熷緢瀹規槗鐢ㄤ簬鑼冩or璇彞
聽聽t = {10, 20, 30}
聽聽聽 聽for element in list_iter(t) do
聽聽聽 聽聽 print(element)
聽聽聽 聽end
聽鑼冩or涓鴻凱浠e驚鐜鐞嗘墍鏈夌殑钖勮(bookkeeping):棣栧厛璋冪敤榪唬宸ュ巶;鍐呴儴淇濈暀榪唬鍑芥暟,鍥犳鎴戜滑涓嶉渶瑕乮ter鍙橀噺;鐒跺悗鍦ㄦ瘡涓涓柊鐨勮凱浠e璋冪敤榪唬鍣ㄥ嚱鏁?褰撹凱浠e櫒榪斿洖nil鏃跺驚鐜粨鏉?鍚庨潰鎴戜滑灝嗙湅鍒拌寖鎬or鑳借儨浠繪洿澶氱殑浠誨姟).
聽涓嬮潰鐪嬩竴涓◢寰珮綰т竴鐐圭殑渚嬪瓙:鎴戜滑鍐欎竴涓凱浠e櫒閬嶅巻涓涓枃浠跺唴鐨勬墍鏈夊尮閰嶇殑鍗曡瘝.涓轟簡瀹炵幇鐩殑,鎴戜滑闇瑕佷繚鐣欎袱涓?褰撳墠琛屽拰鍦ㄥ綋鍓嶈鐨勫亸縐婚噺,鎴戜滑浣跨敤涓や釜澶栭儴灞閮ㄥ彉閲弆ine,pos淇濆瓨榪欎袱涓?
聽聽function allwords ()
聽聽聽 聽聽 local line = io.read()聽 -- current line
聽聽聽 聽聽 local pos = 1聽聽聽聽聽聽聽聽聽聽 -- current position in the line
聽聽聽 聽聽 return function ()聽聽聽聽聽 -- iterator function
聽聽聽 聽聽聽聽 while line do聽聽聽聽聽聽聽聽 -- repeat while there are lines
聽聽聽 聽聽聽聽聽聽 local s, e = string.find(line, "%w+", pos)
聽聽聽 聽聽聽聽聽聽 if s then聽聽聽聽聽聽聽聽聽聽 -- found a word?
聽聽聽 聽聽聽聽聽聽聽聽 pos = e + 1聽聽聽聽聽聽 -- next position is after this word
聽聽聽 聽聽聽聽聽聽聽聽 return string.sub(line, s, e)聽聽聽聽 -- return the word
聽聽聽 聽聽聽聽聽聽 else
聽聽聽 聽聽聽聽聽聽聽聽 line = io.read()聽 -- word not found; try next line
聽聽聽 聽聽聽聽聽聽聽聽 pos = 1聽聽聽聽聽聽聽聽聽聽 -- restart from first position
聽聽聽 聽聽聽聽聽聽 end
聽聽聽 聽聽聽聽 end
聽聽聽 聽聽聽聽 return nil聽聽聽聽聽聽聽聽聽聽聽 -- no more lines: end of traversal
聽聽聽 聽聽 end
聽聽聽 聽end
聽 榪唬鍑芥暟鐨勪富浣撻儴鍒嗚皟鐢ㄤ簡string.find鍑芥暟,string.find鍦ㄥ綋鍓嶈浠庡綋鍓嶄綅緗紑濮嬫煡鎵懼尮閰嶇殑鍗曡瘝,渚嬪瓙涓尮閰嶇殑鍗曡瘝浣跨敤妯″紡'%w+ '鎻忚堪鐨?濡傛灉鏌ユ壘鍒頒竴涓崟璇?榪唬鍑芥暟鏇存柊褰撳墠浣嶇疆pos涓哄崟璇嶅悗鐨勭涓涓綅緗?騫朵笖榪斿洖榪欎釜鍗曡瘝(string.sub鍑芥暟浠巐ine涓彁鍙栦袱涓綅緗? 鍙傛暟涔嬮棿鐨勫瓙涓?.鍚﹀垯榪唬鍑芥暟璇誨彇鏂扮殑涓琛屽茍閲嶆柊鎼滅儲.濡傛灉娌℃湁line鍙榪斿洖nil緇撴潫.
聽灝界榪唬鍑芥暟鏈変簺澶嶆潅,浣嗕嬌鐢ㄨ搗鏉ユ槸寰堢洿瑙傜殑:聽
聽聽for word in allwords() do
聽聽聽聽聽 聽聽print(word)
聽聽聽 聽end
聽聽聽 閫氬父鎯呭喌涓?榪唬鍑芥暟閮介毦鍐欐槗鐢?榪欎笉鏄竴涓ぇ闂:涓鑸琇ua緙栫▼涓嶉渶瑕佽嚜宸卞畾涔夎凱浠e嚱鏁?鑰屾槸浣跨敤璇█鎻愪緵鐨?闄ら潪紜疄闇瑕佽嚜宸卞畾涔?
7.2聽鑼冩or鐨勮涔壜?
聽 鍓嶉潰鎴戜滑鐪嬪埌鐨勮凱浠e櫒鏈変竴涓己鐐?姣忔璋冪敤閮介渶瑕佸垱寤轟竴涓棴鍖?澶у鏁版儏鍐典笅榪欑鍋氭硶閮芥病浠涔堥棶棰?渚嬪鍦╝llwords榪唬鍣ㄤ腑鍒涘緩涓涓棴鍖呯殑浠d環姣? 璧瘋鏁翠釜鏂囦歡鏉ヨ寰笉瓚抽亾,鐒跺悗鍦ㄦ湁浜涙儏鍐典笅鍒涘緩闂寘鐨勪唬浠鋒槸涓嶈兘蹇嶅彈鐨?鍦ㄨ繖浜涙儏鍐典笅鎴戜滑鍙互浣跨敤鑼冩or鏈韓鏉ヤ繚瀛樿凱浠g殑鐘舵?
聽鍓嶉潰鎴戜滑鐪嬪埌鍦ㄥ驚鐜繃紼嬩腑鑼冩or鍦ㄨ嚜宸卞唴閮ㄤ繚瀛樿凱浠e嚱鏁?瀹為檯涓婂畠淇濆瓨涓変釜鍊?榪唬鍑芥暟,鐘舵佸父閲忓拰鎺у埗鍙橀噺.涓嬮潰璇︾粏璇存槑.
聽鑼冩or鐨勬枃娉曞涓?
聽聽for <var-list> in <exp-list> do
聽聽聽 聽聽 <body>
聽聽聽 聽end
聽<var-list>鏄竴涓垨澶氫釜浠ラ楀彿鍒嗗壊鍙橀噺鍚嶇殑鍒楄〃,<exp-list>鏄竴涓垨澶氫釜浠ラ楀彿鍒嗗壊鐨勮〃杈懼紡鍒楄〃,閫氬父鎯呭喌涓媏xp-list鍙湁涓涓?榪唬宸ュ巶鐨勮皟鐢?
聽聽for k, v in pairs(t) do
聽聽聽聽聽聽 print(k, v)聽聽聽聽聽
聽聽聽聽聽聽聽 end聽聽聽聽聽聽聽
聽聽聽 鍙橀噺鍒楄〃k,v;琛ㄨ揪寮忓垪琛╬air(t),鍦ㄥ緢澶氭儏鍐典笅鍙橀噺鍒楄〃涔熷彧鏈変竴涓彉閲?姣斿:
聽聽聽 聽for line in io.lines() do聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 聽io.write(line, '\n')
聽聽聽聽聽聽聽 end聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽 鎴戜滑縐板彉閲忓垪琛ㄤ腑絎竴涓彉閲忎負鎺у埗鍙橀噺,鍏跺間負nil鏃跺驚鐜粨鏉?
聽聽聽 涓嬮潰鎴戜滑鐪嬬湅鑼冩or鐨勬墽琛岃繃紼?
聽聽聽 棣栧厛,鍒濆鍖?璁$畻in鍚庨潰琛ㄨ揪寮忕殑鍊?琛ㄨ揪寮忓簲璇ヨ繑鍥炶寖鎬or闇瑕佺殑涓変釜鍊?榪唬鍑芥暟,鐘舵佸父閲忓拰鎺у埗鍙橀噺;涓庡鍊艱祴鍊間竴鏍?濡傛灉琛ㄨ揪寮忚繑鍥炵殑緇撴灉涓暟涓嶈凍涓変釜浼氳嚜鍔ㄧ敤nil琛ヨ凍錛屽鍑洪儴鍒嗕細琚拷鐣?
聽聽聽 絎簩,灝嗙姸鎬佸父閲忓拰鎺у埗鍙橀噺浣滀負鍙傛暟璋冪敤榪唬鍑芥暟(娉ㄦ剰:瀵逛簬for緇撴瀯鏉ヨ,鐘舵佸父閲忔病鏈夌敤澶?浠呬粎鍦ㄥ垵濮嬪寲鏃惰幏鍙栦粬鐨勫煎茍浼犻掔粰榪唬鍑芥暟).
聽聽聽 絎笁,灝嗚凱浠e嚱鏁拌繑鍥炵殑鍊艱祴緇欏彉閲忓垪琛?
聽聽聽 絎洓,濡傛灉榪斿洖鐨勭涓涓間負nil寰幆緇撴潫,鍚﹀垯鎵ц寰幆浣?
聽聽聽 絎簲,鍥炲埌絎簩姝ュ啀嬈¤皟鐢ㄨ凱浠e嚱鏁?
聽聽聽 鏇寸簿紜殑鏉ヨ:
聽聽聽 聽for var_1, ..., var_n in explist do block end
聽絳変環浜?br />聽聽do
聽聽聽 聽聽 local _f, _s, _var = explist
聽聽聽 聽聽 while true do
聽聽聽 聽聽聽聽 local var_1, ... , var_n = _f(_s, _var)
聽聽聽 聽聽聽聽 _var = var_1
聽聽聽 聽聽聽聽 if _var == nil then break end
聽聽聽 聽聽聽聽 block
聽聽聽 聽聽 end
聽聽聽 聽end
聽濡傛灉鎴戜滑鐨勮凱浠e嚱鏁版槸f錛岀姸鎬佸父閲忔槸s,鎺у埗鍙橀噺鐨勫垵濮嬪兼槸a0,閭d箞鎺у埗鍙橀噺灝嗗驚鐜?a1=f(s,a0);a2=f(s,a1);...鐩村埌ai=nil
7.3聽鏃犵姸鎬佺殑榪唬鍣?br />聽 聽鏃犵姸鎬佺殑榪唬鍣ㄦ槸鎸囦笉淇濈暀浠諱綍鐘舵佺殑榪唬鍣?鍥犳鍦ㄥ驚鐜腑鎴戜滑鍙互鍒╃敤鏃犵姸鎬佽凱浠e櫒閬垮厤鍒涘緩闂寘鑺辮垂棰濆鐨勪唬浠?
聽 聽姣忎竴嬈¤凱浠?榪唬鍑芥暟閮芥槸鐢ㄤ袱涓彉閲?鐘舵佸父閲忓拰鎺у埗鍙橀噺)鐨勫間綔涓哄弬鏁拌璋冪敤,涓涓棤鐘舵佺殑榪唬鍣ㄥ彧鍒╃敤榪欎袱涓煎彲浠ヨ幏鍙栦笅涓涓厓绱?榪欑鏃犵姸鎬佽凱浠e櫒鐨勫吀鍨嬬殑綆鍗曠殑渚嬪瓙鏄痠pairs,浠栭亶鍘嗘暟緇勭殑姣忎竴涓厓绱?
聽 聽聽a = {"one", "two", "three"}
聽聽聽 聽for i, v in ipairs(a) do
聽聽聽 聽聽 print(i, v)
聽聽聽 聽end
聽榪唬鐨勭姸鎬佸寘鎷閬嶅巻鐨勮〃(寰幆榪囩▼涓笉浼氭敼鍙樼殑鐘舵佸父閲?鍜屽綋鍓嶇殑绱㈠紩涓嬫爣(鎺у埗鍙橀噺),ipairs鍜岃凱浠e嚱鏁伴兘寰堢畝鍗?鎴戜滑鍦↙ua涓彲浠ヨ繖鏍峰疄鐜?
聽聽function iter (a, i)
聽聽聽 聽聽 i = i + 1
聽聽聽 聽聽 local v = a[i]
聽聽聽 聽聽 if v then
聽聽聽 聽聽聽聽 return i, v
聽聽聽 聽聽 end
聽聽聽 聽end
聽聽聽 聽
聽聽聽 聽function ipairs (a)
聽聽聽 聽聽 return iter, a, 0
聽聽聽 聽end
聽 褰揕ua璋冪敤ipairs(a)寮濮嬪驚鐜椂,浠栬幏鍙栦笁涓?榪唬鍑芥暟iter,鐘舵佸父閲廰鍜屾帶鍒跺彉閲忓垵濮嬪?;鐒跺悗Lua璋冪敤iter(a,0)榪斿洖1, a[1](闄ら潪a[1]=nil);絎簩嬈¤凱浠h皟鐢╥ter(a,1)榪斿洖2,a[2]...鐩村埌絎竴涓潪nil鍏冪礌.
聽Lua搴撲腑瀹炵幇鐨刾airs鏄竴涓敤next瀹炵幇鐨勫師濮嬫柟娉?
聽聽function pairs (t)
聽聽聽 聽聽 return next, t, nil
聽聽聽 聽end
聽榪樺彲浠ヤ笉浣跨敤ipairs鐩存帴浣跨敤next
聽聽for k, v in next, t do
聽聽聽 聽聽 ...
聽聽聽 聽end
聽璁頒綇:exp-list榪斿洖緇撴灉浼氳璋冩暣涓轟笁涓?鎵浠ua鑾峰彇next,t,nil;紜垏鍦拌褰撲粬璋冪敤pairs鏃惰幏鍙?
7.4聽澶氱姸鎬佺殑榪唬鍣?br />聽 寰堝鎯呭喌涓?榪唬鍣ㄩ渶瑕佷繚瀛樺涓姸鎬佷俊鎭屼笉鏄畝鍗曠殑鐘舵佸父閲忓拰鎺у埗鍙橀噺,鏈綆鍗曠殑鏂規硶鏄嬌鐢ㄩ棴鍖?榪樻湁涓縐嶆柟娉曞氨鏄皢鎵鏈夌殑鐘舵佷俊鎭皝瑁呭埌table 鍐?灝唗able浣滀負榪唬鍣ㄧ殑鐘舵佸父閲?鍥犱負榪欑鎯呭喌涓嬪彲浠ュ皢鎵鏈夌殑淇℃伅瀛樻斁鍦╰able鍐?鎵浠ヨ凱浠e嚱鏁伴氬父涓嶉渶瑕佺浜屼釜鍙傛暟.
聽涓嬮潰鎴戜滑閲嶅啓allwords榪唬鍣?榪欎竴嬈℃垜浠笉鏄嬌鐢ㄩ棴鍖呰屾槸浣跨敤甯︽湁涓や釜鍩?line,pos)鐨則able.
聽寮濮嬭凱浠g殑鍑芥暟鏄緢綆鍗曠殑,浠栧繀欏昏繑鍥炶凱浠e嚱鏁板拰鍒濆鐘舵?
聽聽local iterator聽聽 -- to be defined later
聽聽聽 聽
聽聽聽 聽function allwords ()
聽聽聽 聽聽 local state = {line = io.read(), pos = 1}
聽聽聽 聽聽 return iterator, state
聽聽聽 聽end 聽鐪熸鐨勫鐞嗗伐浣滄槸鍦ㄨ凱浠e嚱鏁板唴瀹屾垚:
聽聽function iterator (state)
聽聽聽 聽聽 while state.line do聽聽聽聽聽聽聽 -- repeat while there are lines
聽聽聽 聽聽聽聽 -- search for next word
聽聽聽 聽聽聽聽 local s, e = string.find(state.line, "%w+", state.pos)
聽聽聽 聽聽聽聽 if s then聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 -- found a word?
聽聽聽 聽聽聽聽聽聽 -- update next position (after this word)
聽聽聽 聽聽聽聽聽聽 state.pos = e + 1
聽聽聽 聽聽聽聽聽聽 return string.sub(state.line, s, e)
聽聽聽 聽聽聽聽 else聽聽聽 -- word not found
聽聽聽 聽聽聽聽聽聽 state.line = io.read() -- try next line...
聽聽聽 聽聽聽聽聽聽 state.pos = 1聽聽聽聽聽聽聽聽聽 -- ... from first position
聽聽聽 聽聽聽聽 end
聽聽聽 聽聽 end
聽聽聽 聽聽 return nil聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 -- no more lines: end loop
聽聽聽 聽end
聽 鎴戜滑搴旇灝藉彲鑳界殑鍐欐棤鐘舵佺殑榪唬鍣?鍥犱負榪欐牱寰幆鐨勬椂鍊欑敱for鏉ヤ繚瀛樼姸鎬?涓嶉渶瑕佸垱寤哄璞¤姳璐圭殑浠d環灝?濡傛灉涓嶈兘鐢ㄦ棤鐘舵佺殑榪唬鍣ㄥ疄鐜?搴斿敖鍙兘浣跨敤闂? 鍖?灝藉彲鑳戒笉瑕佷嬌鐢╰able榪欑鏂瑰紡,鍥犱負鍒涘緩闂寘鐨勪唬浠瘋姣斿垱寤簍able灝?鍙﹀Lua澶勭悊闂寘瑕佹瘮澶勭悊table閫熷害蹇簺.鍚庨潰鎴戜滑榪樺皢鐪嬪埌鍙︿竴 縐嶄嬌鐢ㄥ崗鍚屾潵鍒涘緩榪唬鍣ㄧ殑鏂瑰紡,榪欑鏂瑰紡鍔熻兘鏇村己浣嗘洿澶嶆潅.
7.4聽鐪熸鐨勮凱浠e櫒
聽榪唬鍣ㄧ殑鍚嶅瓧鏈変竴浜涜瀵?鍥犱負瀹冨茍娌℃湁榪唬,瀹屾垚榪唬鍔熻兘鐨勬槸for璇彞,涔熻鏇村ソ鐨勫彨娉曞簲璇ユ槸'鐢熸垚鍣?;浣嗘槸鍦ㄥ叾浠栬璦姣斿java,C++榪唬鍣ㄧ殑璇存硶宸茬粡寰堟櫘閬嶄簡,鎴戜滑涔熷皢娌跨敤榪欑鏈.
聽鏈変竴縐嶆柟寮忓垱寤轟竴涓湪鍐呴儴瀹屾垚榪唬鐨勮凱浠e櫒.榪欐牱褰撴垜浠嬌鐢ㄨ凱浠e櫒鐨勬椂鍊欏氨涓嶉渶瑕佷嬌鐢ㄥ驚鐜簡;鎴戜滑浠呬粎浣跨敤姣忎竴嬈¤凱浠i渶瑕佸鐞嗙殑浠誨姟浣滀負鍙傛暟璋冪敤榪唬鍣ㄥ嵆鍙?鍏蜂綋鍦拌,榪唬鍣ㄦ帴鍙椾竴涓嚱鏁頒綔涓哄弬鏁?騫朵笖榪欎釜鍑芥暟鍦ㄨ凱浠e櫒鍐呴儴琚皟鐢?
聽浣滀負涓涓叿浣撶殑渚嬪瓙,鎴戜滑浣跨敤涓婅堪鏂瑰紡閲嶅啓allwords榪唬鍣?
聽聽function allwords (f)
聽聽聽 聽聽 -- repeat for each line in the file
聽聽聽 聽聽 for l in io.lines() do
聽聽聽 聽聽聽聽 -- repeat for each word in the line
聽聽聽 聽聽聽聽 for w in string.gfind(l, "%w+") do
聽聽聽 聽聽聽聽聽聽 -- call the function
聽聽聽 聽聽聽聽聽聽 f(w)
聽聽聽 聽聽聽聽 end
聽聽聽 聽聽 end
聽聽聽 聽end
聽濡傛灉鎴戜滑鎯寵鎵撳嵃鍑哄崟璇?鍙渶瑕?br />聽聽allwords(print)
聽鏇翠竴鑸殑鍋氭硶鏄垜浠嬌鐢ㄥ尶鍚嶅嚱鏁頒綔涓轟綔涓哄弬鏁?涓嬮潰鐨勪緥瀛愭墦鍗板嚭鍗曡瘝'hello'鍑虹幇鐨勬鏁?
聽聽local count = 0
聽聽聽 聽allwords(function (w)
聽聽聽 聽聽 if w == "hello" then count = count + 1 end
聽聽聽 聽end)
聽聽聽 聽print(count)
聽鐢╢or緇撴瀯瀹屾垚鍚屾牱鐨勪換鍔?
聽聽local count = 0
聽聽聽 聽for w in allwords() do
聽聽聽 聽聽 if w == "hello" then count = count + 1 end
聽聽聽 聽end
聽聽聽 聽print(count)
聽鐪熸鐨勮凱浠e櫒椋庢牸鐨勫啓娉曞湪Lua鑰佺増鏈腑寰堟祦琛?閭f椂榪樻病鏈塮or寰幆.
聽涓ょ椋庢牸鐨勫啓娉曠浉宸笉澶?浣嗕篃鏈夊尯鍒?涓鏂歸潰,絎簩縐嶉鏍兼洿瀹規槗涔﹀啓鍜岀悊瑙?鍙︿竴鏂歸潰,for緇撴瀯鏇寸伒媧?鍙互浣跨敤break鍜宑ontinue璇彞 ;鍦ㄧ湡姝g殑榪唬鍣ㄩ鏍煎啓娉曚腑return璇彞鍙槸浠庡尶鍚嶅嚱鏁頒腑榪斿洖鑰屼笉鏄鍑哄驚鐜?

swo 2006-11-28 12:59 鍙戣〃璇勮
]]>
铏氭嫙涓栫晫http://www.shnenglu.com/swo2006/archive/2006/11/06/14735.htmlswoswoMon, 06 Nov 2006 09:27:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/06/14735.htmlhttp://www.shnenglu.com/swo2006/comments/14735.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/06/14735.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14735.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14735.html

swo 2006-11-06 17:27 鍙戣〃璇勮
]]>
lua 緗戠珯http://www.shnenglu.com/swo2006/archive/2006/11/03/14619.htmlswoswoFri, 03 Nov 2006 05:54:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/03/14619.htmlhttp://www.shnenglu.com/swo2006/comments/14619.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/03/14619.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14619.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14619.html

swo 2006-11-03 13:54 鍙戣〃璇勮
]]>
鍏充簬sdl_gfx http://www.shnenglu.com/swo2006/archive/2006/11/01/14533.htmlswoswoWed, 01 Nov 2006 12:09:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14533.htmlhttp://www.shnenglu.com/swo2006/comments/14533.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14533.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14533.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14533.htmlwww.devmaster.net



swo 2006-11-01 20:09 鍙戣〃璇勮
]]>
Lua鐨勫浠誨姟鏈哄埗鈥斺斿崗紼?coroutine) [杞澆]http://www.shnenglu.com/swo2006/archive/2006/11/01/14530.htmlswoswoWed, 01 Nov 2006 10:46:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14530.htmlhttp://www.shnenglu.com/swo2006/comments/14530.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14530.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14530.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14530.html Lua鐨勫浠誨姟鏈哄埗鈥斺斿崗紼?coroutine)




銆 銆騫跺彂鏄幇瀹炰笘鐣岀殑鏈川鐗瑰緛錛岃岃仾鏄庣殑璁$畻鏈虹瀛﹀鐢ㄦ潵妯℃嫙騫跺彂鐨勬妧鏈墜孌典究鏄浠誨姟鏈哄埗銆傚ぇ鑷翠笂鏈夎繖涔堜袱縐嶅浠誨姟鎶鏈紝涓縐嶆槸鎶㈠崰寮忓浠誨姟 (preemptive multitasking)錛屽畠璁╂搷浣滅郴緇熸潵鍐沖畾浣曟椂鎵ц鍝釜浠誨姟銆傚彟澶栦竴縐嶅氨鏄崗浣滃紡澶氫換鍔?cooperative multitasking)錛屽畠鎶婂喅瀹氭潈浜ょ粰浠誨姟錛岃瀹冧滑鍦ㄨ嚜宸辮涓哄悎閫傜殑鏃跺欒嚜鎰挎斁寮冩墽琛屻傝繖涓ょ澶氫換鍔℃柟寮忓悇鏈変紭緙虹偣錛屽墠鑰呭浐鏈夌殑鍚屾闂浣垮緱紼嬪簭緇? 甯告湁涓嶅彲棰勭煡鐨勮涓猴紝鑰屽悗鑰呭垯瑕佹眰浠誨姟鍏峰鐩稿綋鐨勮嚜寰嬬簿紲炪?br />銆銆銆銆鍗忕▼(coroutine)鎶鏈槸涓縐嶇▼搴忔帶鍒舵満鍒訛紝鏃╁湪涓婁笘綰?0騫翠唬灝卞凡 鎻愬嚭錛岀敤瀹冨彲浠ュ緢鏂逛究鍦板疄鐜板崗浣滃紡澶氫換鍔°傚湪涓繪祦鐨勭▼搴忚璦(濡侰++銆丣ava銆丳ascal絳?閲屾垜浠緢灝戣兘鐪嬪埌鍗忕▼鐨勮韓褰憋紝浣嗘槸鐜板湪涓嶅皯鍔ㄦ佽剼鏈 璦(Python銆丳erl)鍗撮兘鎻愪緵浜嗗崗紼嬫垨涓庝箣鐩鎬技鐨勬満鍒訛紝鍏朵腑鏈紿佸嚭鐨勪究鏄疞ua銆?br />銆銆
銆銆銆銆Lua璇█瀹炵幇鐨勫崗紼嬫槸涓縐嶉潪瀵圭О 寮?asymmetric)鍗忕▼錛屾垨縐板崐瀵圭О寮?semi-asymmetric)鍗忕▼錛屽張鎴栧共鑴嗗氨鍙崐鍗忕▼(semi-coroutine)銆傝繖縐嶅崗紼? 鏈哄埗涔嬫墍浠ヨ縐頒負闈炲縐扮殑錛屾槸鍥犱負瀹冩彁渚涗簡涓ょ浼犻掔▼搴忔帶鍒舵潈鐨勬搷浣滐細涓縐嶆槸(閲?璋冪敤鍗忕▼(閫氳繃coroutine.resume)錛涘彟涓縐嶆槸鎸傝搗鍗忕▼ 騫跺皢紼嬪簭鎺у埗鏉冭繑鍥炵粰鍗忕▼鐨勮皟鐢ㄨ?閫氳繃coroutine.yield)銆備竴涓潪瀵圭О鍗忕▼鍙互鐪嬪仛鏄粠灞炰簬瀹冪殑璋冪敤鑰呯殑錛屼簩鑰呯殑鍏崇郴闈炲父綾諱技浜庝緥紼? (routine)涓庡叾璋冪敤鑰呬箣闂寸殑鍏崇郴銆傛棦鐒舵湁闈炲縐板紡鍗忕▼錛屽綋鐒朵篃灝辨湁瀵圭О寮?symmetric)鍗忕▼浜嗭紝瀹冪殑鐗圭偣鏄彧鏈変竴縐嶄紶閫掔▼搴忔帶鍒舵潈鐨勬搷 浣滐紝鍗沖皢鎺у埗鏉冪洿鎺ヤ紶閫掔粰鎸囧畾鐨勫崗紼嬨傛浘緇忔湁榪欎箞涓縐嶈娉曪紝瀵圭О寮忓拰闈炲縐板紡鍗忕▼鏈哄埗鐨勮兘鍔涘茍涓嶇瓑浠鳳紝浣嗕簨瀹炰笂寰堝鏄撴牴鎹墠鑰呮潵瀹炵幇鍚庤呫傛帴涓嬫潵鎴戜滑灝辯敤 浠g爜鏉ヨ瘉鏄庤繖涓簨瀹炪?br />銆銆
銆銆--瀵圭О寮忓崗紼嬪簱coro.lua
銆銆
銆銆coro = {}
銆銆--coro.main鐢ㄦ潵鏍囪瘑紼嬪簭鐨勪富鍑芥暟
銆銆coro.main = function() end
銆銆-- coro.current鍙橀噺鐢ㄦ潵鏍囪瘑鎷ユ湁鎺у埗鏉冪殑鍗忕▼錛?br />銆銆-- 涔熷嵆姝e湪榪愯鐨勫綋鍓嶅崗紼?br />銆銆coro.current = coro.main
銆銆
銆銆-- 鍒涘緩涓涓柊鐨勫崗紼?br />銆銆function coro.create(f)
銆銆銆 return coroutine.wrap(function(val)
銆銆銆銆銆銆銆銆銆銆銆銆銆銆銆銆return nil,f(val)
銆銆銆銆銆銆銆銆銆銆銆銆銆銆 end)
銆銆end
銆銆
銆銆-- 鎶婃帶鍒舵潈鍙婃寚瀹氱殑鏁版嵁val浼犵粰鍗忕▼k
銆銆function coro.transfer(k,val)
銆銆銆 if coro.current ~= coro.main then
銆銆銆銆銆return coroutine.yield(k,val)
銆銆銆 else
銆銆銆銆銆-- 鎺у埗鏉冨垎媧懼驚鐜?br />銆銆銆銆銆while k do
銆銆銆銆銆銆 coro.current = k
銆銆銆銆銆銆 if k == coro.main then
銆銆銆銆銆銆銆銆return val
銆銆銆銆銆銆 end
銆銆銆銆銆銆 k,val = k(val)
銆銆銆銆銆end
銆銆銆銆銆error("coroutine ended without transfering control...")
銆銆銆 end
銆銆end
銆銆
銆銆濡傛灉鏆傛椂榪樺紕涓嶆噦涓婇潰鐨勭▼搴忥紝娌″叧緋伙紝鐪嬬湅濡備綍浣跨敤榪欎釜搴撳悗鍐嶅洖澶村垎鏋愩備笅闈㈡槸浣跨敤紺轟緥錛?br />銆銆
銆銆require("coro.lua")
銆銆
銆銆function foo1(n)
銆銆銆 print("1: foo1 received value "..n)
銆銆銆 n = coro.transfer(foo2,n + 10)
銆銆銆 print("2: foo1 received value "..n)
銆銆銆 n = coro.transfer(coro.main,n + 10)
銆銆銆 print("3: foo1 received value "..n)
銆銆銆 coro.transfer(coro.main,n + 10)
銆銆end
銆銆
銆銆function foo2(n)
銆銆銆 print("1: foo2 received value "..n)
銆銆銆 n = coro.transfer(coro.main,n + 10)
銆銆銆 print("2: foo2 received value "..n)
銆銆銆 coro.transfer(foo1,n + 10)
銆銆end
銆銆
銆銆function main()
銆銆銆 foo1 = coro.create(foo1)
銆銆銆 foo2 = coro.create(foo2)
銆銆銆 local n = coro.transfer(foo1,0)
銆銆銆 print("1: main received value "..n)
銆銆銆 n = coro.transfer(foo2,n + 10)
銆銆銆 print("2: main received value "..n)
銆銆銆 n = coro.transfer(foo1,n + 10)
銆銆銆 print("3: main received value "..n)
銆銆end
銆銆
銆銆--鎶妋ain璁句負涓誨嚱鏁?鍗忕▼)
銆銆coro.main = main
銆銆--灝哻oro.main璁句負褰撳墠鍗忕▼
銆銆coro.current = coro.main
銆銆--寮濮嬫墽琛屼富鍑芥暟(鍗忕▼)
銆銆coro.main()
銆銆
銆銆
銆 銆涓婇潰鐨勭ず渚嬪畾涔変簡涓涓悕涓簃ain鐨勪富鍑芥暟錛屾暣涓▼搴忕敱瀹冭屽錛屼篃鍥犲畠鑰岀粓銆備負浠涔堥渶瑕佷竴涓繖鏍風殑涓誨嚱鏁板憿錛熶笂闈㈣浜嗭紝紼嬪簭鎺у埗鏉冨彲浠ュ湪瀵圭О寮忓崗紼嬩箣闂? 鑷敱鍦扮洿鎺ヤ紶閫掞紝瀹冧滑涔嬮棿鏃犳墍璋撹皝浠庡睘浜庤皝鐨勯棶棰橈紝閮藉浜庡悓涓涓眰綰э紝浣嗘槸搴旂敤紼嬪簭蹇呴』鏈変竴涓紑濮嬬偣錛屾墍浠ユ垜浠畾涔変竴涓富鍑芥暟錛岃瀹冪偣鐕冪▼搴忚繍琛岀殑瀵肩伀 綰褲傝櫧璇村悇涓崗紼嬮兘鏄鉤絳夌殑錛屼絾鍋氫負紼嬪簭榪愯鍘熷姩鍔涚殑涓誨嚱鏁頒粛鐒朵韓鏈夌壒孌婄殑鍦頒綅(榪欎釜涓栦笂鍝湁緇濆鐨勫鉤絳夛紒)錛屼負姝ゆ垜浠殑搴撲笓闂ㄧ敤浜嗕竴涓? coro.main鍙橀噺鏉ヤ繚瀛樹富鍑芥暟錛屽茍涓斿湪瀹冩墽琛屼箣鍓嶈灝嗗畠璁句負褰撳墠鍗忕▼(铏界劧涓婇潰鐨刴ain瀹為檯鍙槸涓涓櫘閫氬嚱鏁拌岄潪涓涓湡姝g殑鍗忕▼錛屼絾榪欏茍鏃犲お澶х殑 鍏崇郴錛屼互鍚庝富鍑芥暟涔熻縐頒負涓誨崗紼?銆傜ず渚嬭繍琛岀殑緇撴灉鏄細
銆銆
銆銆1: foo1 received value 0
銆銆1: foo2 received value 10
銆銆1: main received value 20
銆銆2: foo2 received value 30
銆銆2: foo1 received value 40
銆銆2: main received value 50
銆銆3: foo1 received value 60
銆銆3: main received value 70
銆銆
銆銆鍗忕▼鐨勬墽琛屽簭鍒楁槸錛歮ain->foo1->foo2->main->foo2->foo1->main->foo1->main銆?br />銆銆
銆 銆銆銆coro.transfer(k,val)鍑芥暟涓璳鏄皢瑕佹帴鏀剁▼搴忔帶鍒舵潈鐨勫崗紼嬶紝鑰寁al鏄紶閫掔粰k鐨勬暟鎹傚鏋滃綋鍓嶅崗紼嬩笉鏄富鍗忕▼錛? tansfer(k,val)灝辯畝鍗曞湴鍒╃敤coroutine.yield(k,val)灝嗗綋鍓嶅崗紼嬫寕璧峰茍浼犲洖涓ら」鏁版嵁錛屽嵆紼嬪簭鎺у埗鏉冪殑涓嬩竴绔欏拰浼犻掔粰瀹? 鐨勬暟鎹紱鍚﹀垯榪涘叆涓涓帶鍒舵潈鍒嗘淳(dispatch)寰幆錛岃寰幆(閲?鍚姩(resume)k鍗忕▼錛岀瓑寰呭畠鎵ц鍒版寕璧?suspend)錛屽茍鏍規嵁姝ゆ椂鍗? 紼嬩紶鍥炵殑鏁版嵁鏉ュ喅瀹氫笅涓涓(閲?鍚姩鐨勫崗紼嬨備粠搴旂敤紺轟緥鏉ョ湅錛屽崗紼嬩笌鍗忕▼涔嬮棿浼間箮鏄敤transfer鐩存帴浼犻掓帶鍒舵潈鐨勶紝浣嗗疄闄呬笂榪欎釜浼犻掕繕鏄氳繃浜嗕富 鍗忕▼銆傛瘡涓涓湪涓誨崗紼嬮噷琚皟鐢?姣旇緝coro.current鍜宑oro.main鏄惁鐩稿悓鍗沖彲鍒ゆ柇鍑?鐨則ransfer閮界浉褰撲簬涓涓崗紼嬬鐞嗗櫒錛屽畠涓? 鏂湴(閲?鍚姩涓涓崗紼嬶紝灝嗘帶鍒舵潈浜ゅ嚭鍘伙紝鐒跺悗絳夐偅涓崗紼嬫寕璧鋒椂鍙堝皢鎺у埗鏉冩敹鍥烇紝鐒跺悗鍐?閲?鍚姩涓嬩竴涓崗紼?..錛岃繖涓姩浣滀笉浼氬仠姝紝闄ら潪< 1>灝?閲?鍚姩鐨勫崗紼嬫槸涓誨崗紼嬶紱<2>鏌愪釜鍗忕▼娌℃湁鎻愪緵鎺у埗鏉冪殑涓嬩竴涓洰鐨勫湴銆傚緢鏄劇劧錛屾瘡涓杞垎媧懼驚鐜紑濮嬫椂閮界敱涓誨崗紼嬫妸鎻℃帶鍒舵潈錛? 鍦ㄥ驚鐜繃紼嬩腑濡傛灉鎺у埗鏉冪殑涓嬩竴绔欏張鏄富鍗忕▼鐨勮瘽灝辨剰鍛崇潃榪欎釜褰撳垵鎶婃帶鍒舵潈浜ゅ嚭鍘葷殑涓誨崗紼媡ransfer鎿嶄綔搴旇緇撴潫浜嗭紝鎵浠ュ嚱鏁扮洿鎺ヨ繑鍥瀡al浠庤岀粨鏉? 榪欒疆寰幆銆傚浜庢儏鍐?lt;2>錛屽洜涓篶oro.create(f)鍒涘緩鐨勫崗紼嬬殑浣撳嚱鏁?body function)瀹為檯鏄痜unction(val) return nil,f(val) end錛屾墍浠ュ綋鍑芥暟f鐨勬渶鍚庝竴鏉℃寚浠や笉鏄痶ransfer鏃訛紝榪欎釜鍗忕▼緇堝皢鎵ц瀹屾瘯騫舵妸nil鍜屽嚱鏁癴鐨勮繑鍥炲間竴璧瘋繑鍥炪傚鏋渒鏄繖鏍風殑鍗忕▼錛? transfer鎵ц瀹宬,val = k(val)璇彞鍚巏鍊煎氨鎴愪簡nil錛岃繖琚涓轟竴涓敊璇紝鍥犱負紼嬪簭姝ゆ椂娌℃硶紜畾涓嬩竴涓簲璇?閲?鍚姩鐨勫崗紼嬪埌搴曟槸璋併傛墍浠ュ湪瀵圭О寮忔ā鍨嬩笅錛屾瘡涓涓崗紼?褰? 鐒朵富鍗忕▼鍑哄)鏈鍚庨兘蹇呴』鏄懼紡鍦板皢鎺у埗鏉冧紶閫掔粰鍏跺畠鐨勫崗紼嬨傛牴鎹互涓婂垎鏋愶紝搴旂敤紺轟緥鐨勬帶鍒舵潈鐨勫垎媧懼簲涓猴細
銆銆
銆銆絎竴杞垎媧? main->foo1->main->foo2->main->main(緇撴潫)
銆銆絎簩杞垎媧? main->foo2->main->foo1->main->main(緇撴潫)
銆銆絎笁杞垎媧? main->foo1->main->main(緇撴潫)
銆銆
銆 銆銆銆鐢變簬鍙互鐩存帴鎸囧畾鎺у埗鏉冧紶閫掔殑鐩爣錛屽縐板紡鍗忕▼鏈哄埗鎷ユ湁鏋佸ぇ鐨勮嚜鐢憋紝浣嗗緱鍒拌繖縐嶈嚜鐢辯殑浠d環鍗存槸鐗虹壊紼嬪簭緇撴瀯銆傚鏋滅▼搴忕◢寰鏉備竴鐐癸紝閭d箞鍗充嬌鏄潪甯? 鏈夌粡楠岀殑紼嬪簭鍛樹篃寰堥毦瀵圭▼搴忔祦紼嬫湁鍏ㄩ潰鑰屾竻鏅扮殑鎶婃彙銆傝繖闈炲父綾諱技goto璇彞錛屽畠鑳借紼嬪簭璺寵漿鍒頒換浣曟兂鍘葷殑鍦版柟錛屼絾浜轟滑鍗村緢闅劇悊瑙e厖鏂ョ潃goto鐨勭▼搴忋? 闈炲縐板紡鍗忕▼鍏鋒湁鑹ソ鐨勫眰嬈″寲緇撴瀯鍏崇郴錛?閲?鍚姩榪欎簺鍗忕▼涓庤皟鐢ㄤ竴涓嚱鏁伴潪甯哥被浼鹼細琚?閲?鍚姩鐨勫崗紼嬪緱鍒版帶鍒舵潈寮濮嬫墽琛岋紝鐒跺悗鎸傝搗(鎴栫粨鏉?騫跺皢鎺у埗 鏉冭繑鍥炵粰鍗忕▼璋冪敤鑰咃紝榪欎笌璁$畻鏈哄厛鍝蹭滑鍊″鐨勭粨鏋勫寲緙栫▼椋庢牸瀹屽叏涓鑷淬?br />銆銆
銆銆銆銆緇間笂鎵榪幫紝Lua鎻愪緵鐨勯潪瀵圭О寮忓崗紼嬩笉浣嗗叿鏈変笌瀵圭О寮忓崗紼嬩竴鏍峰己澶х殑鑳藉姏錛岃屼笖榪樿兘閬垮厤紼嬪簭鍛樻互鐢ㄦ満鍒跺啓鍑虹粨鏋勬販涔辯殑紼嬪簭銆?img src ="http://www.shnenglu.com/swo2006/aggbug/14530.html" width = "1" height = "1" />

swo 2006-11-01 18:46 鍙戣〃璇勮
]]>
lua鐨勭綉鍧http://www.shnenglu.com/swo2006/archive/2006/11/01/14528.htmlswoswoWed, 01 Nov 2006 09:40:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14528.htmlhttp://www.shnenglu.com/swo2006/comments/14528.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14528.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14528.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14528.html
http://www.personal.kent.edu/~rmuhamma/Compilers/compiler.html

http://www.luachina.net/bbs/125/ShowPost.aspx

http://lua-users.org/wiki/WikiHelp

http://luaplus.org/tiki-index.php


swo 2006-11-01 17:40 鍙戣〃璇勮
]]>
鍦ㄤ綘鐨勬父鎴忎腑搴旂敤LUA(ZT)錛堣漿杞斤級http://www.shnenglu.com/swo2006/archive/2006/11/01/14475.htmlswoswoWed, 01 Nov 2006 05:49:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14475.htmlhttp://www.shnenglu.com/swo2006/comments/14475.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14475.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14475.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14475.html
鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細鍦ㄤ綘鐨勬父鎴忎唬鐮佷腑榪愯瑙i噴鍣?br />
聽 閫氬父錛屼綘甯屾湜鍦ㄤ綘鐨勬父鎴忓紑濮嬬殑鏃跺欒鍙栦竴浜涗俊鎭紝浠ラ厤緗綘鐨勬父鎴忥紝榪欎簺淇℃伅閫氬父閮芥槸鏀懼埌涓涓枃鏈枃浠朵腑錛屽湪浣犵殑娓告垙鍚姩鐨勬椂鍊欙紝浣犻渶瑕佹墦寮榪欎釜鏂囦歡錛岀劧鍚庤В鏋愬瓧絎︿覆錛屾壘鍒版墍闇瑕佺殑淇℃伅銆?br />
聽 鏄殑錛屾垨璁鎬綘璁や負榪欐牱灝辮凍澶熶簡錛屼負浠涔堣繕瑕佷嬌鐢↙ua鍛紵

聽 搴旂敤浜庘滈厤緗濊繖涓洰鐨勶紝Lua鎻愪緵緇欎綘鏇翠負寮哄ぇ錛屼篃鏇翠負鐏墊椿鐨勮〃杈炬柟寮忥紝鍦ㄤ笂涓縐嶆柟寮忎腑錛屼綘鏃犳硶鏍規嵁鏌愪簺鏉′歡鏉ラ厤緗綘鐨勬父鎴忥紝Lua鎻愪緵緇欎綘鐏墊椿鐨勮〃杈炬柟寮忥紝浣犲彲浠ョ被浼間簬榪欐牱鏉ラ厤緗綘鐨勬父鎴忥細

if player:is_dead() then
do_something()
else
do_else()
end

鏇翠負閲嶈鐨勬槸錛屽湪浣犲仛浜嗕竴浜涗慨鏀逛箣鍚庯紝瀹屽叏涓嶉渶瑕侀噸鏂扮紪璇戜綘鐨勬父鎴忎唬鐮併?br />
閫氬父錛屽湪娓告垙涓綘騫朵笉闇瑕佷竴涓崟鐙殑瑙i噴鍣紝浣犻渶瑕佸湪娓告垙鏉ヨ繍琛岃В閲婂櫒錛屼笅闈紝璁╂垜浠潵鐪嬬湅錛屽浣曞湪浣犵殑浠g爜涓繍琛岃В閲婂櫒錛?br />
//榪欐槸lua鎵闇鐨勪笁涓ご鏂囦歡
//褰撶劧錛屼綘闇瑕侀摼鎺ュ埌姝g‘鐨刲ib
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(int argc, char *argv[])
{
lua_State *L = lua_open();
luaopen_base(L);
luaopen_io(L);

const char *buf = "print('hello, world!')";

lua_dostring(buf);

lua_close(L);

return 0;
}

紼嬪簭杈撳嚭錛歨ello, world!

鏈夋椂浣犻渶瑕佹墽琛屼竴孌靛瓧絎︿覆錛屾湁鏃朵綘鍙兘闇瑕佹墽琛屼竴涓枃浠訛紝褰撲綘闇瑕佹墽琛屼竴涓枃浠舵椂錛屼綘鍙互榪欎箞鍋氾細
lua_dofile(L, "test.lua");

鐪嬶紝闈炲父綆鍗曞惂銆?br />






鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細Getting Value

鍦ㄤ笂涓綃囨枃绔犳垜浠兘澶熷湪鎴戜滑鐨勬父鎴忎唬鐮佷腑鎵цLua瑙i噴鍣紝涓嬮潰璁╂垜浠潵鐪嬬湅濡備綍浠庤剼鏈腑鍙栧緱鎴戜滑鎵闇瑕佺殑淇℃伅銆?br />
棣栧厛錛岃鎴戞潵綆鍗曠殑瑙i噴涓涓婰ua瑙i噴鍣ㄧ殑宸ヤ綔鏈哄埗錛孡ua瑙i噴鍣ㄨ嚜韜淮鎶や竴涓繍琛屾椂鏍堬紝閫氳繃榪欎釜榪愯鏃舵爤錛孡ua瑙i噴鍣ㄥ悜涓繪満紼嬪簭浼犻掑弬鏁幫紝鎵浠ユ垜浠彲浠ヨ繖鏍鋒潵寰楀埌涓涓剼鏈彉閲忕殑鍊鹼細

lua_pushstring(L, "var"); //灝嗗彉閲忕殑鍚嶅瓧鏀懼叆鏍?br />lua_gettatbl(L, LUA_GLOBALSINDEX);鍙橀噺鐨勫肩幇鍦ㄦ爤欏?br />
鍋囪浣犲湪鑴氭湰涓湁涓涓彉閲?var = 100
浣犲彲浠ヨ繖鏍鋒潵寰楀埌榪欎釜鍙橀噺鍊鹼細
int var = lua_tonumber(L, -1);

鎬庝箞鏍鳳紝鏄笉鏄緢綆鍗曪紵

Lua瀹氫箟浜嗕竴涓畯璁╀綘綆鍗曠殑鍙栧緱涓涓彉閲忕殑鍊鹼細
lua_getglobal(L, name)

鎴戜滑鍙互榪欐牱鏉ュ彇寰椾竴涓彉閲忕殑鍊鹼細
lua_getglobal(L, "var"); //鍙橀噺鐨勫肩幇鍦ㄦ爤欏?br />int var = lua_tonumber(L, -1);

瀹屾暣鐨勬祴璇曚唬鐮佸涓嬶細

#include "lua.h"
#inculde "lauxlib.h"
#include "lualib.h"

int main(int argc, char *argv[])
{
lua_State *L = lua_open();
luaopen_base(L);
luaopen_io(L);

const char *buf = "var = 100";

lua_dostring(L, buf);

lua_getglobal(L, "var");
int var = lua_tonumber(L, -1);

assert(var == 100);

lua_close(L);

return 0;
}






鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細璋冪敤鍑芥暟

鍋囪浣犲湪鑴氭湰涓畾涔変簡涓涓嚱鏁幫細

function main(number)
number = number + 1
return number
end

鍦ㄤ綘鐨勬父鎴忎唬鐮佷腑錛屼綘甯屾湜鍦ㄦ煇涓椂鍒昏皟鐢ㄨ繖涓嚱鏁板彇寰楀畠鐨勮繑鍥炲箋?br />
鍦↙ua涓紝鍑芥暟絳夊悓浜庡彉閲忥紝鎵浠ヤ綘鍙互榪欐牱鏉ュ彇寰楄繖涓嚱鏁幫細

lua_getglobal(L, "main");//鍑芥暟鐜板湪鏍堥《

鐜板湪錛屾垜浠彲浠ヨ皟鐢ㄨ繖涓嚱鏁幫紝騫朵紶閫掔粰瀹冩紜殑鍙傛暟錛?br />
lua_pushnumber(L, 100); //灝嗗弬鏁板帇鏍?br />lua_pcall(L, 1, 1, 0); //璋冪敤鍑芥暟錛屾湁涓涓弬鏁幫紝涓涓繑鍥炲?br />//榪斿洖鍊肩幇鍦ㄦ爤欏?br />int result = lua_tonumber(L, -1);

result 灝辨槸鍑芥暟鐨勮繑鍥炲?br />
瀹屾暣鐨勬祴璇曚唬鐮佸涓嬶細

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int main(int argc, char *argv[])
{
lua_State *L = lua_open();
luaopen_base(L);

const char *buf = "function main(number) number = number + 1 return number end";

lua_dostring(buf);

lua_getglobal(L, "main");
lua_pushnumber(L, 100);
lua_pcall(L, 1, 1, 0);

int result = lua_tonumber(L, -1);

assert(result == 101);

lua_close(L);

return 0;
}





鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細鎵╁睍Lua


Lua鏈韓瀹氫綅鍦ㄤ竴縐嶈交閲忕駭鐨勶紝鐏墊椿鐨勶紝鍙墿鍏呯殑鑴氭湰璇█錛岃繖鎰忓懗鐫浣犲彲浠ヨ嚜鐢辯殑鎵╁厖Lua錛屼負浣犺嚜宸辯殑娓告垙閲忚韓瀹氬仛涓涓剼鏈璦銆?br />
浣犲彲浠ュ湪涓繪満紼嬪簭涓悜鑴氭湰鎻愪緵浣犺嚜瀹氱殑api錛屼緵鑴氭湰璋冪敤銆?br />
Lua瀹氫箟浜嗕竴縐嶇被鍨嬶細lua_CFunction錛岃繖鏄竴涓嚱鏁版寚閽堬紝瀹冪殑鍘熷瀷鏄細
typedef int (*lua_CFunction) (lua_State *L);

榪欐剰鍛崇潃鍙湁榪欑綾誨瀷鐨勫嚱鏁版墠鑳藉悜Lua娉ㄥ唽銆?br />
棣栧厛錛屾垜浠畾涔変竴涓嚱鏁?br />
int foo(lua_State *L)
{
//棣栧厛鍙栧嚭鑴氭湰鎵ц榪欎釜鍑芥暟鏃跺帇鍏ユ爤鐨勫弬鏁?br />//鍋囪榪欎釜鍑芥暟鎻愪緵涓涓弬鏁幫紝鏈変袱涓繑鍥炲?br />
//get the first parameter
const char *par = lua_tostring(L, -1);

printf("%s\n", par);

//push the first result
lua_pushnumber(L, 100);

//push the second result
lua_pushnumber(L, 200);

//return 2 result
return 2;
}

鎴戜滑鍙互鍦ㄨ剼鏈腑榪欐牱璋冪敤榪欎釜鍑芥暟

r1, r2 = foo("hello")

print(r1..r2)

瀹屾暣鐨勬祴璇曚唬鐮佸涓嬶細

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

int foo(lua_State *L)
{
//棣栧厛鍙栧嚭鑴氭湰鎵ц榪欎釜鍑芥暟鏃跺帇鍏ユ爤鐨勫弬鏁?br />//鍋囪榪欎釜鍑芥暟鎻愪緵涓涓弬鏁幫紝鏈変袱涓繑鍥炲?br />
//get the first parameter
const char *par = lua_tostring(L, -1);

printf("%s\n", par);

//push the first result
lua_pushnumber(L, 100);

//push the second result
lua_pushnumber(L, 200);

//return 2 result
return 2;
}

int main(int argc, char *argv[])
{
lua_State *L = lua_open();
luaopen_base(L);
luaopen_io(L);

const char *buf = "r1, r2 = foo("hello") print(r1..r2)";

lua_dostring(L, buf);

lua_close(L);

return 0;
}

紼嬪簭杈撳嚭錛?br />hello
100200




鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細using lua in cpp


lua鍜屼富鏈虹▼搴忎氦鎹㈠弬鏁版槸閫氳繃涓涓繍琛屾椂鏍堟潵榪涜鐨勶紝榪愯鏃舵爤鐨勪俊鎭斁鍦ㄤ竴涓猯ua_State鐨勭粨鏋勪腑錛宭ua鎻愪緵鐨刟pi閮介渶瑕佷竴涓猯ua_State*鐨勬寚閽堬紝闄や簡涓涓細

lua_open();

榪欎釜鍑芥暟灝嗚繑鍥炰竴涓猯ua_State*鍨嬬殑鎸囬拡錛屽湪浣犵殑娓告垙浠g爜涓紝浣犲彲浠ヤ粎浠呮嫢鏈変竴涓繖鏍風殑鎸囬拡錛屼篃鍙互鏈夊涓繖鏍風殑鎸囬拡銆?br />
鏈鍚庯紝浣犻渶瑕侀噴鏀捐繖涓寚閽堬紝閫氳繃鍑芥暟錛?br />
lua_close(L);

娉ㄦ剰榪欎釜浜嬪疄錛屽湪浣犵殑涓繪満紼嬪簭涓紝open()涓巆lose()姘歌繙鏄垚瀵瑰嚭鐜扮殑錛屽湪c++涓紝濡傛灉鏈変竴浜涗簨鎯呮槸鎴愬鍑虹幇鐨勶紝榪欓氬父鎰忓懗鐫浣犻渶瑕佷竴涓瀯閫犲嚱鏁板拰涓涓瀽鏋勫嚱鏁幫紝鎵浠ワ紝鎴戜滑棣栧厛瀵筶ua_State鍋氫竴涓嬪皝瑁咃細

#ifndef LUA_EXTRALIBS
#define LUA_EXTRALIBS /* empty */
#endif

static const luaL_reg lualibs[] =
{
{"base", luaopen_base},
{"table", luaopen_table},
{"io", luaopen_io},
{"string", luaopen_string},
{"math", luaopen_math},
{"debug", luaopen_debug},
{"loadlib", luaopen_loadlib},
/* add your libraries here */
LUA_EXTRALIBS
{NULL, NULL}
};

榪欐槸lua鎻愪緵緇欑敤鎴風殑涓浜涜緟鍔╃殑lib錛屽湪浣跨敤lua_State鐨勬椂鍊欙紝浣犲彲浠ラ夋嫨鎵撳紑鎴栬呭叧闂畠銆?br />
瀹屾暣鐨勭被瀹炵幇濡備笅錛?br />

//lua_State
class state
{
public:
state(bool bOpenStdLib = false)
:
err_fn(0)
{
L = lua_open();

assert(L);

if (bOpenStdLib)
{
open_stdlib();
}
}

~state()
{
lua_setgcthreshold(L, 0);
lua_close(L);
}

void open_stdlib()
{
assert(L);

const luaL_reg *lib = lualibs;
for (; lib->func; lib++)
{
lib->func(L); /* open library */
lua_settop(L, 0); /* discard any results */
}
}

lua_State* get_handle()
{
return L;
}

int error_fn()
{
return err_fn;
}

private:
lua_State *L;

int err_fn;
};


閫氬父鎴戜滑浠呬粎鍦ㄦ父鎴忎唬鐮佷腑浣跨敤涓涓猯ua_State*鐨勬寚閽堬紝鎵浠ユ垜浠負瀹冨疄鐜頒竴涓崟浠訛紝榛樿鎵撳紑鎵鏈塴ua鎻愪緵鐨刲ib:


//return the global instance
state* lua_state()
{
static state L(true);

return &L;
}




鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細using lua in cpp錛堝皝瑁呮爤鎿嶄綔錛夈

鍓嶉潰鎻愬埌浜唋ua涓庝富鏈虹▼搴忔槸閫氳繃涓涓繍琛屾椂鏍堟潵浜ゆ崲淇℃伅鐨勶紝鎵浠ユ垜浠妸瀵規爤鐨勮闂仛涓涓嬬畝鍗曠殑灝佽銆?br />
鎴戜滑鍒╃敤浠巆++鐨勫嚱鏁伴噸杞芥満鍒跺榪欎簺鎿嶄綔鍋氬皝瑁咃紝閲嶈澆鎻愪緵緇欐垜浠竴縐嶄互緇熶竴鐨勬柟寮忔潵澶勭悊鎿嶄綔鐨勬満鍒躲?br />
鍚憀ua浼犻掍俊鎭槸閫氳繃鍘嬫爤鐨勬搷浣滄潵瀹屾垚鐨勶紝鎵浠ユ垜浠畾涔変竴浜汸ush()鍑芥暟錛?br />
inline void Push(lua_State *L, int value);
inline void Push(lua_State *L, bool value);
...

瀵瑰簲綆鍗曠殑c++鍐呭緩綾誨瀷錛屾垜浠疄鐜板嚭鐩稿悓鐨凱ush鍑芥暟錛岃嚦浜庡嚱鏁板唴閮ㄧ殑瀹炵幇鏄潪甯哥殑綆鍗曪紝鍙鍒╃敤lua鎻愪緵鐨刟pi鏉ュ疄鐜板嵆鍙紝渚嬪錛?br />
inline void Push(lua_State *L, int value)
{
lua_pushnumber(L, value);
}

榪欑鏂瑰紡甯︽潵鐨勫ソ澶勬槸錛屽湪鎴戜滑鐨勪唬鐮佷腑鎴戜滑鍙互浠ヤ竴縐嶇粺涓鐨勬柟寮忔潵澶勭悊鍘嬫爤鎿嶄綔錛屽鏋滄湁涓縐嶇被鍨嬫病鏈夊畾涔夌浉鍏崇殑鍘嬫爤鎿嶄綔錛屽皢浜х敓涓涓紪璇戞湡閿欒銆?br />
鍚庨潰鎴戜細鎻愬埌錛屽浣曞皢涓涓敤鎴瘋嚜瀹氫箟綾誨瀷鐨勬寚閽堜紶閫掑埌lua涓紝鍦ㄩ偅縐嶆儏鍐典笅錛屾垜浠殑鍩烘湰浠g爜鏃犻』鏀瑰彉錛屽彧瑕佹坊鍔犱竴涓浉搴旂殑Push()鍑芥暟鍗沖彲銆?br />
璁頒綇close-open鍘熷垯鍚э紝瀹冪殑鎰忔濇槸瀵逛慨鏀規槸灝侀棴鐨勶紝瀵規墿鍏呮槸寮鏀劇殑錛屽ソ鐨勭被搴撹璁″厑璁鎬綘鎵╁厖瀹冿紝鑰屾棤欏諱慨鏀瑰畠鐨勫疄鐜幫紝鐢氳嚦鏃犻』閲嶆柊緙栬瘧銆?br />
銆奵++娉涘瀷璁捐鏂版濈淮銆嬩竴涔︽彁鍒頒簡涓縐嶆妧鏈彨type2type錛屽畠鐨勬湰璐ㄦ槸寰堢畝鍗曪細

template <typename T>
struct type2type
{
typedef T U;
};

姝e浣犵湅鍒扮殑錛屽畠騫舵病鏈変換浣曟暟鎹垚鍛橈紝瀹冪殑瀛樺湪鍙槸涓轟簡鎼哄甫綾誨瀷淇℃伅銆?br />
綾誨瀷鍒扮被鍨嬬殑鏄犲皠鍦ㄥ簲鐢ㄤ簬閲嶈澆鍑芥暟鏃舵槸闈炲父鏈夌敤鐨勶紝搴旂敤type2type錛屽彲浠ュ疄鐜扮紪璇戞湡鐨勫垎媧俱?br />
涓嬮潰鐪嬬湅鎴戜滑濡備綍鍦ㄤ粠鏍堜腑鍙栧緱lua淇℃伅鏃跺簲鐢╰ype2type錛?br />
嫻嬭瘯綾誨瀷錛氱敱浜巐ua鐨勭被鍨嬬郴緇熶笌c++鏄笉鐩稿悓鐨勶紝鎵浠ワ紝鎴戜滑瑕佸鏍堜腑鐨勪俊鎭仛涓涓嬬被鍨嬫嫻嬨?br />
inline bool Match(type2type<bool>, lua_State *L, int idx)
{
return lua_type(L, idx) == LUA_TBOOLEAN;
}

綾諱技鐨勶紝鎴戜滑瑕佷負cpp鐨勫唴寤虹被鍨嬫彁渚涚浉搴旂殑Match鍑芥暟錛?br />
inline bool Match(type2type<int>, lua_State *L, int idx);
inline bool Match(type2type<const char*>, lua_State *L, int idx);

...

鍙互鐪嬪嚭錛宼ype2type鐨勫瓨鍦ㄥ彧鏄負浜嗗湪璋冪敤Match鏃跺喅璁埌姝g‘鐨勫嚱鏁頒笂錛岀敱浜庡畠娌℃湁浠諱綍鎴愬憳錛屾墍浠ヤ笉瀛樺湪榪愯鏃剁殑鎴愭湰銆?br />
鍚屾牱錛屾垜浠負cpp鍐呭緩綾誨瀷鎻愪緵Get()鍑芥暟錛?br />
inline bool Get(type2type<bool>, lua_State *L, int idx)
{
return lua_toboolean(L, idx);
}

inline int Get(type2type<int>, lua_State *L, int idx)
{
return static_cast<int>(lua_tonumber(L, idx));
}

...

鎴戞兂浣犲彲鑳芥敞鎰忓埌浜嗭紝鍦╥nt Get(type2type<int>)涓湁涓涓漿鍨嬬殑鍔ㄤ綔錛岀敱浜巐ua鐨勭被鍨嬬郴緇熶笌cpp鐨勭被鍨嬩笉鍚岋紝鎵浠ヨ漿鍨嬪姩浣滃繀欏葷殑銆?br />
闄ゆ涔嬪錛屽湪Get閲嶈澆鍑芥暟錛坰錛変腑榪樻湁涓涓皬灝忕殑緇嗚妭錛屾瘡涓狦et鐨勫嚱鏁扮殑榪斿洖鍊兼槸涓嶇浉鍚岀殑錛屽洜涓洪噸杞芥満鍒舵槸渚濋潬鍙傛暟鐨勪笉鍚屾潵璇嗗埆鐨勶紝鑰屼笉鏄繑鍥炲箋?br />
鍓嶉潰璇寸殑閮芥槸涓浜涘熀紜鐨勫皝瑁咃紝涓嬫潵鎴戜滑灝嗕粙緇嶅浣曞悜lua娉ㄥ唽涓涓鍙傛暟鐨刢鍑芥暟銆傝繕璁板緱鍚楋紵鍒╃敤lua鐨刟pi鍙兘娉ㄥ唽int (*ua_CFunction)(lua_State *)鍨嬬殑c鍑芥暟錛屽埆蹇樿浜嗭紝lua鏄敤c鍐欑殑銆?br />



鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細using lua in cpp錛堟敞鍐屼笉鍚岀被鍨嬬殑c鍑芥暟錛変箣涓銆

鍓嶉潰璇村埌錛屾垜浠彲浠ュ埄鐢╨ua鎻愪緵鐨刟pi錛屽悜鑴氭湰鎻愪緵鎴戜滑鑷繁鐨勫嚱鏁幫紝鍦╨ua涓紝鍙湁lua_CFunction綾誨瀷鐨勫嚱鏁版墠鑳界洿鎺ュ悜lua娉ㄥ唽錛宭ua_CFunction瀹為檯涓婃槸涓涓嚱鏁版寚閽堬細
typedef int (*lua_CFunction)(lua_State *L);

鑰屽湪瀹為檯鐨勫簲鐢ㄤ腑錛屾垜浠彲鑳介渶瑕佸悜lua娉ㄥ唽鍚勭鍙傛暟鍜岃繑鍥炲肩被鍨嬬殑鍑芥暟錛屼緥濡傦紝鎻愪緵涓涓猘dd鑴氭湰鍑芥暟錛岃繑鍥炰袱涓肩殑鍜岋細

int add(int x, int y);

涓轟簡瀹炵幇榪欎釜鐩殑錛岄鍏堬紝鎴戜滑瀹氫箟涓猯ua_CFunction綾誨瀷鐨勫嚱鏁幫細

int add_proxy(lua_State *L)
{
//鍙栧緱鍙傛暟
if (!Match(TypeWrapper<int>(), L, -1))
return 0;
if (!Match(TypeWrapper<int>(), L, -2))
return 0;

聽 int x = Get(TypeWrapper<int>(), L, -1);
聽 int y = Get(TypeWrapper<int>(), L, -1);

聽 //璋冪敤鐪熸鐨勫嚱鏁?br />聽 int result = add(x, y);

聽 //榪斿洖緇撴灉
聽 Push(result);

聽 return 1;
}

鐜板湪錛屾垜浠彲浠ュ悜lua娉ㄥ唽榪欎釜鍑芥暟錛?br />
lua_pushstring(L, 鈥渁dd鈥?;
lua_pushcclosure(L, add_proxy, 0);
lua_settable(L, LUA_GLOBALINDEX);

鍦ㄨ剼鏈腑鍙互榪欐牱璋冪敤榪欎釜鍑芥暟錛?br />
print(add(100, 200))

浠庝笂闈㈢殑姝ラ鍙互鐪嬪嚭錛屽鏋滈渶瑕佸悜lua娉ㄥ唽涓涓潪lua_CFunction綾誨瀷鐨勫嚱鏁幫紝闇瑕侊細
1錛?涓鴻鍑芥暟瀹炵幇涓涓皝瑁呰皟鐢ㄣ?br />2錛?鍦ㄥ皝瑁呰皟鐢ㄥ嚱鏁頒腑浠巐ua鏍堜腑鍙栧緱鎻愪緵鐨勫弬鏁般?br />3錛?浣跨敤鍙傛暟璋冪敤璇ュ嚱鏁般?br />4錛?鍚憀ua浼犻掑叾緇撴灉銆?br />
娉ㄦ剰錛屾垜浠洰鍓嶅彧鏄拡瀵瑰叏灞c鍑芥暟錛岀被鐨勬垚鍛樺嚱鏁版殏鏃朵笉娑夊強錛屽湪cpp涓紝綾葷殑闈欐佹垚鍛樺嚱鏁頒笌c鍑芥暟綾諱技銆?br />
鍋囪鎴戜滑鏈夊涓潪lua_CFunction綾誨瀷鐨勫嚱鏁板悜lua娉ㄥ唽錛屾垜浠渶瑕佷負姣忎竴涓嚱鏁伴噸澶嶄笂闈㈢殑姝ラ錛屼駭鐢熶竴涓皝瑁呰皟鐢紝鍙互鐪嬪嚭錛岃繖浜涙楠ゅぇ澶氭槸鏈烘鐨勶紝鍥犳錛屾垜浠渶瑕佷竴縐嶆柟寮忚嚜鍔ㄧ殑瀹炵幇涓婇潰鐨勬楠ゃ?br />
棣栧厛鐪嬫楠?錛屽湪cpp涓紝浜х敓榪欐牱涓涓皝瑁呰皟鐢ㄧ殑鍑芥暟鐨勬渶浣崇殑鏂瑰紡鏄嬌鐢╰emplate錛屾垜浠渶瑕佹彁渚涗竴涓猯ua_CFunction綾誨瀷鐨勬ā鏉垮嚱鏁幫紝鍦ㄨ繖涓嚱鏁頒腑璋冪敤鐪熸鐨勫悜鑴氭湰娉ㄥ唽鐨勫嚱鏁幫紝綾諱技浜庤繖鏍鳳細
template <typename Func>
inline int register_proxy(lua_State *L)

鐜板湪鐨勯棶棰樺湪浜庯細鎴戜滑瑕佸湪榪欎釜鍑芥暟涓皟鐢ㄧ湡姝g殑鍑芥暟錛岄偅涔堟垜浠繀欏昏鍦ㄨ繖涓嚱鏁頒腑鍙栧緱涓涓嚱鏁版寚閽堬紝鐒惰岋紝lua_CFunction綾誨瀷鐨勫嚱鏁板張涓嶅厑璁鎬綘鍦ㄥ鍔犲埆鐨勫弬鏁版潵鎻愪緵榪欎釜鍑芥暟鎸囬拡錛岀幇鍦ㄨ鎬庝箞璁﹔egisger_proxy鍑芥暟鐭ラ亾鎴戜滑鐪熸瑕佹敞鍐岀殑鍑芥暟鍛紵

鍦╫op涓紝浼間箮鍙互浣跨敤綾繪潵瑙e喅榪欎釜闂錛?br />
template <Func>
struct register_helper
{
explicit register_helper(Func fn) : m_func(fn)
{}
int register_proxy(lua_State *L);

protected:
Func m_func;
};

鍙槸涓嶈蹇樿錛宭ua_CFunction綾誨瀷鎸囧悜鐨勬槸涓涓猚鍑芥暟錛岃屼笉鏄竴涓垚鍛樺嚱鏁幫紝浠栦滑鐨勮皟鐢ㄦ柟寮忔槸涓嶄竴鏍風殑錛屽鏋滃皢涓婇潰鐨刬nt register_proxy()璁劇疆涓洪潤鎬佹垚鍛樺嚱鏁頒篃涓嶈錛屽洜涓烘垜浠渶瑕佽闂被鐨勬垚鍛樺彉閲弇_func;

璁╂垜浠啀瑙傚療涓涓媗ua_CFunction綾誨瀷鐨勫嚱鏁幫細

int register_proxy(lua_State *L);

鎴戜滑鐪嬪埌錛岃繖閲岄潰鏈変竴涓猯ua_State*鍨嬬殑鎸囬拡錛屾垜浠兘涓嶈兘灝嗙湡姝g殑鍑芥暟鎸囬拡鏀懼埌榪欓噷闈㈠瓨鍌紝鍒扮湡姝h皟鐢ㄧ殑鏃跺欙紝鍐嶄粠閲岄潰鍙栧嚭鏉ュ憿錛?br />
Lua鎻愪緵浜嗕竴涓猘pi鍙互瀛樺偍鐢ㄦ埛鏁版嵁錛?br />Lua_newuserdata(L, size)

鍦ㄩ傚綋鐨勬椂鍒伙紝鎴戜滑鍙互鍐嶅彇鍑鴻繖涓暟鎹細

lua_touserdata(L, idx)

ok錛岀幇鍦ㄤ紶閫掑嚱鏁版寚閽堢殑闂鎴戜滑宸茬粡瑙e喅浜嗭紝鍚庨潰鍐嶇湅絎簩姝ワ細鍙栧緱鍙傛暟銆?br />




鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細using lua in cpp錛堟敞鍐屼笉鍚岀被鍨嬬殑c鍑芥暟錛変箣浜?br />
鍦ㄨВ鍐充簡浼犻掑嚱鏁版寚閽堢殑闂涔嬪悗錛岃鎴戜滑鏉ョ湅鐪嬭皟鐢ㄥ嚱鏁版椂浼氭湁涓浜涗粈涔堟牱鐨勯棶棰樸?br />
棣? 鍏堬紝褰撴垜浠氳繃鍑芥暟鎸囬拡璋冪敤榪欎釜鍑芥暟鐨勬椂鍊欙紝鐢變簬鎴戜滑闈㈠鐨勬槸鏈煡綾誨瀷鐨勫嚱鏁幫紝涔熷氨鏄錛屾垜浠茍涓嶇煡閬撳弬鏁扮殑涓暟錛屽弬鏁扮殑綾誨瀷錛岃繕鏈夎繑鍥炲肩殑綾誨瀷錛屾墍浠ユ垜 浠笉鑳界洿鎺ヤ粠lua鏍堜腑鍙栧緱鍙傛暟錛屽綋鐒訛紝鎴戜滑鍙互閫氳繃榪愯鏃舵祴璇曟爤涓殑淇℃伅鏉ュ緱鍒發ua浼犻掕繘鏉ョ殑鍙傛暟鐨勪釜鏁板拰綾誨瀷錛岃繖鎰忓懗鐫鎴戜滑鍦ㄧ◢鍚庨氳繃鍑芥暟鎸囬拡璋冪敤 鍑芥暟鏃朵篃闇瑕佸姩鎬佺殑鏍規嵁鍙傛暟鐨勪釜鏁板拰綾誨瀷鏉ュ喅璁埌姝g‘鐨勫嚱鏁幫紝榪欐牱錛岄櫎浜嗚繍琛屾椂鐨勬垚鏈紝cpp鎻愪緵緇欐垜浠殑寮虹被鍨嬫鏌ユ満鍒剁殑濂藉涔熷墿涓嶄簡澶氬皯浜嗭紝鎴戜滑闇瑕? 鐨勬槸涓縐嶉潤鎬佺殑緙栬瘧鏃剁殑鈥滃鎬佲濄?br />
鍦╟pp涓紝鑷沖皯鏈変袱縐嶆柟娉曞彲浠ュ疄鐜拌繖鐐廣傛渶鐩存帴綆鍗曠殑鏄嬌鐢ㄥ嚱鏁伴噸杞斤紝榪樻湁涓縐嶆槸鍒╃敤妯℃澘鐗瑰寲鏈哄埗銆?br />
綆鍗曠殑浠嬬粛涓涓嬫ā鏉跨壒鍖栵細

鍦╟pp涓紝鍙互閽堝涓涓ā鏉垮嚱鏁版垨鑰呮ā鏉跨被鍐欏嚭涓浜涚壒鍖栫増鏈紝緙栬瘧鍣ㄥ湪鍖歸厤妯℃澘鍙傛暟鏃朵細瀵繪壘鏈鍚堥傜殑涓涓増鏈傜被浼間簬榪欐牱錛?br />
templat <typename T>
T foo()
{
T tmp();
return tmp;
}

//鎻愪緵鐗瑰寲鐗堟湰
template <>
int foo()
{
return 100;
}

鍦╩ain()鍑芥暟涓紝鎴戜滑鍙互鏄劇ず鎸囧畾浣跨敤鍝釜鐗堟湰鐨刦oo錛?br />
int main(int argc, char **argv)
{
cout << foo<int>() << endl;
return 0;
}

紼嬪簭灝嗚緭鍑?00錛岃屼笉鏄?錛屼互涓婁唬鐮佸湪 g++涓紪璇戦氳繃錛岀敱浜巚c6瀵逛簬妯℃澘鐨勬敮鎸佷笉鏄緢濂斤紝鎵浠ユ湁涓浜涙ā鏉跨殑鎶鏈湪vc6涓彲鑳戒笉鑳界紪璇戦氳繃銆?br />
鎵浠ユ渶濂戒嬌鐢ㄩ噸杞芥潵瑙e喅榪欎釜闂錛屽湪灝佽鍑芥暟璋冪敤涓紝鎴戜滑棣栧厛鍙栧緱榪欎釜鍑芥暟鎸囬拡錛岀劧鍚庯紝鎴戜滑瑕佹彁渚涗竴涓狢all鍑芥暟鏉ョ湡姝h皟鐢ㄨ繖涓嚱鏁幫紝綾諱技浜庤繖鏍鳳細
//浼唬鐮?br />int Call(pfn, lua_State *L, int idx)

鍙槸鎴戜滑騫朵笉鐭ラ亾榪欎釜鍑芥暟鎸囬拡鐨勭被鍨嬶紝鐜板湪璇ユ庝箞鍐欏憿錛熷埆蹇樿浜嗭紝鎴戜滑鐨剅egister_proxy()鏄竴涓ā鏉垮嚱鏁幫紝瀹冩湁涓涓弬鏁拌〃紺轟簡榪欎釜鎸囬拡鐨勭被鍨嬶細

template <typename Func>
int register_proxy(lua_State *L)
{
//浼唬鐮侊紝閫氳繃L鍙傛暟鍙栧緱榪欎釜鎸囬拡
unsigned char *buffer = get_pointer(L);

//瀵硅繖涓寚閽堝仛寮哄埗綾誨瀷杞寲錛岃皟鐢–all鍑芥暟
return Call(*(Func*)buffer, L, 1);
}

鐢遍噸杞藉嚱鏁癈all璋冪敤鐪熸鐨勫嚱鏁幫紝榪欐牱錛屾垜浠彲浠ヤ嬌鐢╨ua api娉ㄥ唽鐩稿叧鐨勫嚱鏁幫紝涓嬫潵鎴戜滑鎻愪緵涓涓敞鍐岀殑鍑芥暟錛?br />
template <typename Func>
void lua_pushdirectclosure(Func fn, lua_State *L, int nUpvalue)
{
//浼唬鐮侊紝鍚慙瀛樺偍鍑芥暟鎸囬拡
save_pointer(L);

//鍚憀ua鎻愪緵鎴戜滑鐨剅egister_proxy鍑芥暟
lua_pushcclosure(L, register_proxy<Func>, nUpvalue + 1);
}

鍐嶅畾涔夌浉鍏崇殑娉ㄥ唽瀹忥細
#define lua_register_directclosure(L, func) \
lua_pushstring(L, #func);
lua_pushdirectclosure(func, L, 1);
lua_settable(L, LUA_GLOBALINDEX)

鐜板湪錛屽亣璁炬垜浠湁涓涓猧nt add(int x, int y)榪欐牱鐨勫嚱鏁幫紝鎴戜滑鍙互鐩存帴鍚憀ua娉ㄥ唽錛?br />
lua_register_directclosure(L, add);

鐪嬶紝鏈鍚庝嬌鐢ㄨ搗鏉ュ緢鏂逛究鍚э紝鎴戜滑鍐嶄篃涓嶇敤鎵嬪啓閭d箞澶氱殑灝佽璋冪敤鐨勪唬鐮佸暒錛屼笉榪囬棶棰樿繕娌℃湁瀹岋紝鍚庨潰鎴戜滑榪樺緱瑙e喅Call鍑芥暟鐨勯棶棰樸?br />



鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細using lua in cpp錛堟敞鍐屼笉鍚岀被鍨嬬殑c鍑芥暟錛変箣涓夈


涓嬮潰錛岃鎴戜滑闆嗕腑綺懼姏鏉ヨВ鍐矯all閲嶈澆鍑芥暟鐨勯棶棰樺惂銆?br />
鍓嶉潰宸茬粡璇磋繃鏉ワ紝Call閲嶈澆鍑芥暟鎺ュ彈涓涓嚱鏁版寚閽堬紝鐒跺悗浠巐ua鏍堜腑鏍規嵁鍑芥暟鎸囬拡鐨勭被鍨嬶紝鍙栧緱鐩稿叧鐨勫弬鏁幫紝騫惰皟鐢ㄨ繖涓嚱鏁幫紝鐒跺悗灝嗚繑鍥炲煎帇鍏ua鏍堬紝綾諱技浜庤繖鏍鳳細

//浼唬鐮?br />int Call(pfn, lua_State *L, int idx)

鐜板湪鐨勯棶棰樻槸pfn璇ュ浣曞0鏄庯紵鎴戜滑鐭ラ亾榪欐槸涓涓嚱鏁版寚閽堬紝鐒惰屽叾鍙傛暟錛屼互鍙婅繑鍥炲奸兘鏄湭鐭ョ殑綾誨瀷錛屽鏋滄垜浠煡閬撹繑鍥炲煎拰鍙傛暟鐨勭被鍨嬶紝鎴戜滑鍙互鐢ㄤ竴涓猼ypedef鏉ュ0鏄庡畠錛?br />
typedef void (*pfn)();

int Call(pfn fn, lua_State *L, int idx);

鎴戜滑鐭ラ亾鐨勮繑鍥炲間互鍙婂弬鏁扮殑綾誨瀷鍙槸涓涓ā鏉垮弬鏁癟錛屽湪cpp涓紝鎴戜滑涓嶈兘榪欐牱鍐欙細

template <typename T>
typedef T (*Func) ();

涓縐嶈В鍐沖姙娉曟槸浣跨敤綾繪ā鏉匡細

template <typename T>
struct CallHelper
{
typedef T (*Func) ();
};

鐒跺悗鍦–all涓紩鐢ㄥ畠錛?br />
template <typename T>
int Call(typename CallHelper::Func fn, lua_State *L, int idx)

娉ㄦ剰typename鍏抽敭瀛楋紝濡傛灉娌℃湁榪欎釜鍏抽敭瀛楋紝鍦╣++涓細浜х敓涓涓紪璇戣鍛婏紝瀹冪殑鎰忔濇槸鍛婅瘔緙栬瘧鍣紝CallHelper錛氾細Func鏄竴涓被鍨嬶紝鑰屼笉鏄彉閲忋?br />
濡傛灉鎴戜滑榪欐牱鏉ヨВ鍐籌紝灝遍渶瑕佸湪CallHelper涓負姣忕鎯呭喌澶ч噺瀹氫箟鍚勭綾誨瀷鐨勫嚱鏁版寚閽堬紝榪樻湁涓縐嶆柟娉曪紝鍐欐硶姣旇緝鍙ゆ紝鑰冭檻涓涓嚱鏁頒腑鍙傛暟鐨勫0鏄庯細

void (int n);

棣栧厛鏄被鍨嬶紝鐒跺悗鏄彉閲忥紝鑰屽簲鐢ㄤ簬鍑芥暟鎸囬拡涓婏細

typedef void (*pfn) ();
void (pfn fn);

浜嬪疄涓婏紝鍙互灝唗ypedef鐩存帴鍦ㄥ弬鏁拌〃涓啓鍑烘潵錛?br />
void (void (*pfn)() );

榪欐牱錛屾垜浠殑Call鍑芥暟鍙互鐩存帴榪欐牱鍐欙細

//閽堝娌℃湁鍙傛暟鐨凜all鍑芥暟
template <typename RT>
int Call(RT (*Func) () , lua_State *L, int idx);
{
//璋冪敤Func
RT ret = (*Func)();

//灝嗚繑鍥炲間氦緇檒ua
Push錛圠, ret錛夛紱

//鍛婅瘔lua鏈夊灝戜釜榪斿洖鍊?br />return 1;
}

//閽堝鏈変竴涓弬鏁扮殑Call
template <typename T, typename P1>
int Call(RT (*Func)(), lua_State *L, int idx)
{
//浠巐ua涓彇寰楀弬鏁?br />if (!Match(TypeWrapper<P1>(), L, -1)
return 0;

RT ret = (*Func) (Get(TypeWrapper<P1>(), L, -1));

Push(L, ret);
return 1;
}

鎸夌収涓婇潰鐨勫啓娉曪紝鎴戜滑鍙互鎻愪緵浠繪剰鍙傛暟涓暟鐨凜all鍑芥暟錛岀幇鍦ㄥ洖鍒版渶鍒濈殑鏃跺欙紝鎴戜滑鐨勫嚱鏁版寚閽堣閫氳繃lua_State *L鏉ュ瓨鍌紝榪欏彧瑕佸埄鐢╨ua鎻愪緵鐨刟pi灝卞彲浠ヤ簡錛岃繕璁板緱鎴戜滑鐨刲ua_pushdirectclosure鍑芥暟鍚楋細

template <typename Func>
void lua_pushdirectclosure(Func fn, lua_State *L, int nUpvalue)
{
//浼唬鐮侊紝鍚慙瀛樺偍鍑芥暟鎸囬拡
save_pointer(L);

//鍚憀ua鎻愪緵鎴戜滑鐨剅egister_proxy鍑芥暟
lua_pushcclosure(L, register_proxy<Func>, nUpvalue + 1);
}

鍏朵腑錛宻ave_pointer(L)鍙互榪欐牱瀹炵幇錛?br />
void save_pointer(lua_State *L)
{
unsigned char* buffer = (unsigned char*)lua_newuserdata(L, sizeof(func));
memcpy(buffer, &func, sizeof(func));
}


鑰屽湪register_proxy鍑芥暟涓細

template <typename Func>
int register_proxy(lua_State *L)
{
//浼唬鐮侊紝閫氳繃L鍙傛暟鍙栧緱榪欎釜鎸囬拡
unsigned char *buffer = get_pointer(L);
//瀵硅繖涓寚閽堝仛寮哄埗綾誨瀷杞寲錛岃皟鐢–all鍑芥暟
return Call(*(Func*)buffer, L, 1);
}
get_pointer鍑芥暟鍙互榪欐牱瀹炵幇錛?br />
unsigned char* get_pointer(lua_State *L)
{
聽 return (unsigned char*) lua_touserdata(L, lua_upvalueindex(1));
}

榪欎竴鐐硅兘澶熸湁鏁堣繍浣滀富瑕佷緷璧栦簬榪欐牱涓涓簨瀹烇細

鎴戜滑鍦╨ua鏍堜腑淇濆瓨榪欎釜鎸囬拡涔嬪悗錛屽湪娌℃湁瀵規爤鍋氫換浣曟搷浣滅殑鎯呭喌涓嬶紝鍙堟妸瀹冧粠鏍堜腑鍙栦簡鍑烘潵錛屾墍浠ヤ笉浼氬紕涔眑ua鏍堜腑鐨勪俊鎭紝璁頒綇錛宭ua鏍堜腑鐨勬暟鎹槸鐢辯敤鎴蜂繚璇佹潵娓呯┖鐨勩?br />
鍒扮幇鍦紝鎴戜滑宸茬粡鍙互鍚憀ua娉ㄥ唽浠繪剰涓弬鏁扮殑c鍑芥暟浜嗭紝鍙渶綆鍗曠殑涓琛屼唬鐮侊細

lua_register_directclosure(L, func)灝卞彲浠ュ暒銆?br />






鍦ㄤ綘鐨勬父鎴忎腑搴旂敤Lua錛?錛夛細Using Lua in cpp(鍩烘湰鏁版嵁綾誨瀷銆佹寚閽堝拰寮曠敤)涔嬩竴

Using Lua in cpp(鍩烘湰鏁版嵁綾誨瀷銆佹寚閽堝拰寮曠敤)

鍓嶉潰浠嬬粛鐨勯兘鏄拡瀵筩pp涓殑鍐呭緩鍩烘湰鏁版嵁綾誨瀷錛岀劧鑰岋紝鍗充嬌鏄繖鏍鳳紝鍦ㄩ潰瀵規寚閽堝拰寮曠敤鐨勬椂鍊欙紝鎯呭喌涔熶細鍙樺緱澶嶆潅璧鋒潵銆?br />
浣跨敤鍓嶉潰鎴戜滑宸茬粡瀹屾垚鐨勫畯lua_register_directclosure鍙兘娉ㄥ唽by value褰㈠紡鐨勫弬鏁扮殑鍑芥暟錛屽綋鍙傛暟涓瓨鍦ㄦ寚閽堝拰寮曠敤鐨勬椂鍊欙紙鍐嶅己璋冧竴嬈★紝鐩墠鍙拡瀵瑰熀鏈暟鎹被鍨嬶級錛?br />
1銆?濡傛灉鏄竴涓寚閽堬紝閫氬父瀹炵幇鍑芥暟鐨勬剰鍥炬槸浠ヨ繖涓寚閽堜紶閫掑嚭涓涓粨鏋滄潵銆?br />2銆?濡傛灉鏄竴涓紩鐢紝鍚屼笂銆?br />3銆?濡傛灉鏄竴涓猚onst鎸囬拡錛岄氬父鍙湁闈㈠char*鐨勬椂鍊欐墠浣跨敤const錛屽疄鐜板嚱鏁扮殑鎰忓浘鏄紝涓嶄細鏀瑰彉榪欎釜鍙傛暟鐨勫唴瀹廣傚叾瀹冩儏鍐典竴鑸兘閬垮厤鍑虹幇浣跨敤const鎸囬拡銆?br />4銆?濡傛灉鏄竴涓猚onst寮曠敤錛屽浜庡熀鏈暟鎹被鍨嬫潵璇達紝涓鑸兘閬垮厤鍑虹幇榪欑鎯呭喌銆?br />
Lua鍜宑pp閮藉厑璁稿嚱鏁扮敤鏌愮鏂瑰紡榪斿洖澶氫釜鍊鹼紝瀵逛簬cpp鏉ヨ錛屽涓繑鍥炲兼槸閫氳繃涓婅堪鐨勭1鍜岀2縐嶆儏鍐佃繑鍥炵殑錛屽浜巐ua鏉ヨ錛屽涓繑鍥炲煎彲浠ョ洿鎺ヨ繑鍥烇細

--in Lua
function swap(x, y)
tmp = x
x = y
y = tmp

return x, y
end

x = 100
y = 200

x, y = swap(x, y)

print(x..y)

紼嬪簭杈撳嚭錛?00100

鍚屾牱鐨勶紝鍦ㄤ富鏈虹▼搴忎腑錛屾垜浠篃鍙互鍚慙ua榪斿洖澶氫釜鍊鹼細

int swap(lua_State *L)
{
//鍙栧緱涓や釜鍙傛暟
int x = Get(TypeWrapper<int>(), L, -1);
int y = Get(TypeWrapper<int>(), L, -2);

//浜ゆ崲鍊?br />int tmp = x;
x = y;
y = tmp;

//鍚慙ua榪斿洖鍊?br />Push(L, x);
Push(L, y);

聽 //鍛婅瘔Lua鎴戜滑榪斿洖浜嗗灝戜釜鍊?br />return 2;
}

鐜板湪鎴戜滑鍙互鍦↙ua涓繖鏍瘋皟鐢ㄨ繖涓嚱鏁幫細

x = 100
y = 200

x, y = swap(x, y)

鍦ㄦ垜浠殑register_proxy鍑芥暟涓彧鑳藉鍩烘湰鏁版嵁綾誨瀷鐨刡y value鏂瑰紡鏈夋晥錛屾牴鎹垜浠笂闈㈢殑鍒嗘瀽錛屽鏋滄垜浠兘澶熷湪緙栬瘧鏈熺煡閬擄紝瀵逛簬涓涓ā鏉垮弬鏁癟錛?br />1銆?榪欐槸涓涓熀鏈殑鏁版嵁綾誨瀷錛岃繕鏄竴涓敤鎴瘋嚜瀹氫箟鐨勬暟鎹被鍨嬶紵
2銆?榪欐槸涓涓櫘閫氱殑鎸囬拡錛岃繕鏄竴涓猧terator錛?br />3銆?榪欐槸涓涓紩鐢ㄥ悧錛?br />4銆?榪欐槸涓涓猚onst 鏅氭寚閽堝悧錛?br />5銆?榪欐槸涓涓猚onst 寮曠敤鍚楋紵

濡傛灉鎴戜滑鑳界煡閬撹繖浜涳紝閭d箞錛屾牴鎹垜浠笂闈㈢殑鍒嗘瀽錛屾垜浠笇鏈涳細錛堝彧閽堝鍩烘湰鏁版嵁綾誨瀷錛?br />1銆?濡傛灉榪欐槸涓涓寚閽堬紝鎴戜滑甯屾湜鎶婃寚閽堟墍鎸囩殑鍐呭榪斿洖緇橪ua銆?br />2銆?濡傛灉榪欐槸涓涓紩鐢紝鎴戜滑甯屾湜鎶婂紩鐢ㄧ殑鎸囪繑鍥炵粰Lua銆?br />3銆?濡傛灉榪欐槸const鎸囬拡錛屾垜浠笇鏈涘皢浠嶭ua鏍堜腑鍙栧緱鐨勫弬鏁頒紶閫?br />聽 聽 聽 聽 聽 緇欒皟鐢ㄥ嚱鏁般?br />4銆?濡傛灉榪欐槸涓涓猚onst寮曠敤錛屾垜浠篃甯屾湜鎶婁粠Lua鏍堜腑鍙栧緱鐨勫弬
聽 聽 聽 聽 聽 鏁頒紶閫掔粰璋冪敤鍑芥暟銆?/div>


swo 2006-11-01 13:49 鍙戣〃璇勮
]]>
Lua鑴氭湰璇█鍏ラ棬錛堣漿杞斤級http://www.shnenglu.com/swo2006/archive/2006/11/01/14474.htmlswoswoWed, 01 Nov 2006 05:40:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14474.htmlhttp://www.shnenglu.com/swo2006/comments/14474.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14474.html#Feedback0http://www.shnenglu.com/swo2006/comments/commentRss/14474.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14474.html Lua鑴氭湰璇█鍏ラ棬
浣滆咃細 娌愭灚

Lua 紼嬪簭璁捐鍒濇

浣滆咃細 娌愭灚 錛堢浜屼漢鐢熸垚鍛橈級
鐗堟潈鎵鏈夎漿杞借娉ㄦ槑鍘熷嚭澶?br />
銆銆鍦ㄨ繖綃囨枃绔犱腑錛屾垜鎯沖悜澶у浠嬬粛濡備綍榪涜Lua紼嬪簭璁捐銆傛垜鍋囪澶у閮藉榪囪嚦灝戜竴闂ㄧ紪紼嬭璦錛屾瘮濡侭asic鎴朇錛岀壒鍒槸C銆傚洜涓篖ua鐨勬渶澶х敤閫旀槸鍦ㄥ涓葷▼搴忎腑浣滀負鑴氭湰浣跨敤鐨勩?br />銆銆Lua 鐨勮娉曟瘮杈冪畝鍗曪紝瀛︿範璧鋒潵涔熸瘮杈冪渷鍔涳紝浣嗗姛鑳藉嵈騫朵笉寮便?br />銆銆鍦↙ua涓紝涓鍒囬兘鏄彉閲忥紝闄や簡鍏抽敭瀛椼傝璁頒綇榪欏彞璇濄?br />
I. 棣栧厛鏄敞閲?br />銆銆鍐欎竴涓▼搴忥紝鎬繪槸灝戜笉浜嗘敞閲婄殑銆?br />銆銆鍦↙ua涓紝浣犲彲浠ヤ嬌鐢ㄥ崟琛屾敞閲婂拰澶氳娉ㄩ噴銆?br />銆銆鍗曡娉ㄩ噴涓紝榪炵畫涓や釜鍑忓彿"--"琛ㄧず娉ㄩ噴鐨勫紑濮嬶紝涓鐩村歡緇埌琛屾湯涓烘銆傜浉褰撲簬C++璇█涓殑"http://"銆?br />銆銆澶氳娉ㄩ噴涓紝鐢?--[["琛ㄧず娉ㄩ噴寮濮嬶紝騫朵笖涓鐩村歡緇埌"]]"涓烘銆傝繖縐嶆敞閲婄浉褰撲簬C璇█涓殑"/*鈥?/"銆傚湪娉ㄩ噴褰撲腑錛?[["鍜?]]"鏄彲浠ュ祵濂楃殑銆?br />II. Lua緙栫▼
銆銆緇忓吀鐨?Hello world"鐨勭▼搴忔繪槸琚敤鏉ュ紑濮嬩粙緇嶄竴縐嶈璦銆傚湪Lua涓紝鍐欎竴涓繖鏍風殑紼嬪簭寰堢畝鍗曪細
銆銆print("Hello world")
銆銆鍦↙ua涓紝璇彞涔嬮棿鍙互鐢ㄥ垎鍙?錛?闅斿紑錛屼篃鍙互鐢ㄧ┖鐧介殧寮銆備竴鑸潵璇達紝濡傛灉澶氫釜璇彞鍐欏湪鍚屼竴琛岀殑璇濓紝寤鴻鎬繪槸鐢ㄥ垎鍙烽殧寮銆?br />銆銆Lua 鏈夊ソ鍑犵紼嬪簭鎺у埗璇彞錛屽錛?br />
銆銆鏉′歡鎺у埗錛歩f 鏉′歡 then 鈥?elseif 鏉′歡 then 鈥?else 鈥?end
銆銆While寰幆錛歸hile 鏉′歡 do 鈥?end
銆銆Repeat寰幆錛歳epeat 鈥?until 鏉′歡
銆銆For寰幆錛歠or 鍙橀噺 = 鍒濆鹼紝緇堢偣鍊鹼紝姝ヨ繘 do 鈥?end
銆銆For寰幆錛歠or 鍙橀噺1錛屽彉閲?錛屸?錛屽彉閲廚 in琛ㄦ垨鏋氫婦鍑芥暟 do 鈥?end

銆銆娉ㄦ剰涓涓嬶紝for鐨勫驚鐜彉閲忔繪槸鍙綔鐢ㄤ簬for鐨勫眬閮ㄥ彉閲忥紝浣犱篃鍙互鐪佺暐姝ヨ繘鍊鹼紝榪欐椂鍊欙紝for寰幆浼氫嬌鐢?浣滀負姝ヨ繘鍊箋?br />銆銆浣犲彲浠ョ敤break鏉ヤ腑姝竴涓驚鐜?br />銆銆濡傛灉浣犳湁紼嬪簭璁捐鐨勫熀紜錛屾瘮濡備綘瀛﹁繃Basic錛孋涔嬬被鐨勶紝浣犱細瑙夊緱Lua涔熶笉闅俱備絾Lua鏈夊嚑涓湴鏂規槸鏄庢樉涓嶅悓浜庤繖浜涚▼搴忚璁¤璦鐨勶紝鎵浠ヨ鐗瑰埆娉ㄦ剰銆?br />
銆銆錛庤鍙ュ潡
銆銆銆銆璇彞鍧楀湪C++涓槸鐢?{"鍜?}"鎷搗鏉ョ殑錛屽湪Lua涓紝瀹冩槸鐢╠o 鍜?end 鎷搗鏉ョ殑銆傛瘮濡傦細
銆銆銆銆do print("Hello") end
銆銆銆銆浣犲彲浠ュ湪 鍑芥暟 涓拰 璇彞鍧?涓畾灞閮ㄥ彉閲忋?br />
銆銆錛庤祴鍊艱鍙?br />銆銆銆銆璧嬪艱鍙ュ湪Lua琚己鍖栦簡銆傚畠鍙互鍚屾椂緇欏涓彉閲忚祴鍊箋?br />銆銆銆銆渚嬪錛?br />銆銆銆銆a,b,c,d=1,2,3,4
銆銆銆銆鐢氳嚦鏄細
銆銆銆銆a,b=b,a -- 澶氫箞鏂逛究鐨勪氦鎹㈠彉閲忓姛鑳藉晩銆?br />銆銆銆銆鍦ㄩ粯璁ゆ儏鍐典笅錛屽彉閲忔繪槸璁や負鏄叏灞鐨勩傚亣濡備綘瑕佸畾涔夊眬閮ㄥ彉閲忥紝鍒欏湪絎竴嬈¤祴鍊肩殑鏃跺欙紝闇瑕佺敤local璇存槑銆傛瘮濡傦細
銆銆銆銆local a,b,c = 1,2,3 -- a,b,c閮芥槸灞閮ㄥ彉閲?br />
銆銆錛庢暟鍊艱繍綆?br />銆銆銆銆鍜孋璇█涓鏍鳳紝鏀寔 +, -, *, /銆備絾Lua榪樺浜嗕竴涓?^"銆傝繖琛ㄧず鎸囨暟涔樻柟榪愮畻銆傛瘮濡?^3 緇撴灉涓?, 2^4緇撴灉涓?6銆?br />銆銆銆銆榪炴帴涓や釜瀛楃涓詫紝鍙互鐢?.."榪愬絎︺傚錛?br />銆銆銆銆"This a " .. "string." -- 絳変簬 "this a string"

銆銆錛庢瘮杈冭繍綆?br />銆銆銆銆< > <= >= == ~=
銆銆銆銆鍒嗗埆琛ㄧず 灝忎簬錛屽ぇ浜庯紝涓嶅ぇ浜庯紝涓嶅皬浜庯紝鐩哥瓑錛屼笉鐩哥瓑
銆銆銆銆鎵鏈夎繖浜涙搷浣滅鎬繪槸榪斿洖true鎴杅alse銆?br />銆銆銆銆瀵逛簬Table錛孎unction鍜孶serdata綾誨瀷鐨勬暟鎹紝鍙湁 == 鍜?~=鍙互鐢ㄣ傜浉絳夎〃紺轟袱涓彉閲忓紩鐢ㄧ殑鏄悓涓涓暟鎹傛瘮濡傦細
銆銆銆銆a={1,2}
銆銆銆銆b=a
銆銆銆銆print(a==b, a~=b) -- true, false
銆銆銆銆a={1,2}
銆銆銆銆b={1,2}
銆銆銆銆print(a==b, a~=b) -- false, true

銆銆錛庨昏緫榪愮畻
銆銆銆銆and, or, not
銆銆銆銆鍏朵腑錛宎nd 鍜?or 涓嶤璇█鍖哄埆鐗瑰埆澶с?br />銆銆銆銆鍦ㄨ繖閲岋紝璇峰厛璁頒綇錛屽湪Lua涓紝鍙湁false鍜宯il鎵嶈綆椾負false錛屽叾瀹冧換浣曟暟鎹兘璁$畻涓簍rue錛?涔熸槸true錛?br />銆銆銆銆and 鍜?or鐨勮繍綆楃粨鏋滀笉鏄痶rue鍜宖alse錛岃屾槸鍜屽畠鐨勪袱涓搷浣滄暟鐩稿叧銆?br />銆銆銆銆a and b錛氬鏋渁涓篺alse錛屽垯榪斿洖a錛涘惁鍒欒繑鍥瀊
銆銆銆銆a or b錛氬鏋?a 涓簍rue錛屽垯榪斿洖a錛涘惁鍒欒繑鍥瀊

銆銆銆銆涓懼嚑涓緥瀛愶細
銆銆銆銆 print(4 and 5) --> 5
銆銆銆銆 print(nil and 13) --> nil
銆銆銆銆 print(false and 13) --> false
銆銆銆銆 print(4 or 5) --> 4
銆銆銆銆 print(false or 5) --> 5

銆銆銆銆鍦↙ua涓繖鏄緢鏈夌敤鐨勭壒鎬э紝涔熸槸姣旇緝浠や漢娣鋒揣鐨勭壒鎬с?br />銆銆銆銆鎴戜滑鍙互妯℃嫙C璇█涓殑璇彞錛歺 = a? b : c錛屽湪Lua涓紝鍙互鍐欐垚錛歺 = a and b or c銆?br />銆銆銆銆鏈鏈夌敤鐨勮鍙ユ槸錛?x = x or v錛屽畠鐩稿綋浜庯細if not x then x = v end 銆?br />
銆銆錛庤繍綆楃浼樺厛綰э紝浠庨珮鍒頒綆欏哄簭濡備笅錛?br />銆銆銆銆^
銆銆銆銆not - 錛堜竴鍏冭繍綆楋級
銆銆銆銆 * /
銆銆銆銆 + -
銆銆銆銆 ..錛堝瓧絎︿覆榪炴帴錛?br />銆銆銆銆 < > <= >= ~= ==
銆銆銆銆 and
銆銆銆銆 or

III. 鍏抽敭瀛?br />銆銆鍏抽敭瀛楁槸涓嶈兘鍋氫負鍙橀噺鐨勩侺ua鐨勫叧閿瓧涓嶅錛屽氨浠ヤ笅鍑犱釜錛?br />銆銆and break do else elseif
銆銆end false for function if
銆銆in local nil not or
銆銆repeat return then true until銆while

IV. 鍙橀噺綾誨瀷
銆銆鎬庝箞紜畾涓涓彉閲忔槸浠涔堢被鍨嬬殑鍛紵澶у鍙互鐢╰ype()鍑芥暟鏉ユ鏌ャ侺ua鏀寔鐨勭被鍨嬫湁浠ヤ笅鍑犵錛?br />
銆銆Nil 絀哄鹼紝鎵鏈夋病鏈変嬌鐢ㄨ繃鐨勫彉閲忥紝閮芥槸nil銆俷il鏃㈡槸鍊鹼紝鍙堟槸綾誨瀷銆?br />銆銆Boolean 甯冨皵鍊?br />銆銆Number 鏁板鹼紝鍦↙ua閲岋紝鏁板肩浉褰撲簬C璇█鐨刣ouble
銆銆String 瀛楃涓詫紝濡傛灉浣犳効鎰忕殑璇濓紝瀛楃涓叉槸鍙互鍖呭惈'\0'瀛楃鐨?br />銆銆Table 鍏崇郴琛ㄧ被鍨嬶紝榪欎釜綾誨瀷鍔熻兘姣旇緝寮哄ぇ錛屾垜浠湪鍚庨潰鎱㈡參璇淬?br />銆銆Function 鍑芥暟綾誨瀷錛屼笉瑕佹鐤戯紝鍑芥暟涔熸槸涓縐嶇被鍨嬶紝涔熷氨鏄錛屾墍鏈夌殑鍑芥暟錛屽畠鏈韓灝辨槸涓涓彉閲忋?br />銆銆Userdata 鍡紝榪欎釜綾誨瀷涓撻棬鐢ㄦ潵鍜孡ua鐨勫涓繪墦浜ら亾鐨勩傚涓婚氬父鏄敤C鍜孋++鏉ョ紪鍐欑殑錛屽湪榪欑鎯呭喌涓嬶紝Userdata鍙互鏄涓葷殑浠繪剰鏁版嵁綾誨瀷錛屽父鐢ㄧ殑鏈塖truct鍜屾寚閽堛?br />銆銆Thread銆銆 綰跨▼綾誨瀷錛屽湪Lua涓病鏈夌湡姝g殑綰跨▼銆侺ua涓彲浠ュ皢涓涓嚱鏁板垎鎴愬嚑閮ㄤ喚榪愯銆傚鏋滄劅鍏磋叮鐨勮瘽錛屽彲浠ュ幓鐪嬬湅Lua鐨勬枃妗c?br />
V. 鍙橀噺鐨勫畾涔?br />銆銆鎵鏈夌殑璇█錛岄兘瑕佺敤鍒板彉閲忋傚湪Lua涓紝涓嶇浣犲湪浠涔堝湴鏂逛嬌鐢ㄥ彉閲忥紝閮戒笉闇瑕佸0鏄庯紝騫朵笖鎵鏈夌殑榪欎簺鍙橀噺鎬繪槸鍏ㄥ眬鍙橀噺錛岄櫎闈烇紝浣犲湪鍓嶉潰鍔犱笂"local"銆?br />銆銆榪欎竴鐐硅鐗瑰埆娉ㄦ剰錛屽洜涓轟綘鍙兘鎯沖湪鍑芥暟閲屼嬌鐢ㄥ眬閮ㄥ彉閲忥紝鍗村繕浜嗙敤local鏉ヨ鏄庛?br />銆銆鑷充簬鍙橀噺鍚嶅瓧錛屽畠鏄ぇ灝忓啓鐩稿叧鐨勩備篃灝辨槸璇達紝A鍜宎鏄袱涓笉鍚岀殑鍙橀噺銆?br />銆銆瀹氫箟涓涓彉閲忕殑鏂規硶灝辨槸璧嬪箋?錛?鎿嶄綔灝辨槸鐢ㄦ潵璧嬪肩殑
銆銆鎴戜滑涓璧鋒潵瀹氫箟鍑犵甯哥敤綾誨瀷鐨勫彉閲忓惂銆?br />銆銆A. Nil
銆銆銆銆姝e鍓嶉潰鎵璇寸殑錛屾病鏈変嬌鐢ㄨ繃鐨勫彉閲忕殑鍊鹼紝閮芥槸Nil銆傛湁鏃跺欐垜浠篃闇瑕佸皢涓涓彉閲忔竻闄わ紝榪欐椂鍊欙紝鎴戜滑鍙互鐩存帴緇欏彉閲忚祴浠il鍊箋傚錛?br />銆銆銆銆var1=nil -- 璇鋒敞鎰?nil 涓瀹氳灝忓啓

銆銆B. Boolean
銆 銆銆銆甯冨皵鍊奸氬父鏄敤鍦ㄨ繘琛屾潯浠跺垽鏂殑鏃跺欍傚竷灝斿兼湁涓ょ錛歵rue 鍜? false銆傚湪Lua涓紝鍙湁false鍜宯il鎵嶈璁$畻涓篺alse錛岃屾墍鏈変換浣曞叾瀹冪被鍨嬬殑鍊鹼紝閮芥槸true銆傛瘮濡?錛岀┖涓茬瓑絳夛紝閮芥槸true銆備笉瑕佽 C璇█鐨勪範鎯墍璇錛?鍦↙ua涓殑鐨勭‘紜槸true銆備綘涔熷彲浠ョ洿鎺ョ粰涓涓彉閲忚祴浠oolean綾誨瀷鐨勫鹼紝濡傦細
銆銆銆銆varboolean = true

銆銆C. Number
銆銆銆銆鍦↙ua涓紝鏄病鏈夋暣鏁扮被鍨嬬殑錛屼篃涓嶉渶瑕併備竴鑸儏鍐典笅錛屽彧瑕佹暟鍊間笉鏄緢澶э紙姣斿涓嶈秴榪?00,000,000,000,000錛夛紝鏄笉浼氫駭鐢熻垗鍏ヨ宸殑銆傚湪寰堝CPU涓婏紝瀹炴暟鐨勮繍綆楀茍涓嶆瘮鏁存暟鎱€?br />銆銆銆銆瀹炴暟鐨勮〃紺烘柟娉曪紝鍚孋璇█綾諱技錛屽錛?br />銆銆銆銆4 0.4 4.57e-3 0.3e12 5e+20

銆銆D. String
銆銆銆銆瀛楃涓詫紝鎬繪槸涓縐嶉潪甯稿父鐢ㄧ殑楂樼駭綾誨瀷銆傚湪Lua涓紝浣犲彲浠ラ潪甯告柟渚跨殑瀹氫箟寰堥暱寰堥暱鐨勫瓧絎︿覆銆?br />銆銆銆銆瀛楃涓插湪Lua涓湁鍑犵鏂規硶鏉ヨ〃紺猴紝鏈閫氱敤鐨勬柟娉曪紝鏄敤鍙屽紩鍙鋒垨鍗曞紩鍙鋒潵鎷搗涓涓瓧絎︿覆鐨勶紝濡傦細
銆銆銆銆"This is a string."
銆銆銆銆鍜孋璇█鐩稿悓鐨勶紝瀹冩敮鎸佷竴浜涜漿涔夊瓧絎︼紝鍒楄〃濡備笅錛?br />銆銆銆銆\a bell
銆銆銆銆\b back space
銆銆銆銆\f form feed
銆銆銆銆\n newline
銆銆銆銆\r carriage return
銆銆銆銆\t horizontal tab
銆銆銆銆\v vertical tab
銆銆銆銆\\ backslash
銆銆銆銆\" double quote
銆銆銆銆\' single quote
銆銆銆銆\[ left square bracket
銆銆銆銆\] right square bracket

銆銆銆銆鐢變簬榪欑瀛楃涓插彧鑳藉啓鍦ㄤ竴琛屼腑錛屽洜姝わ紝涓嶅彲閬垮厤鐨勮鐢ㄥ埌杞箟瀛楃銆傚姞鍏ヤ簡杞箟瀛楃鐨勪覆錛岀湅璧鋒潵瀹炲湪鏄笉鏁㈡伃緇達紝姣斿錛?br />銆銆銆銆"one line\nnext line\n\"in quotes\", 'in quotes'"
銆銆銆銆涓澶у爢鐨?\"絎﹀彿璁╀漢鐪嬭搗鏉ュ緢鍊掕儍鍙c傚鏋滀綘涓庢垜鏈夊悓鎰燂紝閭d箞錛屾垜浠湪Lua涓紝鍙互鐢ㄥ彟涓縐嶈〃紺烘柟娉曪細鐢?[["鍜?]]"灝嗗琛岀殑瀛楃涓叉嫭璧鋒潵錛屽錛?br />銆銆銆銆page = [[
銆銆銆銆<HTML>
銆銆銆銆銆銆<HEAD>
銆銆銆銆銆銆銆銆<TITLE>An HTML Page</TITLE>
銆銆銆銆銆銆</HEAD>
銆銆銆銆銆銆<BODY>
銆銆銆銆銆銆銆銆<A HREF="http://www.lua.org">Lua</A>
銆銆銆銆銆銆銆銆[[a text between double brackets]]
銆銆銆銆銆銆</BODY>
銆銆銆銆</HTML>
銆銆銆銆]]

銆銆銆銆鍊煎緱娉ㄦ剰鐨勬槸錛屽湪榪欑瀛楃涓蹭腑錛屽鏋滃惈鏈夊崟鐙嬌鐢ㄧ殑"[["鎴?]]"灝變粛鐒跺緱鐢?\["鎴?\]"鏉ラ伩鍏嶆涔夈傚綋鐒訛紝榪欑鎯呭喌鏄瀬灝戜細鍙戠敓鐨勩?br />
E. Table
銆 銆銆銆鍏崇郴琛ㄧ被鍨嬶紝榪欐槸涓涓緢寮哄ぇ鐨勭被鍨嬨傛垜浠彲浠ユ妸榪欎釜綾誨瀷鐪嬩綔鏄竴涓暟緇勩傚彧鏄疌璇█鐨勬暟緇勶紝鍙兘鐢ㄦ鏁存暟鏉ヤ綔绱㈠紩錛涘湪Lua涓紝浣犲彲浠ョ敤浠繪剰綾誨瀷鏉? 浣滄暟緇勭殑绱㈠紩錛岄櫎浜唍il銆傚悓鏍鳳紝鍦–璇█涓紝鏁扮粍鐨勫唴瀹瑰彧鍏佽涓縐嶇被鍨嬶紱鍦↙ua涓紝浣犱篃鍙互鐢ㄤ換鎰忕被鍨嬬殑鍊兼潵浣滄暟緇勭殑鍐呭錛岄櫎浜唍il銆?br />銆銆銆銆Table鐨勫畾涔夊緢綆鍗曪紝瀹冪殑涓昏鐗瑰緛鏄敤"{"鍜?}"鏉ユ嫭璧蜂竴緋誨垪鏁版嵁鍏冪礌鐨勩傛瘮濡傦細

銆銆銆銆T1 = {} -- 瀹氫箟涓涓┖琛?br />銆銆銆銆T1[1]=10 -- 鐒跺悗鎴戜滑灝卞彲浠ヨ薄C璇█涓鏍鋒潵浣跨敤瀹冧簡銆?br />銆銆銆銆T1["John"]={Age=27, Gender="Male"}
銆銆銆銆榪欎竴鍙ョ浉褰撲簬錛?br />銆銆銆銆T1["John"]={} -- 蹇呴』鍏堝畾涔夋垚涓涓〃錛岃繕璁板緱鏈畾涔夌殑鍙橀噺鏄痭il綾誨瀷鍚?br />銆銆銆銆T1["John"]["Age"]=27
銆銆銆銆T1["John"]["Gender"]="Male"
銆銆銆銆褰撹〃鐨勭儲寮曟槸瀛楃涓茬殑鏃跺欙紝鎴戜滑鍙互綆鍐欐垚錛?br />銆銆銆銆T1.John={}
銆銆銆銆T1.John.Age=27
銆銆銆銆T1.John.Gender="Male"
銆銆銆銆鎴?br />銆銆銆銆T1.John{Age=27, Gender="Male"}
銆銆銆銆榪欐槸涓涓緢寮虹殑鐗規с?br />
銆銆銆銆鍦ㄥ畾涔夎〃鐨勬椂鍊欙紝鎴戜滑鍙互鎶婃墍鏈夌殑鏁版嵁鍐呭涓璧峰啓鍦?{"鍜?}"涔嬮棿錛岃繖鏍峰瓙鏄潪甯告柟渚匡紝鑰屼笖寰堝ソ鐪嬨傛瘮濡傦紝鍓嶉潰鐨凾1鐨勫畾涔夛紝鎴戜滑鍙互榪欎箞鍐欙細

銆銆銆銆T1=
銆銆銆銆{
銆銆銆銆銆銆10, -- 鐩稿綋浜?[1] = 10
銆銆銆銆銆銆[100] = 40,
銆銆銆銆銆銆John= -- 濡傛灉浣犲師鎰忥紝浣犺繕鍙互鍐欐垚錛歔"John"] =
銆銆銆銆銆銆{
銆銆銆銆銆銆銆銆Age=27, 聽 -- 濡傛灉浣犲師鎰忥紝浣犺繕鍙互鍐欐垚錛歔"Age"] =27
銆銆銆銆銆銆銆銆Gender=Male 聽 -- 濡傛灉浣犲師鎰忥紝浣犺繕鍙互鍐欐垚錛歔"Gender"] =Male
銆銆銆銆銆銆},
銆銆銆銆銆銆20 -- 鐩稿綋浜?[2] = 20
銆銆銆銆}

銆銆銆銆鐪嬭搗鏉ュ緢婕備寒錛屼笉鏄悧錛熸垜浠湪鍐欑殑鏃跺欙紝闇瑕佹敞鎰忎笁鐐癸細
銆銆銆銆絎竴錛屾墍鏈夊厓绱犱箣闂達紝鎬繪槸鐢ㄩ楀彿"錛?闅斿紑錛?br />銆銆銆銆絎簩錛屾墍鏈夌儲寮曞奸兘闇瑕佺敤"["鍜?]"鎷搗鏉ワ紱濡傛灉鏄瓧絎︿覆錛岃繕鍙互鍘繪帀寮曞彿鍜屼腑鎷彿錛?br />銆銆銆銆絎笁錛屽鏋滀笉鍐欑儲寮曪紝鍒欑儲寮曞氨浼氳璁や負鏄暟瀛楋紝騫舵寜欏哄簭鑷姩浠?寰鍚庣紪錛?br />
銆銆銆銆琛ㄧ被鍨嬬殑鏋勯犳槸濡傛鐨勬柟渚匡紝浠ヨ嚧浜庡父甯歌浜虹敤鏉ヤ唬鏇塊厤緗枃浠躲傛槸鐨勶紝涓嶇敤鎬鐤戯紝瀹冩瘮ini鏂囦歡瑕佹紓浜紝騫朵笖寮哄ぇ鐨勫銆?br />
銆銆F. Function
銆銆銆銆鍑芥暟錛屽湪Lua涓紝鍑芥暟鐨勫畾涔変篃寰堢畝鍗曘傚吀鍨嬬殑瀹氫箟濡備笅錛?br />銆銆銆銆function add(a,b) -- add 鏄嚱鏁板悕瀛楋紝a鍜宐鏄弬鏁板悕瀛?br />銆銆銆銆 return a+b -- return 鐢ㄦ潵榪斿洖鍑芥暟鐨勮繍琛岀粨鏋?br />銆銆銆銆end

銆銆銆銆璇鋒敞鎰忥紝return璇█涓瀹氳鍐欏湪end涔嬪墠銆傚亣濡備綘闈炶鍦ㄤ腑闂存斁涓婁竴鍙eturn錛岄偅涔堣鍐欐垚錛歞o return end銆?br />銆銆銆銆榪樿寰楀墠闈㈣榪囷紝鍑芥暟涔熸槸鍙橀噺綾誨瀷鍚楋紵涓婇潰鐨勫嚱鏁板畾涔夛紝鍏跺疄鐩稿綋浜庯細
銆銆銆銆add = function (a,b) return a+b end
銆銆銆銆褰撲綘閲嶆柊緇檃dd璧嬪兼椂錛屽畠灝變笉鍐嶈〃紺鴻繖涓嚱鏁頒簡銆備綘鐢氳嚦鍙互璧嬬粰add浠繪剰鏁版嵁錛屽寘鎷琻il 錛堣繖鏍鳳紝浣犲氨娓呴櫎浜哸dd鍙橀噺錛夈侳unction鏄笉鏄緢璞璇█鐨勫嚱鏁版寚閽堝憿錛?br />
銆銆銆銆鍜孋璇█涓鏍鳳紝Lua鐨勫嚱鏁板彲浠ユ帴鍙楀彲鍙樺弬鏁頒釜鏁幫紝瀹冨悓鏍鋒槸鐢?鈥?鏉ュ畾涔夌殑錛屾瘮濡傦細
銆銆銆銆function sum (a,b,鈥?
銆銆銆銆濡傛灉鎯沖彇寰椻︽墍浠h〃鐨勫弬鏁幫紝鍙互鍦ㄥ嚱鏁頒腑璁塊棶arg灞閮ㄥ彉閲忥紙琛ㄧ被鍨嬶級寰楀埌銆?br />銆銆銆銆濡?sum(1,2,3,4)
銆銆銆銆鍒欙紝鍦ㄥ嚱鏁頒腑錛宎 = 1, b = 2, arg = {3, 4}
銆銆銆銆鏇村彲璐電殑鏄紝瀹冨彲浠ュ悓鏃惰繑鍥炲涓粨鏋滐紝姣斿錛?br />銆銆銆銆function s()
銆銆銆銆銆銆return 1,2,3,4
銆銆銆銆end
銆銆銆銆a,b,c,d = s() -- 姝ゆ椂錛宎 = 1, b = 2, c = 3, d = 4
鍓嶉潰璇磋繃錛岃〃綾誨瀷鍙互鎷ユ湁浠繪剰綾誨瀷鐨勫鹼紝鍖呮嫭鍑芥暟錛佸洜姝わ紝鏈変竴涓緢寮哄ぇ鐨勭壒鎬ф槸錛屾嫢鏈夊嚱鏁扮殑琛紝鍝︼紝鎴戞兂鏇存伆褰撶殑搴旇璇存槸瀵硅薄鍚с侺ua鍙互浣跨敤闈㈠悜瀵硅薄緙栫▼浜嗐備笉淇★紵閭f垜涓句緥濡備笅錛?br />
銆銆銆銆t =
銆銆銆銆{
銆銆銆銆 Age = 27
銆銆銆銆 add = function(self, n) self.Age = self.Age+n end
銆銆銆銆}
銆銆銆銆print(t.Age) -- 27
銆銆銆銆t.add(t, 10)
銆銆銆銆print(t.Age) -- 37

銆銆銆銆涓嶈繃錛宼.add(t,10) 榪欎竴鍙ュ疄鍦ㄦ槸鏈夌偣鍦熷鍚э紵娌″叧緋伙紝鍦↙ua涓紝浣犲彲浠ョ畝鍐欐垚錛?br />銆銆銆銆t:add(10) 聽 -- 鐩稿綋浜?t.add(t,10)

銆銆G. Userdata 鍜?Thread
銆銆銆銆榪欎袱涓被鍨嬬殑璇濋錛岃秴鍑轟簡鏈枃鐨勫唴瀹癸紝灝變笉鎵撶畻緇嗚浜嗐?br />
VI. 緇撴潫璇?br />銆銆灝辮繖涔堢粨鏉熶簡鍚楋紵褰撶劧涓嶆槸錛屾帴涓嬫潵錛岄渶瑕佺敤Lua瑙i噴鍣紝鏉ュ府鍔╀綘鐞嗚В鍜屽疄璺典簡銆傝繖綃囧皬鏂囧彧鏄府鍔╀綘澶т綋浜嗚ВLua鐨勮娉曘傚鏋滀綘鏈夌紪紼嬪熀紜錛岀浉淇′細寰堝揩瀵筁ua涓婃墜浜嗐?br />銆銆灝辮薄C璇█涓鏍鳳紝Lua鎻愪緵浜嗙浉褰撳鐨勬爣鍑嗗嚱鏁版潵澧炲己璇█鐨勫姛鑳姐備嬌鐢ㄨ繖浜涙爣鍑嗗嚱鏁幫紝浣犲彲浠ュ緢鏂逛究鐨勬搷浣滃悇縐嶆暟鎹被鍨嬶紝騫跺鐞嗚緭鍏ヨ緭鍑恒傛湁鍏寵繖鏂歸潰鐨勪俊鎭紝浣犲彲浠ュ弬鑰冦奝rogramming in Lua 銆嬩竴涔︼紝浣犲彲浠ュ湪緗戠粶涓婄洿鎺ヨ鐪嬬數瀛愮増錛岀綉鍧涓猴細http://www.lua.org/pil/index.html
銆銆褰撶劧錛孡ua鐨勬渶寮哄ぇ鐨勫姛鑳芥槸鑳戒笌瀹夸富紼嬪簭浜茶湝鏃犻棿鐨勫悎浣滐紝鍥犳錛屼笅涓綃囨枃绔狅紝鎴戜細鍛婅瘔澶у錛屽浣曞湪浣犵殑紼嬪簭涓嬌鐢↙ua璇█浣滀負鑴氭湰錛屼嬌浣犵殑紼嬪簭鍜孡ua鑴氭湰榪涜浜や簰銆?br />---------------------------------------------------------------------------------------------------------
浣跨敤嫻佺▼
1. 鍑芥暟鐨勪嬌鐢?br />浠ヤ笅紼嬪簭婕旂ず浜嗗浣曞湪Lua涓嬌鐢ㄥ嚱鏁? 鍙婂眬閮ㄥ彉閲?br />渚媏02.lua
-- functions
function pythagorean(a, b) 聽
local c2 = a^2 + b^2 聽
return sqrt(c2)
end
print(pythagorean(3,4))

榪愯緇撴灉
5

紼嬪簭璇存槑
鍦↙ua涓嚱鏁扮殑瀹氫箟鏍煎紡涓?
function 鍑芥暟鍚?鍙傛暟)
...
end
涓嶱ascal璇█涓嶅悓, end涓嶉渶瑕佷笌begin閰嶅, 鍙渶瑕佸湪鍑芥暟緇撴潫鍚庢墦涓猠nd灝卞彲浠ヤ簡.
鏈緥鍑芥暟鐨勪綔鐢ㄦ槸宸茬煡鐩磋涓夎褰㈢洿瑙掕竟, 姹傛枩杈歸暱搴? 鍙傛暟a,b鍒嗗埆琛ㄧず鐩磋杈歸暱,
鍦ㄥ嚱鏁板唴瀹氫箟浜唋ocal褰㈠彉閲忕敤浜庡瓨鍌ㄦ枩杈圭殑騫蟲柟. 涓嶤璇█鐩稿悓, 瀹氫箟鍦ㄥ嚱鏁板唴鐨勪唬
鐮佷笉浼氳鐩存帴鎵ц, 鍙湁涓葷▼搴忚皟鐢ㄦ椂鎵嶄細琚墽琛?
local琛ㄧず瀹氫箟涓涓眬閮ㄥ彉閲? 濡傛灉涓嶅姞local鍒氳〃紺篶2涓轟竴涓叏灞鍙橀噺, local鐨勪綔鐢ㄥ煙
鏄湪鏈閲屽眰鐨別nd鍜屽叾閰嶅鐨勫叧閿瓧涔嬮棿, 濡俰f ... end, while ... end絳夈傚叏灞鍙橀噺鐨?br />浣滅敤鍩熸槸鏁翠釜紼嬪簭銆?br />
2. 寰幆璇彞
渚媏03.lua
-- Loops
for i=1,5 do 聽
print("i is now " .. i)
end

榪愯緇撴灉
i is now 1
i is now 2
i is now 3
i is now 4
i is now 5

紼嬪簭璇存槑
榪欓噷鍋朵滑鐢ㄥ埌浜唂or璇彞
for 鍙橀噺 = 鍙傛暟1, 鍙傛暟2, 鍙傛暟3 do
寰幆浣?br />end
鍙橀噺灝嗕互鍙傛暟3涓烘闀? 鐢卞弬鏁?鍙樺寲鍒板弬鏁?
渚嬪: 聽
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end

榪欓噷print("i is now " .. i)涓紝鍋朵滑鐢ㄥ埌浜?.錛岃繖鏄敤鏉ヨ繛鎺ヤ袱涓瓧絎︿覆鐨勶紝
鍋跺湪(1)鐨勮瘯璇曠湅涓彁鍒扮殑錛屼笉鐭ラ亾浣犱滑絳斿浜嗘病鏈夈?br />铏界劧榪欓噷i鏄竴涓暣鍨嬮噺錛孡ua鍦ㄥ鐞嗙殑鏃跺欎細鑷姩杞垚瀛楃涓插瀷錛屼笉闇鍋朵滑璐瑰績銆?br />
3. 鏉′歡鍒嗘敮璇彞
渚媏04.lua
-- Loops and conditionals
for i=1,5 do
print(鈥渋 is now 鈥?.. i)
聽 if i < 2 then 聽 聽
聽 print(鈥渟mall鈥? 聽
聽 elseif i < 4 then 聽 聽
聽 print(鈥渕edium鈥? 聽
聽 else 聽 聽
聽 print(鈥渂ig鈥? 聽
聽 end
end

榪愯緇撴灉
i is now 1
small
i is now 2
medium
i is now 3
medium
i is now 4
big
i is now 5
big

紼嬪簭璇存槑
if else鐢ㄦ硶姣旇緝綆鍗? 綾諱技浜嶤璇█, 涓嶈繃姝ゅ闇瑕佹敞鎰忕殑鏄暣涓猧f鍙渶瑕佷竴涓猠nd,
鍝曠敤浜嗗涓猠lseif, 涔熸槸涓涓猠nd.
渚嬪
if op == "+" then
聽 r = a + b
elseif op == "-" then
聽 r = a - b
elseif op == "*" then
聽 r = a*b
elseif op == "/" then
聽 r = a/b
else
聽 error("invalid operation")
end


4.璇曡瘯鐪?br />Lua涓櫎浜唂or寰幆浠ュ, 榪樻敮鎸佸縐嶅驚鐜? 璇風敤while...do鍜宺epeat...until鏀瑰啓鏈枃涓殑for紼嬪簭
----------------------------------------------------------------------------------------------------------
鏁扮粍鐨勪嬌鐢?br />
1.綆浠?br />Lua璇█鍙湁涓縐嶅熀鏈暟鎹粨鏋? 閭e氨鏄痶able, 鎵鏈夊叾浠栨暟鎹粨鏋勫鏁扮粍鍟?
綾誨暒, 閮藉彲浠ョ敱table瀹炵幇.

2.table鐨勪笅鏍?br />渚媏05.lua
-- Arrays
myData = {}
myData[0] = 鈥渇oo鈥?
myData[1] = 42

-- Hash tables
myData[鈥渂ar鈥漖 = 鈥渂az鈥?

-- Iterate through the
-- structure
for key, value in myData do 聽
print(key .. 鈥?鈥?.. value)
end

杈撳嚭緇撴灉
0=foo
1=42
bar=baz

紼嬪簭璇存槑
棣栧厛瀹氫箟浜嗕竴涓猼able myData={}, 鐒跺悗鐢ㄦ暟瀛椾綔涓轟笅鏍囪祴浜嗕袱涓肩粰瀹? 榪欑
瀹氫箟鏂規硶綾諱技浜嶤涓殑鏁扮粍, 浣嗕笌鏁扮粍涓嶅悓鐨勬槸, 姣忎釜鏁扮粍鍏冪礌涓嶉渶瑕佷負鐩稿悓綾誨瀷,
灝卞儚鏈緥涓竴涓負鏁村瀷, 涓涓負瀛楃涓?

紼嬪簭絎簩閮ㄥ垎, 浠ュ瓧絎︿覆鍋氫負涓嬫爣, 鍙堝悜table鍐呭鍔犱簡涓涓厓绱? 榪欑table闈炲父
鍍廠TL閲岄潰鐨刴ap. table涓嬫爣鍙互涓篖ua鎵鏀寔鐨勪換鎰忓熀鏈被鍨? 闄や簡nil鍊間互澶?

Lua瀵筎able鍗犵敤鍐呭瓨鐨勫鐞嗘槸鑷姩鐨? 濡備笅闈㈣繖孌典唬鐮?br />a = {}
a["x"] = 10
b = a 聽 -- `b' refers to the same table as `a'
print(b["x"]) --> 10
b["x"] = 20
print(a["x"]) --> 20
a = nil 聽 -- now only `b' still refers to the table
b = nil 聽 -- now there are no references left to the table
b鍜宎閮芥寚鍚戠浉鍚岀殑table, 鍙崰鐢ㄤ竴鍧楀唴瀛? 褰撴墽琛屽埌a = nil鏃? b浠嶇劧鎸囧悜table,
鑰屽綋鎵ц鍒癰=nil鏃? 鍥犱負娌℃湁鎸囧悜table鐨勫彉閲忎簡, 鎵浠ua浼氳嚜鍔ㄩ噴鏀総able鎵鍗犲唴瀛?br />
3.Table鐨勫祵濂?br />Table鐨勪嬌鐢ㄨ繕鍙互宓屽錛屽涓嬩緥
渚媏06.lua
-- Table 鈥榗onstructor鈥?
myPolygon = { 聽
color=鈥渂lue鈥? 聽
thickness=2, 聽
npoints=4; 聽
{x=0, 聽 y=0}, 聽
{x=-10, y=0}, 聽
{x=-5, y=4}, 聽
{x=0, 聽 y=4}
}

-- Print the color
print(myPolygon[鈥渃olor鈥漖)

-- Print it again using dot
-- notation
print(myPolygon.color)

-- The points are accessible
-- in myPolygon[1] to myPolygon[4]

-- Print the second point鈥檚 x
-- coordinate
print(myPolygon[2].x)

紼嬪簭璇存槑
棣栧厛寤虹珛涓涓猼able, 涓庝笂涓渚嬩笉鍚岀殑鏄紝鍦╰able鐨刢onstructor閲岄潰鏈墈x=0,y=0},
榪欐槸浠涔堟剰鎬濆憿錛?榪欏叾瀹炲氨鏄竴涓皬table, 瀹氫箟鍦ㄤ簡澶able涔嬪唴, 灝弔able鐨?br />table鍚嶇渷鐣ヤ簡.
鏈鍚庝竴琛宮yPolygon[2].x錛屽氨鏄ぇtable閲岄潰灝弔able鐨勮闂柟寮?
-----------------------------------------------------------------------------------------------------------
濡備綍綆鍖栦綘鐨勫畯.

铏界劧浠ヤ笂浠嬬粛璁╂垜浠簡瑙i亾瀹忓彲浠ュ畬鎴愰潪甯稿己澶х殑鍔熻兘錛屼絾鏆撮洩瀹炲湪澶皬姘斾簡錛屼粎浠呭彧緇欐垜浠?55涓瓧絎︽潵緙栧啓瀹忕殑鍐呭錛屽亣濡備綘鐨勫畯鐨勫姛鑳芥瘮杈冪綏鍡︼紝閭e氨寰堥夯鐑︿簡錛屾墍浠ヤ互涓嬫垜浠嬬粛涓涓嬩竴浜涚畝鍖栧畯鐨勫皬鎶宸э細

1銆佸畾涔夊叏灞鍙橀噺
鐪? 瀹屼箣鍓峀ua浠嬬粛鐨勪漢璇ラ兘鐭ラ亾鎶婏紝鍦↙ua閲岋紝鎵鏈夌殑鍙橀噺閮芥槸鍏ㄥ眬鍙橀噺錛屼篃灝辨槸璇翠換浣曚竴涓彉閲忓彧瑕佷綘鍦ㄥ紑濮嬫父鎴忓悗鍋氳繃瀹氫箟錛岄偅涔堝埌娓告垙緇撴潫鏃跺彧瑕佷綘涓嶉噸鏂? 瀹氫箟浠栭兘鏄湁鏁堢殑銆備絾涓轟簡涓嶈鎴戜滑鑷繁涓嶆販娣嗗仛鍏ㄥ眬鐢ㄧ殑鍙橀噺鍜屽眬閮ㄤ嬌鐢ㄧ殑鍙橀噺錛屾垜浠彲浠ラ噰鐢ㄥぇ灝忓啓鍖哄垎鐨勫姙娉曪紝鍗沖ぇ鍐欎竴寰嬪仛涓哄叏灞鍙橀噺浣跨敤錛屽皬鍐欓兘鐢ㄥ眬閮? 鍙橀噺銆?br />榪欐牱錛屾垜浠彲浠ュ湪涓涓畯閲屾妸鑷繁甯哥敤鐨勯瓟娉?鎶鑳介兘瀹氫箟鎴愬彉閲忔潵琛ㄧず錛屾瘮濡傛垜鏄釜鏈+錛屽氨鍙互榪欐牱錛?br />F="鑵愯殌鏈?絳夌駭 3)" 聽 X="鐚キ(絳夌駭 3)".......
涔嬪悗錛屾垜浠浣跨敤榪欐牱欖旀硶鐨勬椂鍊欙紝鍙鐩存帴鐢‵鎴朮鏉ヤ唬鏇垮氨鍙互浜嗭紝榪?"閮藉彲浠ョ渷鎺夛紝鏄笉鏄緢鏂逛究鍛
鎴栬呰繕鍙互鎶婁竴浜涘父瑙佺殑API鍑芥暟鍙橀噺涔熻嚜宸卞畾涔夛細
T="target" P="player".....
浣跨敤鐨勬椂鍊欏拰涓婇潰涓鏍楓?br />
2銆佽嚜瀹氫箟鍑芥暟
璇村疄鍦ㄨ瘽錛岄瓟鍏界殑鏈変簺鍑芥暟瀹炲湪闀跨殑榪囧ご錛屽緢澶氭椂鍊欑弽璐電殑瀛楄妭閮界粰鍑芥暟鍗犲幓浜嗐傛墍浠ュ繀瑕佺殑鏃跺欐垜浠氨寰楃敤鑷畾涔夊嚱鏁扮殑鏂規硶鍘葷畝鍖栬繖浜涘嚱鏁般?br />鑷畾涔夊嚱鏁扮殑璇彞涓猴細
function 鍑芥暟鍚嶇О(鍑芥暟鍙橀噺1銆佸嚱鏁板彉閲?....) return 鍑芥暟榪斿洖鍊?end
姣斿錛屼嬌鐢ㄦ硶鏈殑榪欎釜鍑芥暟鏄疌astByName()錛屾垜浠彲浠ュ湪瀹忛噷榪欐牱鍐欙細
/scirpt function C(a) CastByName(a) end
榪愯鍚庯紝鎴戜滑鍏朵粬瀹忎嬌鐢ㄦ硶鏈氨鍙鐩存帴鐢–()灝卞彲浠ヤ簡錛屾槸涓嶆槸寰堟柟渚垮憿錛?br />鎴栨槸璇磋瘽鐨勫嚱鏁幫細
/script function S(a) SendChatMessage(a,"SAY") end
涔嬪悗浣犺鎺у埗浜虹墿璇磋瘽灝辯敤S()灝卞彲浠ヤ簡銆?br />
濡傛灉鏄湁榪斿洖鍊肩殑鍑芥暟錛?br />/script
function N(a)
return UNitName(a) 聽 聽 聽 --return涔嬪悗灝辨槸琛ㄧず鍑芥暟鐨勮繑鍥炲?浣唕eturn蹇呴』鍦╡nd鍓嶉潰.
end
濡傛灉浠ュ悗浣犺璋冪敤鐩爣鐨勫悕瀛楋紝鐩存帴鐢?x=N("target")錛屽鏋滄寜鍓嶉潰絎竴鐐瑰畾涔変簡鍏ㄥ眬鍙橀噺鐨勮瘽錛屾洿綆鍗晉=N(T)銆?br />
榪欐牱錛屾垜浠氨鍙互鎶婇噸瑕佺殑瀛楄妭閮界敤鍦ㄥ畯鐨勫垽鏂唴瀹逛笂錛岃屼笉鏄矇闀跨殑鍑芥暟涓婁簡銆傚鏋滀綘榪樻湁浠涔堟洿濂界殑綆鍖栨柟娉曪紝鍙互璺熻創鍝︺?br />-------------------------------------------------------------------------------------------------------
鍏充簬鑳屽寘鐗╁搧浣跨敤鏁寸悊綾葷殑瀹忕殑鍒朵綔

鐢變簬娓告垙鎻愪緵鐨勫嚱鏁版棤娉曠洿鎺ョ敱鐗╁搧鍚嶇О璋冪敤璇ョ墿鍝侊紝鎵浠ラ氬父綆鍗曠殑浣跨敤鐗╁搧瀹忔槸姣旇緝楹葷儲鐨勶紝涓瀹氳鎶婁嬌鐢ㄧ殑鐗╁搧鏀懼湪鑳屽寘鍐呯壒瀹氱殑浣嶇疆

錛涙垨鍒欏ぇ澶氭湳澹兘闇瑕佺殑闂錛岃兘闅忔椂鐩戣鑷繁鐨勭伒欖傜鐗囷紙褰撶劧錛屾湁鎻掍歡鍙互鍋氬埌榪欎竴鐐癸級銆?br />
浠ヤ笅鎴戝啓鍐欏叧浜庡浣曞埗浣滆繖綾誨畯錛?br />
棣栧厛錛屾垜浠鍦ㄨ儗鍖呴噷鎵懼埌鑷繁闇瑕佺殑涓滆タ錛屽繀欏葷敤寰幆閲岄亶鍘嗚繖浜涘寘銆傜敱浜庢斁鐨勪綅緗湁2涓弬鏁幫紝1涓槸鍖呯殑緙栧彿錛屼竴涓槸鍖呭唴妲戒綅鐨勭紪鍙鳳紝

鎵浠ユ垜浠渶瑕佷竴涓驚鐜祵濂楁潵鎼滅儲錛?br />
浠ヤ笅鍋囪鎴戜滑韜笂閮芥槸16鏍肩殑鍖咃細
for bag=0,4,1 do 聽 聽 --鍖呯殑緙栧彿涓轟粠鍙沖埌宸︼紝0,1,2,3,4
for cw=1,16,1 do 聽 --妲戒綅鐨勭紪鍙蜂負涓婂埌涓嬶紝宸﹀埌鍙?1,2,3,4,5......16
.............. 聽 --榪欓噷鎴戜滑鍙互鍐欏鍒ゆ柇鐗╁搧鏄惁涓烘垜浠渶瑕佺殑涓滆タ鐨勮鍙?br />end 聽 聽 聽 聽 --琛ㄧず鍐呭驚鐜粨鏉?聽 聽 聽
end 聽 聽 聽 聽 聽 --澶栧驚鐜粨鏉?br />

鎴栬呯敤鍏朵粬鏂瑰紡鍋氳繖涓驚鐜細
While寰幆錛歸hile 鏉′歡 do 鈥?end

Repeat寰幆錛歳epeat 鈥?until 鏉′歡


鐒跺悗錛岃澶勭悊鐨勬槸鐗╁搧鐨勫垽鏂細
鎴戜滑鏈変袱涓嚱鏁板彲浠ヤ嬌鐢?br />GetContainerItemLink() 鍜?GetContainerItemInfo()
榪欎袱涓嚱鏁頒嬌鐢ㄧ殑鍙橀噺閮芥槸2涓紝涓涓槸鍖呯殑緙栧彿錛屼竴涓槸妲戒綅鐨勭紪鍙鳳紝浣嗕粬浠殑榪斿洖鍊間笉鍚?br />
GetContainerItemLink()鏄繑鍥炰竴涓甫鐫鐗╁搧鍚嶅瓧鐨勮繛鎺ワ紝濡傛灉浣犵敤鑱婂ぉ鍑芥暟鎶婅繑鍥炲艱鍑烘潵灝卞彲浠ョ湅鍒幫紝璇村嚭鏉ョ殑涓嶅厜鏄墿鍝佺殑鍚嶇О錛岃繕鏄?br />
涓涓彲浠ヨ繛鎺ュ埌鐗╁搧璇︾粏鍐呭紿楀彛鐨勮繛鎺ャ?br />
姣斿錛屼綘鐨勫寘閲?錛?鐨勪綅緗斁浜嗕竴鍧楃唺鑲夛紝閭d箞鐢?script SendChatMessage(GetContainerItemLink(4,1),"SAY")鍚庯紝灝卞彲浠ョ湅鍒拌嚜宸辮鈥淸鐔?br />
鑲塢鈥濓紝鑰屼笖鐢ㄩ紶鏍囩偣涓涓嬭鐨勫唴瀹癸紝榪樺彲浠ュ脊鍑轟竴涓弿鍐欒繖鍧楄倝鐨勭獥鍙c?br />
浣嗚娉ㄦ剰錛岀洿鎺ョ敤"[鐔婅倝]"榪欐牱瀛楃涓叉潵鍒ゆ柇榪欎釜鐗╁搧鏄笉琛岀殑錛屼緥濡傦細

if GetContainerItemLink(4,1)=="[鐔婅倝]" then ..... end
榪欎釜鍒ゆ柇鏄棤鏁堢殑銆?br />
姝g‘鐨勬柟娉曟槸錛屽厛鎶婅鍋氬垽鏂殑鐗╁搧鐨勮祴涓涓彉閲忥紝鍐嶇敤鍙橀噺鍋氬嚭鍒ゆ柇錛?br />
rou=GetContainerItemLink(4,1) 聽 聽 --鎶婄墿鍝佽繛鎺ュ艱祴緇檙ou

if GetContainerItemLink(4,1)==rou then ..... end 聽 --鐜板湪灝卞彲浠ユ甯稿垽鏂墿鍝佷簡

鏈鍚庤娉ㄦ剰鐨勬槸錛岃繖涓嚱鏁版棤娉曞鏈+鐨勭伒欖傜鐗囧仛鍑烘紜殑鍒ゆ柇錛屾剰鎬濆氨鏄紝铏界劧鐏甸瓊紕庣墖鐢ㄨ繖涓嚱鏁版樉紺哄嚭鏉ユ槸涓鏍風殑錛屼絾榪欎釜鍑芥暟鍗磋

涓烘墍鏈夌殑鐏甸瓊紕庣墖閮芥槸涓嶅悓鐨勪笢瑗匡紝鍗充綘鎶婅繖涓伒欖傜鐗囩殑榪炴帴璧嬬粰涓涓彉閲忓悗錛岃繖涓彉閲忓氨鍙兘鍒ゆ柇榪欎釜鐏甸瓊紕庣墖錛屽叾浠栫殑鐏甸瓊紕庣墖灝辨棤娉?br />
浣滃嚭鍒ゆ柇錛屽鎬妸銆傛墍浠ヨ鍒ゆ柇鐏甸瓊紕庣墖錛屽氨蹇呴』鐢ㄥ埌絎簩涓嚱鏁癎etContainerItemInfo()

GetContainerItemInfo()鐨勮繑鍥炲奸潪甯稿錛屽嚑涔庢墍鏈夌殑鐗╁搧淇℃伅閮藉彲浠ヨ繑鍥烇紝浣嗘垜浠繖閲屽垽鏂彧鐢ㄥ畠榪斿洖鐨勭涓涓箋?br />鎴戜滑鍙互鍏堢敤鑱婂ぉ鍑芥暟鏉ョ湅鐪嬬涓涓繑鍥炲兼槸浠涔堟牱瀛愮殑錛?br />/script
a=GetContainerItemInfo(4,1)
SendChatMessage(a,"SAY")

鍙互鐪嬪埌錛岃繑鍥炲肩浉褰撻暱鐨勮嫳鏂囷紝浣嗙墿鍝佺殑鍏抽敭瀛楁槸鍦ㄥ悗闈€?br />
榪欐牱錛屾垜浠氨鏈?縐嶆柟娉曟潵浣跨敤榪欎釜鍑芥暟鏉ュ垽鏂墿鍝併?br />
1銆佸拰鍓嶄竴涓嚱鏁扮殑鏂規硶涓鏍鳳紝鐢ㄥ彉閲忓瓨鍌ㄥ煎悗鍐嶅垽鏂紝鍓嶆彁鏄鎶婂垽鏂殑鐗╁搧鏀懼湪鐗瑰畾鐨勪綅緗祴涓涓嬪箋?br />2銆佸彧浣跨敤鐗瑰畾鐗╁搧錛屾妸鐗╁搧鐨勫垽鏂叧閿瓧鍐欏湪鍑芥暟閲岋紝鐒跺悗鐢╯tring.find()鏉ュ垽鏂粬銆?br />渚嬪瓙錛氭煇鐗╁搧鐨勫叧閿瓧鏄痓d
if string.find(GetContainerItemInfo(4,1),bd) then .....end --鍒ゆ柇鍖?錛?浣嶇疆鏄惁瀛樺湪鍏抽敭瀛椾負bd鐗╁搧銆?br />

鎺ョ潃瑕佸鐞嗙殑鏄墿鍝佺殑浣跨敤鍜屼氦鎹€?br />浣跨敤鐗瑰畾鑳屽寘浣嶇疆鐨勭墿鍝佸嚱鏁幫細UseContainerItem(index,slot)
榪欎釜濂界悊瑙o紝涓嶇敤澶氳В閲婁簡鎶娿?br />
鎷懼彇/鏀句笅鐗╁搧鐨勫嚱鏁幫細PickupContainerItem(index,slot)
榪欎釜鍑芥暟鏈夋剰鎬濓紝浣犻紶鏍囦笂娌℃姄鐫涓滆タ鐨勬椂鍊欏氨鏄府浣犳嬁璧風壒瀹氫綅緗殑鐗╁搧錛屾湁鐨勮瘽灝卞彉鎴愭斁涓嬬墿鍝佸埌鐗瑰畾鐨勪綅緗茍浜ゆ崲鎷胯搗璇ヤ綅緗殑鐗╁搧銆?br />
鎵浠ヨ瀹屾垚2涓墿鍝佸湪鍖呭唴鐨勪氦鎹㈣浣跨敤3嬈¤繖涓嚱鏁幫細
PickupContainerItem(4,1) --鎷胯搗4錛?浣嶇疆鐨勭墿鍝?br />PickupContainerItem(1,4) --鏀懼湪1錛?浣嶇疆騫舵嬁璧?錛?浣嶇疆鐨勭墿鍝?br />PickupContainerItem(4,1) --鎶?錛?浣嶇疆鐨勭墿鍝佹斁鍦?錛?浣嶇疆


濂芥媺錛屾妸浠ヤ笂鍑犵偣緇勫悎鍚庡畯灝卞熀鏈畬鎴愪簡錛?br />
涓嬮潰鐨勪緥瀛愭槸鍏充簬鐏甸瓊紕庣墖鐨勬暣鐞嗭紝鎶婂墠4涓寘鐨勭伒欖傜鐗囧叏鏀懼埌鏈鍚庝竴涓寘鍐咃細


/script
bag=0 cw=1 sc=1 聽 --瀹氫箟濂藉彉閲忥紝bag鏄寘鐨勭紪鍙鳳紝cw琛ㄧず鏌ユ壘鍖呯殑妲戒綅錛宻c鎸囧悜鏈鍚庝竴涓寘鍐呯殑妲戒綅
for bag=0,3,1 do --浠?鍙峰寘寮濮嬶紝鍒?鍙峰寘緇撴潫錛屾渶鍚庝竴涓寘涓嶆悳绱€?br />for cw=1,16,1 do 聽 --榪欓噷鍋囪鎵鏈夌殑鍖呴兘鏄?6涓Ы浣嶇殑錛屽鏋滄病閭d箞澶氭Ы浣嶇殑鍖呬篃鍙互鐢ㄣ?br />if GetContainerItemLink(bag,cw)~=nil --鍒ゆ柇榪欎釜妲戒綅鏄惁鏄┖鐨勶紝鏄┖灝辯洿鎺ヨ煩鍒頒笅涓涓Ы浣?br />聽 then
聽 if string.find(GetContainerItemInfo(bag,cw),"Gem") --鍒ゆ柇榪欎釜妲戒綅閲屾槸鍚︽槸鐏甸瓊紕庣墖錛孏em涓虹伒欖傜鐗囩殑鍏抽敭瀛?br />聽 then
聽 聽 while string.find(GetContainerItemInfo(4,sc),"Gem") do sc=sc+1 end
聽 聽 聽 聽 --榪欐槸涓涓皬寰幆錛岀敤浜庡垽鏂渶鍚庝竴涓寘閲屽師鏉ユ槸鍚﹀凡緇忔湁鐏甸瓊紕庣墖錛屾湁鐨勮瘽灝辨寚鍚戝寘鐨勪笅涓涓Ы浣?br />聽 聽 PickupContainerItem(bag,cw)
聽 聽 PickupContainerItem(4,sc)
聽 聽 PickupContainerItem(bag,cw) 聽 --榪?鍙ユ帶鍒剁伒欖傜鐗囧拰鏈鍚庝竴涓寘鍐呯墿鍝佺殑浜ゆ崲
聽 聽 sc=sc+1 聽 --閲嶈錛屼笉鑳藉繕璁拌繖涓紝姣忔斁緗ソ涓涓鐗囧悗灝辮鎶婃渶鍚庝竴涓寘鐨?br />聽 聽 聽 聽 妲戒綅鎸囬拡鎸囧悜涓嬩竴涓Ы浣嶏紝涓婇潰鐨勫皬寰幆鏄棤娉曞垽鏂垰鍒氭斁濂界殑紕庣墖鐨勩?br />聽 end
end
end
end 聽 -寰幆緇撴潫

瀹屼簡涔堬紝褰撶劧涓嶈銆傘傘傚洜涓哄畯鐨勯檺鍒舵槸255涓瓧銆傛墍浠ヨ綆鍖栨垜浠殑瀹忋?br />
鏈闀跨殑鍐呭浼拌灝辨槸鍑芥暟浜嗭紝灝卞厛浠庣畝鍖栧嚱鏁板紑濮嬶細

寤虹珛浠ヤ笅瀹忥細


/script function P(c,d) PickupContainerItem(c,d) end
/script function I(e,f) if GetContainerItemInfo(e,f) then return string.find(GetContainerItemInfo(e,f),"Gem") else return nil end end

鍘熸潵鐨勫畯灝卞彉鎴愪簡錛?br />
/script
bag=0 cw=1 sc=1 聽
for bag=0,3,1 do
for cw=1,16,1 do
if G(bag,cw)~=nil
聽 then
聽 if I(bag,cw)
聽 then
聽 聽 while I(4,sc) do sc=sc+1 end
聽 聽 P(bag,cw)
聽 聽 P(4,sc)
聽 聽 P(bag,cw) 聽
聽 聽 sc=sc+1 聽
聽 end 聽 聽 聽
end
end
end

澶氫綑鐨勫彉閲忓畾涔夊拰榪囬暱鐨勫彉閲忛兘鍙互鏇存敼錛?br />

/script
s=1 聽
for g=0,3 do
for w=1,16 do
if G(g,w)
聽 then
聽 if I(g,w)
聽 then
聽 聽 while I(4,s) do s=s+1 end
聽 聽 P(g,w)
聽 聽 P(4,s)
聽 聽 P(g,w) 聽
聽 聽 s=s+1 聽
聽 end 聽 聽 聽
end
end
end

鐜板湪鍐欑殑涓嬩簡鍚с傚懙鍛碉紝鑷充簬浣跨敤鐗╁搧鐨勫畯鎴戣櫧鐒跺凡緇忓啓濂戒簡錛屼絾娌℃湁嫻嬭瘯榪囷紝絳夋祴璇曟病闂鍚庡啀鏀懼嚭鏉ユ妸銆傛湁鍏磋叮鐨勬湅鍙嬩篃鍙互鑷繁鍐欏啓銆?br />
浣嗚娉ㄦ剰涓鐐癸紝浣跨敤鐗╁搧鐨勫畯鍙鎵懼埌鐗╁搧灝卞彲浠ラ┈涓婅煩鍑哄驚鐜紝鎵浠ョ敤Repeat寰幆鍋氭瘮杈冨悎閫傘?/span>


swo 2006-11-01 13:40 鍙戣〃璇勮
]]>
榪欐牱杞歡寮鍙戜漢鎵嶇駭鍒殑鍒掑垎浣犲悓鎰忓悧http://www.shnenglu.com/swo2006/archive/2006/11/01/14473.htmlswoswoWed, 01 Nov 2006 05:29:00 GMThttp://www.shnenglu.com/swo2006/archive/2006/11/01/14473.htmlhttp://www.shnenglu.com/swo2006/comments/14473.htmlhttp://www.shnenglu.com/swo2006/archive/2006/11/01/14473.html#Feedback4http://www.shnenglu.com/swo2006/comments/commentRss/14473.htmlhttp://www.shnenglu.com/swo2006/services/trackbacks/14473.html 榪欐牱杞歡寮鍙戜漢鎵嶇駭鍒殑鍒掑垎浣犲悓鎰忓悧(ZT)
鏈漢鍋氳蔣浠跺騫達紝涓鐩翠笌杞歡寮鍙戣涓氱殑鍚勭綰у埆鐨勮蔣浠跺紑鍙戜漢鎵嶆墦浜ら亾錛屽緢澶氭椂鍊欙紝

銆銆榪樻壆婕旈潰瑙嗚冨畼鐨勮鑹詫紙寰堥仐鎲撅紝鏈漢榪樻病鏈夎闈㈣瘯榪囷級銆?/p>

銆銆鍐欎笅榪欑瘒鏂囩珷錛岀洰鐨勬槸鍖哄垎鍚勭灞傛鐨勮蔣浠跺紑鍙戜漢鍛橈紝涔熻杞歡寮鍙戜漢鍛樿兘澶熷鐓ц嚜宸憋紝鐪嬬湅鑷繁鍦ㄤ粈涔堝眰嬈°?/p>

銆銆杞歡寮鍙戝伐浣滐紝鍏跺疄鏄竴縐嶅緢澶嶆潅鐨勫伐浣滐紝闇瑕佸鏂歸潰鐨勬妧鑳姐傛垜璁や負錛屽挨鍏朵互瀛︿範鑳藉姏鍜屽垱鏂拌兘鍔涗負涓匯傛墍浠ワ紝鎴戜互涓嬪杞歡浜烘墠鐨勫眰嬈″垝鍒嗭紝涔熷洿緇曡繖涓や釜鑳藉姏灞曞紑銆?/p>

銆銆涓銆侀棬澶栨眽鍨嬶細鍑犱箮娌℃湁瀛︿範鑳藉姏錛屾洿娌℃湁鍒涙柊鑳藉姏銆傛瘮濡傦紝涔頒簡涓鏈婁竴姝ヤ竴姝ヨ窡鎴戝VB緙栫▼銆嬩箣綾葷殑涔︼紝瀵圭収涔︿笂鍐欑殑錛屾妸渚嬪瓙紼嬪簭緇欏仛鍑烘潵浜嗭紝 榪樻妸渚嬪瓙紼嬪簭鐨勬煇浜涚獥鍙f爣棰樼粰淇敼浜嗕竴涓嬨傜劧鍚庯紝灝辮嚜璁や負鑷繁鍙互鍋氳蔣浠跺紑鍙戝伐浣滀簡銆傚埌澶勯掔畝鍘嗭紝搴旇仒鐨勮亴浣嶄負杞歡寮鍙戝伐紼嬪笀銆傝繖綾諱漢錛屼互鍒氭瘯涓氱殑璁$畻鏈? 涓撲笟鐨勫ぇ瀛︾敓涓哄錛堝綋鐒訛紝鍒氭瘯涓氱殑瀛︾敓涓篃鏈夐潪甯擱珮綰х殑浜烘墠錛夈傝涔︽湡闂達紝灝變互鐜╂父鎴忎負涓伙紝鑰冭瘯鐨勬椂鍊欙紝灝辨悶鐐硅垶寮婅繃鍏熾?/p>

銆銆浜屻佸叆闂ㄥ瀷錛氳綾誨瀷鐨勪漢鍛橈紙涓嶅彨浜烘墠錛屾墍浠ュ彨浜哄憳錛夛紝鍙兘鍏ラ棬鏌愪竴縐嶅埌涓ょ寮鍙戣璦錛?0騫村墠錛屾垜涓婂ぇ瀛︾殑鏃跺欙紝榪欑被浜虹殑鍏稿瀷鐗圭偣鏄儹琛蜂簬 DOS鍛戒護鐨刵縐嶇敤娉曘傛瘮濡傦紝dir鍛戒護鐨勫悇縐嶅弬鏁般傚涔犺繃basic璇█錛岀煡閬揅璇█涓璸rintf鍑芥暟鐨勫悇縐嶅弬鏁扮殑鐢ㄦ硶錛屽埌浜?005騫達紝榪欑被浜烘槸鐑? 琛蜂簬windows涓嬬殑娉ㄥ唽琛紝鐑浜庡湪鑷繁鐨勬満鍣ㄤ笂瀹夎鍚勭寮鍙戝伐鍏鳳紙VB,VC,dephi錛宎sp絳夛級銆備絾鏄紝浠呬粎鍋滅暀鍦ㄧ紪璇戝紑鍙戝伐鍏蜂腑鑷甫鐨勫嚑涓? 渚嬪瓙紼嬪簭涓傦紙鍙兘榪樹細鍋氱偣淇敼錛夈傜粡榪囦竴孌墊椂闂寸殑瀛︿範錛屽彲鑳借繕鑷繁鑳藉緙栧啓涓畝鍗曠殑windows搴旂敤紼嬪簭錛屼慨鏀規敞鍐岃〃鐨勭▼搴忕瓑絳夈傚叾寰堝鏃墮棿榪樻槸鍦ㄧ帺 娓告垙錛屼笂QQ鑱婂ぉ娉M錛岀湅浜嗕竴綃囧浣曚慨鏀規煇鐥呮瘨鐨勬枃绔狅紝涓瀹氫細瀵圭収鏂囩珷涓婄殑璇存槑錛屾妸鐥呮瘨緇欎慨鏀逛簡錛岀劧鍚庡埌澶勫彂錛屼互鏄劇ず鑷繁鐨勮兘鍔涖傚綋鐒訛紝寰堝鏃跺欙紝璇ョ被 浜哄嵆浣垮鐓ф枃绔犵殑璇存槑錛屼篃涓嶈兘灝嗙梾姣掍慨鏀廣傞偅灝辨壘閭d簺甯﹂厤緗伐鍏風殑榛戝紼嬪簭鍘誨紕鍚э紝姣斿銆侭O絳夊氨鏄粬浠渶甯哥敤鏉ョ偒鑰鐨勩備腑鍥界殑鐮磋В鑰呬笌鍒濈駭榛戝錛岀粷澶ч儴 鍒嗘槸榪欎竴綾諱漢銆傛噦鐨勪笉澶氾紝榪樺枩嬈㈢偒鑰錛堜負鐐鐩殑鐨勭牬瑙e拰淇敼鐥呮瘨灝辨槸榪欎竴綾諱漢鐨勬渶澶х壒鐐癸級銆傝綾諱漢鍛橈紝涓鑸兘娌℃湁鍦ㄨ蔣浠跺叕鍙鎬粠浜嬭蔣浠跺紑鍙戝伐浣溿?/p>

銆銆涓夈佸熀鏈瀷浜烘墠錛氳綾誨瀷涓鑸槸澶у姣曚笟錛屽茍涓斾粠浜嬭蔣浠跺紑鍙戝伐浣滆秴榪?騫寸殑浜轟負澶氾紝鑷沖皯姣旇緝鐔熸倝涓闂ㄨ璦錛堜互VB,dephi,java錛宎sp 絳夊叾涓殑涓縐嶏級銆備篃鏈夊皯鏁頒漢鐔熸倝C鎴栬匔++錛屼絾鏄鏋滄槸C鎴栬匔++錛屼竴鑸鎸囬拡絳夋蹇佃繕鏄技鎳傞潪鎳傜殑鐘舵併傚摝錛屽浜嗭紝璇ョ被浜哄憳鍙兘榪樹細鍦ㄨ嚜宸辯殑鏈哄櫒涓? 瀹夎榪噇inux鎴栬卻co unix絳夈備絾鐢變簬瀵硅嚜宸辨病鏈変俊蹇冿紝澶ч儴鍒嗕漢浼氬湪鍗婁釜鏈堜箣鍚庢妸linux鍒犻櫎銆傝綾誨瀷浜烘墠錛屾湁涓瀹氬涔犺兘鍔涖傚垱鏂拌兘鍔涗負闆躲傞傚悎鍩瑰吇鎴愪負杞歡钃濋錛屽鏋滀漢 闄呬氦寰鑳藉姏榪樺彲浠ョ殑璇濓紝鍙互鍩瑰吇鎴愪負涓涓垵綰ц惀閿浜哄憳銆傝綾誨瀷鐨勪漢鍏稿瀷鐨勭壒鐐規槸錛氫綘瑕佷粬鍋氫釜欏圭洰錛屼粬棣栧厛灝變細闂細鐢ㄤ粈涔堣璦錛燂紙鍥犱負鐢ㄤ粬涓嶇啛鎮夌殑璇█瀵逛粬 鏉ヨ錛屼粬灝辨病鏈変俊蹇冿級錛岃綾諱漢鍛橈紝涔犳儻鐪嬩腑鏂囨枃妗o紝涓嶅緱浠ョ殑鎯呭喌涓嬶紝鎵嶄細鐪嬭嫳鏂囨枃妗c傚彟澶栵紝璇ョ被浜哄憳錛屽枩嬈㈣喘涔拌蔣浠跺紑鍙戠被鐨勪功綾嶃傝綾諱漢鍛橈紝涓鑸湪杞歡鍏? 鍙鎬粠浜嬭蔣浠跺紑鍙戝伐浣滐紝寰呴亣鍦?000鍏冨埌10000鍏冧互涓嬩負涓匯?/p>

銆銆鍥涖佺啛緇冨伐錛氳綾誨瀷涓鑸槸姣曚笟5騫村茍涓鐩翠粠浜嬭蔣浠跺紑鍙戝伐浣滐紝鑷沖皯鐔熸倝 VB,asp ,鐔熸倝鏁版嵁搴擄紝鐭ラ亾浠涔堝彨瀛樺偍榪囩▼錛屼粈涔堝彨瑙﹀彂鍣ㄣ傜煡閬撹蔣浠跺伐紼嬬鐞嗙殑鍩烘湰姒傚康錛屽鏋滃仛闈㈣薄瀵硅薄寮鍙戯紝鍙兘榪樹細鐢ㄥ埌Rose絳夊伐鍏楓傛湁榪?0浜轟互涓嬭蔣浠墮」 鐩鐞嗙殑緇忛獙銆傚浜巐inux錛岃嚦灝戠煡閬撴槸涓紑婧愮殑欏圭洰銆傜敱浜庡仛榪囨瘮杈冨ぇ鐨勮蔣浠墮」鐩紝欏圭洰涓甫鐨勫皬鍏典竴鑸兘涓嶅叿澶噓nix涓嬬殑寮鍙戠粡楠岋紝鎵浠ワ紝欏圭洰涓毦鍏? 浼氬嚭鐜伴渶瑕佸湪unix涓嬭繍琛岀殑浠g爜錛屾墍浠ワ紝灝辮嚜宸卞姩鎵嬨傜敤c緙栧啓榪囧嚑孌礥nix涓嬬殑灝忕▼搴忋傚涔犺兘鍔涙瘮杈冨己錛岃綾諱漢鍛橈紝宸茬粡涔犳儻鐪嬭嫳鏂囨枃妗o紝鏈夋椂鍊欑湅緲昏瘧 鐨勫埆鎵殑涓枃鏂囨。浼氳寰椾笉鐖姐傚共鑴嗗氨鎵捐嫳鏂囨枃妗c傝綾諱漢鍛橈紝鏄惁鍠滄涔頒功涓嶅緱鑰岀煡錛屽鏋滃枩嬈拱涔︼紝涓鑸互闈炶蔣浠跺紑鍙戠被涔︾睄涓轟富浜嗐傚湪鎶鏈夊瀷鏂歸潰鍏峰涓瀹? 鐨勫垱鏂拌兘鍔涳紝鑷沖皯錛屼綘鍙粬鍋氫竴涓姤琛ㄧ▼搴忥紝浠栦細鑰冭檻鐢‥xcel鐨凜OM瀵硅薄鏉ュ疄鐜般傚浗鍐呰蔣浠跺叕鍙鎬腑鐨勯」鐩粡鐞嗭紝緇濆ぇ閮ㄥ垎鏄繖涓綾誨瀷鐨勪漢鎵嶃傚緟閬囦竴鑸湪 6000鍒?5000鍏冨乏鍙熾?/p>

銆銆浜斻佽仾鏄庡瀷錛氳綾諱漢鍛樼殑宸ヤ綔緇忓巻涓嶉噸瑕侊紝鍙互鏄繕娌℃瘯涓氱殑瀛︾敓錛屼篃鍙互鏄伐浣滀簡10騫寸殑鑰侀笩錛?鍛ㄥ唴錛堢敋鑷充竴灝忔椂錛夊氨鐔熸倝浜嗕竴闂ㄨ璦錛屽茍涓斿彲浠? 寮濮嬬敤璇ヨ璦寮鍙戯紝璇ョ被浜哄憳錛岀敱浜庡涔犺兘鍔涘緢寮猴紝鐭椂闂村唴灝辯啛鎮変簡璁稿璇█錛屽嵆浣夸粠鏉ユ病鐢ㄨ繃璇ヨ璦錛屼篃鏁簬鍦ㄨ璇█涓婅繘琛岃蔣浠跺紑鍙戯紝閫夋嫨浠涔堟牱鐨勮璦錛屼笉 鍦ㄤ簬瀛︽病瀛﹁繃錛岃屽湪浜庢槸鍚﹂傚悎瑙e喅褰撳墠闂銆傚鎶鏈厖婊″ソ濂囦笌嬋鎯咃紝涓句釜渚嬪瓙錛屽鏋滆綾諱漢鍛樻帴瑙﹁繃linux錛岄┈涓婂氨浼氳Linux鐨勯瓍鍔涙墍鍚稿紩銆傚嵆浣夸笌 鑷繁鐨勫伐浣滄棤鍏籌紝涔熶細涓鍥炲灝辯爺絀秎inux錛屽彲浠ヨ偗瀹氱殑鏄紝璇ョ被浜哄憳鐨勭瑪璁版湰鐢佃剳涓婏紝鑲畾瀹夎鏈塴inux 錛屽茍涓旓紝linux鐨勫惎鍔ㄦ鏁板拰windows鐨勫惎鍔ㄦ鏁頒竴鏍峰鐢氳嚦鏇村銆傚鏋滆綾諱漢鍛樻帴瑙﹀埌浜嗕漢宸ユ櫤鑳斤紝鑷沖皯浼氱紪鍐欎竴涓帹鐞嗘満紼嬪簭鏉ョ敤鐢ㄣ傚彟澶栵紝璇ョ被鍨? 浜烘墠鐨勫吀鍨嬬壒鐐規槸瀛︿範鑳藉姏瓚呭己銆傝嫳璇笉涓瀹氬緢鍘夊錛屼絾鏄紝涓嶅鎬曠湅鑻辨枃璧勬枡銆傝綾誨瀷浜烘墠錛岃澶氬茍涓嶆槸璁$畻鏈轟笓涓氭瘯涓氾紝鍙互鏄鏁板鐨勶紝鐗╃悊鐨勶紝闊充箰鐨勭瓑絳? 閮芥湁鍙兘銆傛垜灝辮榪囦竴涓鑻辮鐨勫鐢熷睘浜庤繖縐嶇被鍨嬨傝綾誨瀷鐨勪漢鎵嶏紝鍑犱箮鎵鏈夌殑鐥呮瘨浠g爜鏄粬浠啓鍑烘潵鐨勶紙涓嶇畻閭d簺淇敼鐥呮瘨浠g爜鐨勪漢錛夈傜埍琛ㄧ幇錛屼篃鏄粬浠殑鐗? 鐐廣傚鏋滆綾諱漢鍛樺湪璇諱功錛岄偅涔堬紝浠栦滑鏄蔣浠跺叕鍙擱潚鐫愮殑浜烘墠錛岀粷瀵逛笉浼氬嚭鐜扮畝鍘嗛掑嚭涓変喚榪樻病鏈変漢瑕佺殑鎯呭喌錛屼竴鏃﹁繘鍏ュ叕鍙革紝鍦ㄥ崐騫村唴錛屽叾鎵嶈兘涓瀹氫細寰楀埌鍏徃棰? 瀵肩殑璁ゅ彲錛屽茍浣滀負閲嶇偣鍩瑰吇瀵硅薄銆備負浜嗙暀浣忚繖鏍風殑浜烘墠錛岃蔣浠跺叕鍙鎬竴鑸細姣忓惉璇存湁鍒殑鍏徃瑕佹寲浠栫殑娑堟伅灝變細緇欎粬娑ㄥ伐璧勪竴嬈°傝柂姘寸殑澧為暱閫熷害寰寰浠ゅ悓浜嬬孩鐪箋?/p>

銆銆鍏佹妧鏈ぉ鎵嶅瀷錛氳綾諱漢鎵嶏紝鎶鏈柟闈竴嫻侊紝濡傛灉鍙粠鎶鏈柟闈㈢殑瀛︿範鑳藉姏錛屽垱鏂拌兘鍔涙潵璁詫紝閮借瓚呰繃浠ヤ笂鐨勪換浣曚竴縐嶇被鍨嬬殑浜烘墠銆備笂甯濋犱漢鎬繪槸寰堝叕騫? 鐨勶紝浠栦滑鍦ㄦ妧鏈柟闈㈡槸澶╂墠錛屽線寰鍏朵粬鏂歸潰鍑犱箮鐧界棿錛屼笉鍠勪笌浜轟氦寰錛岀敋鑷沖鎬曚笌浜轟氦寰銆傚彟澶栵紝鏌愪簺涓滆タ瀵逛粬浠湁鑷村懡鍚稿紩鍔涳紝姣斿錛屾湁浜涗漢灝辮糠鎭嬭嚜宸卞仛涓涓搷 浣滅郴緇燂紝鏈変簺浜哄氨榪鋒亱浜哄伐鏅鴻兘銆傝綾諱漢鍛橈紝涓嶅啓杞歡鍒欎互錛屼竴鍐欙紝鑲畾鏄竴嫻佺殑銆傚叏鐞冧竴嫻併備粠璇█鏉ヨ錛屽洜涓轟粬浠嚑涔庝笉鐢ㄥ井杞殑寮鍙戝伐鍏峰仛鍏蜂綋鐨勯」鐩紝浠栦滑 鎵鐪嬬殑鎶鏈祫鏂欙紝鍏ㄩ儴鏄嫳鏂囪祫鏂欙紝鍦ㄧ綉涓婁氦嫻佺殑錛屽叏鏄搷鑻辮鎴栬呮硶璇殑浜恒傚嵆浣挎槸涓浗浜猴紝浠栦滑涔熶範鎯敤鑻辮涓庡埆浜鴻繘琛屾妧鏈矡閫氥傝綾諱漢鎵嶏紝濡傛灉鍦ㄥ伐浣滐紝涓 鑸槸鍦ㄦ煇瀹為獙瀹わ紝鎴栬呮槸鍦ㄦ煇鍩洪噾鐨勮祫鍔╀笅寮灞曠爺絀訛紝濡傛灉鍦ㄨ蔣浠跺叕鍙革紝涓瀹氭槸涓繪寔涓句笘鐬╃洰鐨勮蔣浠墮」鐩傛垨鑰咃紝鍦ㄨ嚜宸卞紑鐨勫皬鍏徃鏃㈠綋CEO鍙堝綋CTO銆傜敱浜庡叾 鎶鏈殑浼樺娍鏄庢樉錛屽嵆浣夸粬涓嶆槸涓涓緢縐拌亴鐨凜EO錛屼篃鑳借榪欎釜鍏徃緇存寔涓嬪幓銆?/p>

銆銆涓冦佹暟瀛﹀鍨嬶細璇ョ被鍨嬩漢鎵嶏紝涔熻鏍規湰灝變笉鎳傚叿浣撴煇縐嶈璦鐨勫紑鍙戯紙涔熷彲浠ユ噦錛夛紝鏁村ぉ灝辯爺絀剁畻娉曘傚緩妯°備竴鑸笉灞炰簬璁$畻鏈轟笓涓氥備粬浠鎶婅嚜宸辯殑鎴愭灉 鍙樻垚鐜板疄錛屽線寰涔犳儻鎵捐仾鏄庡瀷鎴栬呭ぉ鎵嶅瀷浜烘墠甯粬浠疄鐜般傝綾諱漢鍛橈紝鍥犱負涓嶅璁$畻鏈猴紝鎵浠ワ紝鏃犳硶鎻忚堪浠栦滑鍦ㄥ涔犳妧鏈柟闈㈢殑鑳藉姏錛屼絾鏄紝鍒涙柊鑳藉姏緇濆涓嫻併傝 綾諱漢鎵嶏紝娌℃湁鍦ㄨ蔣浠跺叕鍙稿伐浣滅殑錛屽綋鐒訛紝濡傛灉鍏舵垚鏋滄湁涓瀹氬晢涓氫環鍊鹼紝浠栦滑浼氭垚涓烘煇杞歡鍏徃鐨勯【闂傛垨鑰呭共鑴嗗湪鏌愯蔣浠跺叕鍙哥殑瀹為獙瀹や腑褰撲釜涓諱換浠涔堢殑銆?/p>

銆銆鍏佹瘮灝斿瀷錛氬洜涓烘瘮灝旂殑褰卞搷鍔涘法澶э紝鎵浠ワ紝鎴戜滑鎶婂叿鏈変竴瀹氳蔣浠跺紑鍙戣兘鍔涳紝鍙堟湁寰堝己鐨勫晢涓氳繍浣滆兘鍔涚殑浜哄綊鍒拌繖涓綾匯傛瘮灝斿瀷浜烘墠錛屽涔犺兘鍔涳紝鍦ㄨ仾鏄? 鍨嬩箣涓婏紝鍦ㄦ妧鏈ぉ鎵嶅瀷涔嬩笅銆傜敱浜庤搗紺句細鐭ヨ瘑闈㈤潪甯稿箍娉涳紝鎵浠ワ紝鐭ラ亾浠涔堣蔣浠惰兘璧氶挶錛屾庝箞鏍峰仛鑳借禋閽便傝綾諱漢鍐欒蔣浠剁殑鐩殑鍙湁涓涓紝閭e氨鏄禋閽憋紝鑰屼笉浼氬お鍦? 涔庨噰鐢ㄤ粈涔堟牱鐨勬妧鏈備粬浠啓杞歡錛屼細鏋佸姏榪庡悎鐢ㄦ埛錛岃繋鍚堝競鍦恒?/p>

銆銆瀵逛漢鐨勫垝鍒嗭紝鏈夋椂鍊欐槸寰堥毦鐨勶紝鏈夌殑浜烘槸璺ㄧ被鍨嬬殑銆備絾鏄紝緙哄皯鍒涢犵殑浜猴紝鏈澶氬氨鍒拌揪鐔熺粌宸ュ瀷錛屽叿鏈夎秴寮哄垱閫犲姏鐨勪漢錛屽彲浠ヨ揪鍒版妧鏈ぉ鎵嶅瀷鍜屾暟瀛﹀ 鍨嬶紝濡傛灉榪樻湁鍟嗕笟澶磋剳錛屾垚涓烘瘮灝斿瀷涔熸槸鍙兘銆傛渶鍚庝竴鍙ヨ瘽錛屽鏋滀綘榪炶凍澶熺殑瀛︿範鑳藉姏閮芥病鏈夛紝閭d箞錛屽氨璇蜂綘紱誨紑杞歡寮鍙戣涓氾紝鍙﹁皨鍑鴻礬姣旇緝鍚堥傘?/p>

銆銆榪欑瘒甯栧瓙錛屾垜棣栧彂鍦ㄥ叡浜蔣浠惰鍧涳紝鎴戣涓猴紝濡傛灉浣犱笉鍏峰瓚呭己鐨勫涔犺兘鍔涳紝鍩烘湰鐨勫垱鏂拌兘鍔涘拰鍩烘湰鐨勫晢涓氳兘鍔涳紝閭d箞錛屽氨璇蜂綘灝芥棭涓嶈鍋氬叡浜蔣浠躲?/p>



swo 2006-11-01 13:29 鍙戣〃璇勮
]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲综合日韩中文字幕v在线| 性欧美大战久久久久久久免费观看 | 欧美国产日韩一区二区三区| 又紧又大又爽精品一区二区| 老司机精品视频网站| 久久se精品一区精品二区| 韩国三级电影一区二区| 免费在线亚洲欧美| 免费观看久久久4p| 一区二区三区日韩精品| 亚洲精品视频免费| 欧美午夜电影网| 久久久久99| 欧美高清你懂得| 午夜亚洲激情| 久久久久久97三级| 亚洲视频免费看| 性欧美大战久久久久久久免费观看| 国语自产精品视频在线看一大j8| 美女主播精品视频一二三四| 欧美伦理影院| 欧美在线看片| 榴莲视频成人在线观看| 亚洲欧美日韩网| 欧美1区2区视频| 午夜精品一区二区三区四区| 久久久爽爽爽美女图片| 中国成人亚色综合网站| 久久国产精品免费一区| 一区二区高清| 久久夜色精品国产亚洲aⅴ| 国产精品99久久99久久久二8| 欧美一区二区三区四区在线观看地址| 在线观看日韩av| 亚洲欧美成人一区二区在线电影 | 欧美日韩中国免费专区在线看| 久久成人综合网| 欧美日韩国产在线看| 久久尤物电影视频在线观看| 欧美视频日韩视频在线观看| 久久综合精品一区| 国产精品激情| 亚洲三级免费| 在线免费观看欧美| 新67194成人永久网站| 亚洲深爱激情| 欧美精品久久久久久久| 久久中文字幕一区| 国产精品素人视频| 日韩亚洲国产欧美| 亚洲精品免费在线播放| 久久婷婷国产综合国色天香| 欧美亚洲自偷自偷| 欧美亚洲成人免费| 亚洲精品综合久久中文字幕| 亚洲欧洲精品一区二区| 久久久久久久久久久一区| 欧美一区二区三区四区高清| 欧美少妇一区| 99视频超级精品| 一本色道久久综合一区| 欧美14一18处毛片| 亚洲第一伊人| 999亚洲国产精| 欧美大成色www永久网站婷| 欧美黑人多人双交| 91久久国产精品91久久性色| 久久免费少妇高潮久久精品99| 久久综合色8888| 在线观看欧美黄色| 免费不卡视频| 91久久久国产精品| 一本久道久久久| 欧美日韩裸体免费视频| 一个人看的www久久| 亚洲欧美伊人| 国产视频一区在线观看| 欧美在线日韩在线| 免费一级欧美在线大片| 亚洲片区在线| 国产精品高潮在线| 欧美一区二视频| 欧美暴力喷水在线| 日韩午夜在线视频| 国产精品嫩草99av在线| 新狼窝色av性久久久久久| 久久综合伊人77777尤物| 亚洲国产精品免费| 欧美日韩成人激情| 午夜在线电影亚洲一区| 麻豆免费精品视频| 日韩视频免费在线观看| 欧美日韩一区成人| 小嫩嫩精品导航| 亚洲国产一区二区三区a毛片| 亚洲一区二区四区| 黄色亚洲免费| 欧美精品色综合| 欧美一区二视频| 亚洲精品偷拍| 久久精品免视看| 日韩视频在线免费| 国产日韩精品久久| 欧美激情va永久在线播放| 亚洲永久免费视频| 亚洲欧洲久久| 美女视频黄免费的久久| 亚洲免费视频一区二区| 亚洲电影毛片| 国产乱码精品一区二区三| 美女诱惑一区| 欧美亚洲视频在线观看| 亚洲美女诱惑| 欧美激情偷拍| 久久精品系列| 午夜精品久久久久| 日韩视频一区二区三区在线播放| 国产午夜精品理论片a级探花| 欧美激情黄色片| 久久一区国产| 欧美一区二区在线播放| 夜夜夜久久久| 亚洲日本中文字幕区| 久久久综合香蕉尹人综合网| 亚洲在线中文字幕| 日韩一区二区免费高清| 禁久久精品乱码| 国产日韩精品视频一区二区三区| 欧美日韩精品综合| 欧美国产日本高清在线| 亚洲欧洲在线一区| 亚洲日本成人女熟在线观看| 美女精品在线观看| 欧美日韩国产成人在线| 亚洲欧美电影院| 一区二区三区在线观看视频 | 精品96久久久久久中文字幕无| 今天的高清视频免费播放成人 | 国产精品每日更新| 国产精品一区二区在线观看网站| 国产一级揄自揄精品视频| 亚洲国产小视频| 亚洲欧美日韩国产一区| 免费在线一区二区| 一区二区国产日产| 久久色在线播放| 国产精品初高中精品久久| 一区二区三区在线高清| 亚洲一区二区三区激情| 老司机一区二区三区| 一本色道久久综合一区| 久久精品最新地址| 欧美日韩一区二区在线观看视频 | 国产精品99久久久久久久久久久久 | 亚洲影音先锋| 欧美成人精品激情在线观看| 国产精品一区二区久久精品| 亚洲国产成人久久综合一区| 性欧美8khd高清极品| 亚洲欧洲精品天堂一级| 欧美在线视频a| 欧美日韩色一区| 亚洲福利视频免费观看| 欧美一区在线视频| 亚洲国产精品电影| 久久精品2019中文字幕| 国产精品女主播一区二区三区| 最近中文字幕mv在线一区二区三区四区| 欧美一区二区三区另类| 亚洲国产aⅴ天堂久久| 欧美在线视频全部完| 国产精品美女久久久久久2018 | 99精品欧美一区| 免费不卡在线观看av| 激情久久久久| 欧美在线黄色| 亚洲午夜未删减在线观看| 欧美日韩免费网站| 亚洲另类黄色| 亚洲国产美女精品久久久久∴| 久久久水蜜桃| 精品成人在线| 蜜桃av一区二区三区| 欧美在线观看视频在线| 国产日产欧美一区| 欧美一区二区视频网站| 亚洲一区二区三区免费观看| 欧美三级电影大全| 亚洲一区二区高清| 亚洲国产乱码最新视频| 美女国产一区| 最近中文字幕mv在线一区二区三区四区| 毛片一区二区| 美女视频一区免费观看| 亚洲国产精品一区二区www在线| 久久一区视频| 美女久久网站| 99视频国产精品免费观看| 亚洲啪啪91| 国产精品第三页|