The Compiler |
The compiler takes the tokens produced by the lexical analyzer and compiles the code. The compiler has:
|
Compiler operation |
The compiler:
|
Compiling |
The figure below illustrates how the Wintempla compiler works. First, the lexical analyzer creates a list of tokens. Second, the compiler uses the list of tokens to create a list of instructions. Third, the virtual-machine takes the list of instructions, and stores the values in its memory. |
Problem 1 |
Create a Wintempla dialog application called MyCompiler to test the compiler. Using Wintempla add two textboxes (tbxInput and tbxOutput) with the multi-line property, vertical scrollbars and horizontal scrollbar. Set the event of "Change" in the tbxInput textbox. Set also the "Want return" property in the tbxInput textbox. |
MyCompiler.cpp |
... void MyCompiler::tbxInput_Change(Win::Event& e) { wstring output; Cpl::Compiler compiler; vector<Cpl::Compiler::Instruction> machineCode; //_______________________________________________ Compile if (compiler.Compile(tbxInput.Text.c_str(), machineCode) == true) { compiler.ListCode(machineCode, output); } else { const int count = machineCode.size(); int line; for(int i = 0; i<count; i++) { if (machineCode[i].type == VM_ERROR) { line = machineCode[i].line_number; output += (wchar_t*)machineCode[i].name; break; } } } //______________________________________________ Display tbxOutput.Text = output; } |
Tip |
When there are compilation errors, the Compile method of the class Cpl::Compiler returns false. You may inspect the vector of instructions looking for a instruction of type VM_ERROR as shown in the previous example. |