Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

สร้าง Toy Language ของคุณเองใน Ruby: คู่มือ Step-by-Step

คุณคิดว่า Programming Languages เป็น Tools ที่ Marvelous (มหัศจรรย์) แต่ Mysterious (ลึกลับ)? ถ้าคุณได้โอกาส Peek Under the Hood (ดูภายใน) และเข้าใจว่ามัน Function ยังไง? ถ้าสนใจในโอกาสที่จะ Get Your Hands Dirty (ลงมือทำจริง) และ Develop Programming Language จากศูนย์ บล็อกโพสต์นี้และโพสต์ต่อไปในซีรีส์จะช่วยได้

ในซีรีส์ Articles เราจะสร้าง Interpreted, Dynamically Typed Programming Language ที่ Simple มาก Step-by-Step แต่ตอนนี้อย่ากังวลถ้าคุณ Uncertain (ไม่แน่ใจ) เกี่ยวกับความหมายที่แน่นอนของ Terms เหล่านี้ หรือรู้สึก Intimidated (เฉยเมย/หวาดกลัว) จากภารกิจที่มีอยู่ เราจะใช้ Ruby ที่ Lovely (รัก) เพื่อ Implement Interpreter และอธิบายแต่ละขั้นตอนชัดเจน เพื่อให้ทั้ง Novice (มือใหม่) และ Advanced Developers ติดตามได้ เราจะตั้งชื่อภาษานี้ว่า 'Stoffle' เป็น Tribute (การเคารพ) ให้ Honey Badger (หมาจร) ที่น่ารักจาก South Africa ที่ชื่อนี้

ทำไมต้อง Build Programming Language?

Stoffle น่าจะไม่ Replace Python หรือ Ruby เลย แล้วทำไมต้อง Develop? นอกจาก Fun แล้ว ผมหวังจะแสดงในซีรีส์นี้ว่า Building a Language เป็น Programming Exercise ที่ยอดเยี่ยม มี Multiple Benefits:

  • มัน Demystifies (ทำให้ไม่ลึกลับ) Programming Languages (และ Developer Tools ต่อเนื่อง) และให้เราเห็นตัวเองไม่ได้แค่ Consumers ของ Tools เหล่านี้ แต่เป็น Creators ที่ Capable ของ Devising (สร้าง/คิดค้น) ของตัวเองได้ถ้า Needs หรือ Desires บังคับ;

  • Exercise นี้ Present Uncommon Programming Challenges ที่ส่วนใหญ่ของเราไม่เคย Face ใน Daily Lives;

  • เราจะเรียนรู้ Components ที่ Useful นอกเหนือจาก Programming Language Implementation Parser สามารถใช้แทน Regular Expressions ลึกลับล้านลูก เพื่อจัดการกับ Legacy Text File ปัญหาที่ Boss บอกว่าต้อง Support เป็น New Mechanism สำหรับ Import Data เข้า System ที่คุณทำงาน

Programming Language ทำงานอย่างไร?

ก่อน Look Ahead (ดูข้างหน้า) ในโปรเจกต์นี้ มา Zoom Out เข้าใจก่อนว่า Program รันบน Computer ยังไง ตามที่คุณนึก CPU ไม่ Support High-level Language เช่น Ruby โดยตรง CPUs มี Series ของ Very Low-level Instructions ที่ Support ขึ้นอยู่กับ Architecture

ถ้าสนใจ Research เช่น x86 Architecture ซึ่ง Most Likely Power เครื่องของคุณถ้าอ่านบน PC หรือ Mac

ดังนั้น Task ของ Programming Language คือ Transform High-level Code เป็น Machine Code ที่ CPU เข้าใจ Plethora (จำนวนมาก) ของ Strategies ใช้ Accomplish Task นี้ Stoffle จะเป็น Interpreted Language หมายถึง Interpreter จะ Translate Stoffle Source Code เป็น Machine Code ขณะโปรแกรม Run อยู่

Compiled Languages เช่น C ต่างออกไป มี Compilation Step Translate และ Produce Binary (Source Converted to Machine Code) พร้อม Execute โดย Target CPU Strategy อีกอย่างคือ Compile Source File เป็น Another ( 종종 High-level) Language ที่มีอยู่แล้ว Strategy นี้เรียกว่า 'Transpilation'

จำไว้ว่า Real-world Languages ไม่ Clear-cut พวกมัน Commonly Embrace (นำมาใช้) Aspects และ Techniques ของ Strategies ทั้งหมด (และอื่นๆ) ผมแนะนำ Research เพิ่มเติมเกี่ยวกับ Path ที่ Preferred Language ของคุณ Follow หลังเรา Cover Basics แล้ว

Bird's-eye View ของ Stoffle

เหมือนกล่าว Stoffle จะเป็น Elementary, Interpreted, Dynamically Typed Programming Language ประกอบด้วย Only a Few Basic Data Types, Four Elementary Arithmetic Operators, Comparison และ Equality, Logical Operators, If/Else, While Loop, Functions และ Ability to Print to Console

Trivia: Languages คล้าย Stoffle ที่ Primary Purpose เป็น Learning และ Experimentation มักเรียกว่า Toy Languages

Stoffle's Interpreter จะ Implement ด้วย Ruby ที่ Trusted และ Beloved ตามที่กล่าว เมื่อเรา Fire Up Interpreter (stoffle hello_world.sfe) Components และ Phases ที่ Source File จะผ่านก่อน Run คือ:

สร้าง Toy Language ของคุณเองใน Ruby: คู่มือ Step-by-Step

The parts of our interpreter and what happens when a .sfe file is run.

Lexer

Known as Scanner เล็กน้อย Lexer's Mission คือ Convert Plain String of Characters เป็น Sensible Groupings เรียกว่า 'Tokens' 想像เรา Declare Variable ชื่อ my_var. Lexer ของเราจะอ่าน Characters เหล่านี้และ Produce Token::VARIABLE Token

Parser

ตอนเราคิดถึง Source Code Nested Nature ของมัน Undeniable (ไม่ไหวปฏิเสธ) พิจารณา Conditional Expression ตัวอย่าง True และ False Branches Nested และ Execute ขึ้นอยู่กับ Result ของ Evaluation ของ Condition

Main Job ของ Parser คือ Transform Flat Sequence of Tokens เป็น Data Structure ที่สามารถ Represent Relationships Existing ระหว่างพวกมัน Function สำคัญอีกอย่างของ Parser คือบอกเราตอนเรา Messed Up (ทำผิด) โดย Report Syntax Errors

Interpreter

Stoffle's Interpreter จะ Simple และ Work Directly กับ Data Structure ที่ Parser Produce Interpreter จะ Analyze Structure นี้ Piece-by-piece และ Execute As It Goes

ใน Stoffle Programming Language การ Convert เป็น Machine Code จะเกิดเพราะ Interpreter เองจะเป็น Ruby Program (ดังนั้น Interpreted โดย Ruby's Interpreter ขณะเรา Run มัน!)

Wrapping up

ใน Article วันนี้ เรา Present Rough Overview ของ Steps เราจะ Take เพื่อ Bring Stoffle to Life ผมหวังว่าคุณ Excited (ตื่นเต้น) เท่าผม และผม Infuse Confidence (สร้างความเชื่อมั่น) ให้那些 Initially Thought (ที่คิดว่าเริ่มต้น) Implementing a Programming Language เป็น Something Out of Reach (ไกลเกินไป) สำหรับ Mere Mortals (คนทั่วไป)

ใน Blog Post ถัดไปในซีรีส์ เราจะ Start Getting Our Hands Dirty (ลงมือทำจริง) โดย Implement Stoffle's Lexer ซึ่งหมายถึงจบ Next Post เราจะ Develop Ruby Program ที่ Capable ของ Read Stoffle Source Code และ Transform Bland Sequence of Characters เป็น More Structured (และ Interesting!) Sequence of Tokens

See you soon ใน Next Installment ของซีรีส์นี้!