Pages

Friday 7 September 2012

computer language ကိုယ္ပိုင္ တီထြင္ျခင္း+1


Introduction

ဒီတခါ ကၽြန္ေတာ္ေရးမဲ့အေၾကာင္းအရာကေတာ့ computer language အသစ္တခုကို ကိုယ္ပိုင္ တီထြင္ျခင္းျဖစ္ ပါတယ္။ ဒီေနရာမွာ ေမးစရာ ေမးခြန္း ၂ ခုေပၚလာပါတယ္။ 
(၁) Language တစ္ခုတီထြင္ရတာ ခက္ပါသလား။
 (၂) Language အသစ္ထြင္ဖို႔လိုအပ္ပါသလား။ Language အသစ္တခုထြင္ရာမွာ ရွိၿပီးသား C လို procedural language မ်ိဳးတီထြင္ဖို႔ အရမ္းလြယ္ပါတယ္။ Coding အနည္းငယ္နဲ႔ဘဲ တည္ေဆာက္လို႔ရ ပါတယ္။


Language အသစ္တီထြင္ဖို႔ လိုအပ္ပါသလားဆိုေတာ့ ဒီေခတ္ထဲမွာေတာ့ လုံး၀နည္းပါး မလိုအပ္ပါဘူး။ ရွိၿပီးသား java လို c# လို scripting language ေတြဟာ ေတာ္ေတာ္ေလးေကာင္းၿပီး၊ ေနရာ ေတာ္ေတာ္မ်ားမ်ားမွာ (embedded) သုံးလို႔ရပါတယ္။ ေနာက္ၿပီး ကိုယ္တီထြင္မဲ့ language ဆိုတာ ဘယ္လိုလုပ္ဒီလို ကမၻာေက်ာ္ languages ေတြနဲ႔ယွဥ္လို႔ရမွာလဲ။ မဟုတ္ဘူးလား :)

ဒါေပမဲ့၊ ကိုယ္ပိုင္ language၊ compiler နဲ႔ interpreter ေတြကို ေရးႏိုင္သြားရင္၊ C# တို႕ Java တို႕ သုံးတဲ့အခါ၊ ေအာ္... ဒီ syntax ကဒီေနရာမွာ ဒါေၾကာင့္လိုအပ္တာကိုး၊ ဒီလိုေရးရင္ ဒါေၾကာင့္ error တက္တာ၊ ဒီလိုေရးရင္ Optimized ပိုျဖစ္တယ္ စသည္ျဖင့္ စဥ္းစားႏိုင္သြားပါတယ္။ Programming ဆိုတာႀကီးကို ဘာမွန္းမသိတဲ့ Black Box အျဖစ္ကေန၊ codes ေတြကို complier ကဘယ္လို compile လုပ္တယ္၊ Assembler က ဘယ္လို execute လုပ္တယ္ဆိုတာကို အေသးစိတ္သိသြားမွာ ျဖစ္ပါတယ္။ ကၽြန္ေတာ္အျမင္ကေတာ့ compiler ေရးတာကို programmer တိုင္းသိထားသင့္တယ္ ထင္ပါတယ္။ ကဲဒီေတာ့၊ ကၽြန္ေတာ္တို႕ programming language တခုကိုဘယ္လိုတည္ေဆာက္တယ္ ဆိုတာ ေလ့လာရင္း ကိုယ္ပိုင္ Programming language ေလးတခု ထြင္ေရးၾကည့္ရေအာင္။

ဒီ tutorial မွာေတာ့ programming language အသစ္ထြင္ရာမွာ C-based syntax ေတြကိုဘဲ သုံးပါမယ္။ CPU ေပၚ run တဲ့ assemblies codes ေတြထုတ္မဲ့အစား၊ Java လို ကိုယ္ပိုင္ virtual machine မွာ run မဲ့ byte-codes ေတြထုတ္ေပးတဲ့ complier မ်ိဳးေရးမွာ ျဖစ္ပါတယ္။ စာဖတ္သုူေတြ အေနနဲ႔ ဒီ tutorial series ကိုလိုက္လုပ္ရင္း ကိုယ္ေပးခ်င္တဲ့ syntax ေတြ၊ နာမည္ေတြလဲ ေပးႏိုင္ပါတယ္။ ကိုယ္ပိုင္ language ထြင္ေနတာဘဲ၊ မဟုတ္ဘူးလား? :) Tutorial ကိုလိုက္လုပ္ရင္းနဲ႔ have fun ေပါ့ ;)

Note: ခုကၽြန္ေတာ္တင္ျပမဲ့ tutorial ဟာလြယ္ေပမဲ့၊ programming ကို beginner ကစေလ့လာေနသူေတြ အတြက္ရည္ရြယ္ၿပီး ေရးထားတဲ့ basic programming tutorial ေတာ့မဟုတ္ပါ။ ဒီ tutorial ကို ဖတ္ဖို႕ programming concept အေျခခံေတာ့ ရွိဖို႕လိုပါလိမ့္မယ္။ ဒီ tutorial ကိုမဖတ္ခင္ သိထားသင့္တာေတြ ကေတာ့ stack အပါအ၀င္ အေျခခံ data structure မ်ား၊ CRT ႏွင့္ STL (Standard Template Library) မွ function မ်ား၊ vector, list ကဲ့သို႕ template class မ်ား၊ File အေရး/အဖတ္ တို႕ကို သိထားသင့္ပါတယ္။ စာဖတ္သူမ်ား သိထားၿပီးလို႔ ယူဆၿပီး ဒီ tutorial မွာအေသးစိတ္ ရွင္းျပေပးသြားမွာ မဟုတ္ပါ။

C++ ကိုအေျခခံေရးထားေပမဲ့၊ တျခား third generation languages မ်ားျဖင့္လဲ implement လုပ္ႏိုင္ပါတယ္။ Scripting language မ်ားႏွင့္ေတာ့ implement လုပ္ရန္ recommend မလုပ္ပါ။

Simplest Programming Language

ခု language အသစ္တခုဘယ္လို ေရးရမလဲဆိုတာ ေလ့လာဖို႕ ကၽြန္ေတာ္တို႕ ကမၻာေပၚမွာ အရိုးရွင္းဆုံး language တခုကို စေရးၾကည့္ရေအာင္။ ကၽြန္ေတာ္တို႕ language မွာ Op-code ၂ခုဘဲပါပါမယ္။ TALK နဲ႔ END ပါ။ ကိုယ္က TALK လို႔ေရးလိုက္ရင္ computer က "Hello, I am simplest language!" လို႔ screen ေပၚမွာ ေရးျပပါမယ္။ END လို႔ေရးလိုက္ရင္ program က exit ထြက္ပါတယ္။ ကဲ ဒီေလာက္လြယ္တဲ့ language ေလးကိုဘဲ နမူနာအေနနဲ႔ စမ္းေရးၾကည့္ရေအာင္။

Note: Op-code ဆိုတာ Assembler မွာ execute လုပ္ႏိုင္တဲ့ single instruction ပါ။ Assembly ရဲ႕ syntax က high-level language ေတြနဲ႔မတူပါဘူး။ သူမွာ Op-code ေတြကိုတန္းစီၿပီး ေရးရပါတယ္။ Op-code တခုမွာ အမ်ားအားျဖင့္ parameter သုည၊ တခု (သို႕) ႏွစ္ခုပါတတ္ပါတယ္။ ဥပမာ၊ 3 နဲ႔ 4 ေပါင္းတာ (3+4) ကို pseudo assembly နဲ႔ေအာက္ကလို ေရးပါတယ္။
1: PUSH 3
2: PUSH 4
3: ADD
4: POP


ကဲ၊ ခုကၽြန္ေတာ္တို႔ရဲ႕ simplest language ဆီျပန္သြားရေအာင္ :) ကၽြန္ေတာ္တို႔ရဲ႕ Op-code ၂ ခုကို define လုပ္လိုက္ပါ။
enum OpCode {    OP_TALK,
    OP_END

};
Op-code တခုခ်င္းဆီကို store လုပ္ဖို႔ Instruction ဆိုတဲ့ class တခုေဆာက္လိုက္ပါမယ္။ ဒါမွ Op-code တခုခ်င္းဆီနဲ႔ သက္ဆိုင္တဲ့ data ေတြကိုပါ တပါတည္း သိမ္းထားလို႔ရမွာ။

class Instruction {public:
    OpCode    code;

    Instruction(OpCode _c) : code(_c) { };

};
ေလာေလာဆယ္ေတာ့ OpCode ဆိုတဲ့ data field တခုဘဲရွိပါေသးတယ္။ ဘာလို႕လဲဆိုေတာ့ ခုကၽြန္ေတာ္တို႕ေရးေနတာ simplest language ေလ :) Parameter ေတြပါထည့္ေရးခ်င္ရင္ ဒီ Instruction class မွာဘဲ member data ေတြထပ္တိုးလိုက္ရုံပါဘဲ။

Program ေရးတဲ့အခါ Instruction က ၁ ခုထဲရွိမွာ မဟုတ္ပါဘူး။ Program ဆိုတာ Instruction ေတြအမ်ားၾကီး နဲ႔တည္ေဆာက္ထားတာပါ။ ဒီေတာ့ Instruction ေတြသိမ္းထားဖို႔ Program ဆိုတဲ့ class တခုထပ္ေဆာက္လိုက္ပါ အုံးမယ္။
class Program {public:
    vector<Instruction>    InstructionList;

    int            nCurrent;

    Program() : nCurrent(0) {};

};
ဒီ Program class ရဲ႕တာ၀န္က instructions ေတြကို store လုပ္ဖို႔နဲ႔ retrieve လုပ္ဖို႔္ပါ။ ဒီေတာ့ store လုပ္ဖို႔အတြက္ function နဲ႔ retrieve လုပ္ဖို႔ function ၂ခုေရးလိုက္ပါမယ္။

class Program {public:
    vector<Instruction>        InstructionList;

    int                nCurrent;

    Program() : nCurrent(0) {};



    // Add a new instruction to the program

    void Add(Instruction inst)    {

        InstructionList.push_back(inst);

    };



    // Retrieve current instruction and move to next instruction

    Instruction Next() {

        return InstructionList[nCurrent++];

    };

};
Program ဆိုတဲ့ class က instruction ေတြကို store လုပ္ရုံပါ။ Instruction ေတြ run ဖို႔အတြက္ Virtual Machine ဆိုတဲ့ class တခုထပ္ေဆာက္လိုက္ပါမယ္။ Virtual Machine ကို interpreter လို႕လဲေခၚပါတယ္။ ဒါက Java မွာပါတဲ့ Virtual Machine နဲ႔သေဘာတရားျခင္း အတူတူပါဘဲ။ ဒီ Virtual Machine ကကၽြန္ေတာ္တို႔ရဲ႕ instruction တခုခ်င္းဆီကို ဖတ္ၿပီး execute လုပ္ေပးမွာ ျဖစ္ပါတယ္။ ကဲ ကၽြန္ေတာ္တို႔ရဲ႕ Virtual Machine ေလးကို ေရးၾကည့္ရေအာင္၊

class Virtual Machine {public:
    void Run(Program& program);

};
Virtual Machine မွာေတာ့ ေလာေလာဆယ္ program ကို run မဲ့ function တစ္ခုဘဲရွိပါတယ္။ ကဲ Run() function ကိုေရးၾကည့္ရေအာင္။ ကၽြန္ေတာ္တို႕မွာ instruction ၂ မ်ိဳးဘဲရွိပါတယ္။ TALK နဲ႕ END ပါ။ ဒီေတာ့ ေအာက္ကလို instruction ရဲ႕ Op-code ကိုဖတ္ၿပီး switch နဲ႕ခံၿပီး လိုခ်င္တာ ေရးလိုက္ရင္ရပါၿပီ။

switch (program.Next().code){
case OP_TALK:

    printf("Hello, I am simplest language!");

    break;

case OP_END:

    return;

}
Implementation အျပည့္အစုံကိုေအာက္မွာ ၾကည့္ပါ။
class Virtual Machine {

public:

    void Run(Program& program) {

        while (true)

        {

            switch (program.Next().code)

            {

            case OP_TALK:

                printf("Hello, I am simplest language!\n");

                break;

            case OP_END:

                return;

            }

        }

    };

};
ကဲဒါဆို ကၽြန္ေတာ္တို႕ရဲ႕ simplest language ေလးဟာၿပီးသြားပါၿပီ။ ကဲ ေရးရတာ ဘယ္ေလာက္လြယ္သလဲ ;) Simplest language ဆိုေပမဲ့ Virtual Machine ရဲ႕ run ပုံ concept အျပည့္အစုံသုံးသြားပါတယ္။ ခုေလာေလာဆယ္ ဘာမွသိပ္အလုပ္မလုပ္ေသးေပမဲ့ ေနာက္ chapter ေတြက်ရင္ ဒီ code base ကိုဘဲ တကယ့္ language ျဖစ္ေအာင္ခ်ဲ႕သြားပါမယ္။ ေလာေလာဆယ္ေတာ့ ကၽြန္ေတာ္တို႔ရဲ႕ Virtual Machine ေလးကို စမ္းၾကည့္ရေအာင္၊

VirtualMachine vm;
void main() {

    Program MyProgram;



    // my script :)

    MyProgram.Add(Instruction(OP_TALK));

    MyProgram.Add(Instruction(OP_TALK));

    MyProgram.Add(Instruction(OP_END));



    vm.Run(MyProgram);

};
ဒါဆို ကၽြန္ေတာ္တို႔ရဲ႕ "script" ကေအာက္ကလို print ထုတ္ေပးပါလိမ့္မယ္။

Hello, I am simplest language!
Hello, I am simplest language!

သိပ္ေတာ့ စိတ္လႈပ္ရွားစရာ မရွိေသးဘူး :) ဒီေနရာမွာ သတိထားစရာက OP_END ဆိုတဲ့ instruction ကို ထည့္မေပးရင္ error တက္ပါလိမ့္မယ္ (ဘာေၾကာင့္ဆိုတာ Virtual Machine ရဲ႕ Run() function ကိုျပန္ ၾကည့္ ပါ)။ ဒါက ကၽြန္ေတာ္တို႕ simplest language ေလးရဲ႕ rule တခုပါ၊ Syntax လို႔လဲ ေျပာလို႔ရပါတယ္။ ဒီေနရာမွာ၊ ဒီလို အလြယ္ဆုံး language ေလးေတာင္ syntax ဆိုတာရွိတယ္၊ ရွိတဲ့ syntax ကိုမလိုက္နာရင္ ဘာေၾကာင့္ error တက္တတ္တယ္ဆိုတာ သတိျပဳႏိုင္ပါတယ္။

ဆက္ပါအုံးမည္...

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

အေထြးေထြးနည္းပညာမ်ား