fbpx
วิกิพีเดีย

เมทาโปรแกรมมิง

เมทาโปรแกรมมิง (อังกฤษ: metaprogramming) คือเทคนิคการเขียนโปรแกรมคอมพิวเตอร์ ซึ่งจะทำงานโดยเขียนหรือจัดการโปรแกรมอื่น (หรือโปรแกรมของตัวเอง) เสมือนข้อมูลของโปรแกรม หรือสร้างงานบางส่วนขณะแปลโปรแกรมแล้วเติมเต็มงานที่เหลือขณะโปรแกรมทำงาน ในหลายกรณี เมทาโปรแกรมมิงช่วยให้โปรแกรมเมอร์สามารถประหยัดเวลาในการเขียนรหัสต้นฉบับที่เหมือนๆ กันจำนวนมากแทนที่จะเขียนทั้งหมดเองด้วยมือ อาจเรียกได้ว่าเป็นการทำงานแบบ "โปรแกรมที่เขียนโปรแกรม"

ภาษาที่ใช้เขียน เมทาโปรแกรม (metaprogram) จะถูกเรียกว่าอภิภาษา (metalanguage) และภาษาที่ถูกโปรแกรมจัดการจะเรียกว่าภาษาจุดหมาย (object language) ความสามารถของภาษาที่เป็นอภิภาษาภายในภาษาเดียวกันได้ เรียกว่าสมบัติสะท้อนของภาษาโปรแกรม (reflection/reflexivity) ซึ่งสมบัติสะท้อนเป็นคุณลักษณะหนึ่งของภาษาที่จะนำไปสู่เมทาโปรแกรมมิงได้สะดวกยิ่งขึ้น

เมทาโปรแกรมมิงมักจะมีแนวทางการเขียนหนึ่งในสองทาง ทางแรกคือการเปิดเผยโครงสร้างภายในของเอนจินขณะทำงานไปเป็นรหัสต้นฉบับผ่านทางเอพีไอ ทางที่สองคือการจัดการนิพจน์สตริงที่รวมคำสั่งไปเป็นรหัสต้นฉบับแบบพลวัต

ตัวอย่าง

ตัวอย่างต่อไปนี้เป็นเมทาโปรแกรมที่เขียนขึ้นใน bash

#!/bin/bash # metaprogram echo '#!/bin/bash' >program for ((I=1; I<=992; I++)) do echo "echo $I" >>program done chmod +x program 

สคริปต์ (หรือโปรแกรม) ข้างต้นจะสร้างสคริปต์ขึ้นมาใหม่จำนวน 993 บรรทัด เก็บไว้ในแฟ้มชื่อ "program" ซึ่งจะทำงานเป็นการพิมพ์ค่า 1 ถึง 992 ออกมา นี่เป็นเพียงตัวอย่างหนึ่งของการใช้รหัสเพื่อเขียนรหัสจำนวนมาก ไม่ใช่แนวทางที่เหมาะสมที่สุดสำหรับการพิมพ์รายชื่อจำนวน ถึงอย่างไรก็ตาม โปรแกรมเมอร์สามารถเขียนและสั่งให้เมทาโปรแกรมทำงาน และจะได้รหัสต้นฉบับเป็นจำนวนหลายพันบรรทัดภายในไม่กี่นาที

ถ้าหากโปรแกรมสามารถถูกปรับแต่งในขณะทำงานได้ (เช่น ภาษาลิสป์ ภาษาไพทอน ภาษาสมอลล์ทอล์ก ภาษารูบี ภาษาพีเอชพี ภาษาเพิร์ล และจาวาสคริปต์) เทคนิคเมทาโปรแกรมมิงจะสามารถทำงานได้ทันทีโดยไม่ต้องสร้างรหัสต้นฉบับด้วยซ้ำไป แต่ก็ไม่ใช่ว่าทุกภาษาจะทำเช่นนี้ได้

ตัวอย่างหนึ่งของเครื่องมือเมทาโปรแกรมมิงที่เป็นที่รู้จักมากที่สุดก็คือคอมไพเลอร์ (compiler) ซึ่งช่วยให้โปรแกรมเมอร์เขียนโปรแกรมขนาดเล็กได้จากภาษาโปรแกรมระดับสูง เพื่อเขียนโปรแกรมภาษาเครื่องหรือภาษาแอสเซมบลีที่เทียบเท่ากัน คอมไพเลอร์จึงกลายเป็นเครื่องมือพื้นฐานสำหรับการเขียนโปรแกรม เนื่องจากการเขียนภาษาเครื่องลงไปโดยตรงนั้นไม่มีประสิทธิภาพเมื่อนำมาใช้งานจริง

อีกตัวอย่างหนึ่งของเครื่องมือเมทาโปรแกรมมิงก็คือ lex และ yacc ซึ่งเครื่องมือสองอย่างนี้ใช้สำหรับสร้างตัววิเคราะห์ศัพท์ (lexical analyzer) และตัวแจงส่วนของศัพท์ (lexical parser) สำหรับ yacc นั้นอาจเรียกได้ว่าเป็น "คอมไพเลอร์ของคอมไพเลอร์" คือเครื่องมือที่ใช้สำหรับสร้างเครื่องมือแปลภาษาระดับสูงให้เป็นภาษาเครื่อง

ดูเพิ่ม

  • การเขียนโปรแกรมอัตโนมัติ
  • (automatic/generative programming)
  • การเขียนโปรแกรมเชิงภาษา (language-oriented programming)
  • รหัสดัดแปรตัวเอง (self-modifying code)

เมทาโปรแกรมม, บทความน, ไม, การอ, างอ, งจากแหล, งท, มาใดกร, ณาช, วยปร, บปร, งบทความน, โดยเพ, มการอ, างอ, งแหล, งท, มาท, าเช, อถ, เน, อความท, ไม, แหล, งท, มาอาจถ, กค, ดค, านหร, อลบออก, เร, ยนร, าจะนำสารแม, แบบน, ออกได, อย, างไรและเม, อไร, งกฤษ, metaprogramming, . bthkhwamniimmikarxangxingcakaehlngthimaidkrunachwyprbprungbthkhwamni odyephimkarxangxingaehlngthimathinaechuxthux enuxkhwamthiimmiaehlngthimaxacthukkhdkhanhruxlbxxk eriynruwacanasaraemaebbnixxkidxyangiraelaemuxir emthaopraekrmming xngkvs metaprogramming khuxethkhnikhkarekhiynopraekrmkhxmphiwetxr sungcathanganodyekhiynhruxcdkaropraekrmxun hruxopraekrmkhxngtwexng esmuxnkhxmulkhxngopraekrm hruxsrangnganbangswnkhnaaeplopraekrmaelwetimetmnganthiehluxkhnaopraekrmthangan inhlaykrni emthaopraekrmmingchwyihopraekrmemxrsamarthprahydewlainkarekhiynrhstnchbbthiehmuxn kncanwnmakaethnthicaekhiynthnghmdexngdwymux xaceriykidwaepnkarthanganaebb opraekrmthiekhiynopraekrm phasathiichekhiyn emthaopraekrm metaprogram cathukeriykwaxphiphasa metalanguage aelaphasathithukopraekrmcdkarcaeriykwaphasacudhmay object language khwamsamarthkhxngphasathiepnxphiphasaphayinphasaediywknid eriykwasmbtisathxnkhxngphasaopraekrm reflection reflexivity sungsmbtisathxnepnkhunlksnahnungkhxngphasathicanaipsuemthaopraekrmmingidsadwkyingkhunemthaopraekrmmingmkcamiaenwthangkarekhiynhnunginsxngthang thangaerkkhuxkarepidephyokhrngsrangphayinkhxngexncinkhnathanganipepnrhstnchbbphanthangexphiix thangthisxngkhuxkarcdkarniphcnstringthirwmkhasngipepnrhstnchbbaebbphlwttwxyang aekikhtwxyangtxipniepnemthaopraekrmthiekhiynkhunin bash bin bash metaprogram echo bin bash gt program for I 1 I lt 992 I do echo echo I gt gt program done chmod x program skhript hruxopraekrm khangtncasrangskhriptkhunmaihmcanwn 993 brrthd ekbiwinaefmchux program sungcathanganepnkarphimphkha 1 thung 992 xxkma niepnephiyngtwxyanghnungkhxngkarichrhsephuxekhiynrhscanwnmak imichaenwthangthiehmaasmthisudsahrbkarphimphraychuxcanwn thungxyangirktam opraekrmemxrsamarthekhiynaelasngihemthaopraekrmthangan aelacaidrhstnchbbepncanwnhlayphnbrrthdphayinimkinathithahakopraekrmsamarththukprbaetnginkhnathanganid echn phasalisp phasaiphthxn phasasmxllthxlk phasarubi phasaphiexchphi phasaephirl aelacawaskhript ethkhnikhemthaopraekrmmingcasamarththanganidthnthiodyimtxngsrangrhstnchbbdwysaip aetkimichwathukphasacathaechnniidtwxyanghnungkhxngekhruxngmuxemthaopraekrmmingthiepnthiruckmakthisudkkhuxkhxmiphelxr compiler sungchwyihopraekrmemxrekhiynopraekrmkhnadelkidcakphasaopraekrmradbsung ephuxekhiynopraekrmphasaekhruxnghruxphasaaexsesmblithiethiybethakn khxmiphelxrcungklayepnekhruxngmuxphunthansahrbkarekhiynopraekrm enuxngcakkarekhiynphasaekhruxnglngipodytrngnnimmiprasiththiphaphemuxnamaichngancringxiktwxyanghnungkhxngekhruxngmuxemthaopraekrmmingkkhux lex aela yacc sungekhruxngmuxsxngxyangniichsahrbsrangtwwiekhraahsphth lexical analyzer aelatwaecngswnkhxngsphth lexical parser sahrb yacc nnxaceriykidwaepn khxmiphelxrkhxngkhxmiphelxr khuxekhruxngmuxthiichsahrbsrangekhruxngmuxaeplphasaradbsungihepnphasaekhruxngduephim aekikhkarekhiynopraekrmxtonmti automatic generative programming karekhiynopraekrmechingphasa language oriented programming rhsddaeprtwexng self modifying code ekhathungcak https th wikipedia org w index php title emthaopraekrmming amp oldid 7665286, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

, อ่าน, ดาวน์โหลด, ฟรี, ดาวน์โหลดฟรี, mp3, วิดีโอ, mp4, 3gp, jpg, jpeg, gif, png, รูปภาพ, เพลง, เพลง, หนัง, หนังสือ, เกม, เกม