Virtual Machine


Virtual Machine

The virtual-machine executes the vector of instructions produced by the compiler. The variable memory of the virtual machine is stored in some member variables of the class Cpl::VirtualMachine. These variables are:
  • vector<bool> memBool;
  • vector<double> memDouble;
  • vector<int> memInteger;
  • vector<Sys::MatrixX> memMatrix;
  • vector<valarray<double> > memVector;
  • vector<complex<double> > memComplex;
  • vector<valarray<complex<double> > >memVectorC;
  • vector<MATRIXC> memMatrixC;
  • vector<Nn::LayerNe> memLayerNet;
  • vector<Nn::LayerNetC> memLayerNetC;
  • vector<Nn::KohoNet> memKohoNet;
  • vector<Nn::ProbNet> memProbNet;

Running the virtual machine

To run code in the Wintempla virtual machine, you must:
  1. Call Cpl::VirtualMachine::Setup
  2. Call Cpl::VirtualMachine::ThreadFunc directly, or use a Mt::ThreadObject to call this function in another thread

Problem 1
Create a Wintempla dialog application called MyVirtualMachine to test the Wintempla virtual machine. 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.

tbxInputProperties

Scrollbars

ChangeEvent

MyVirtualMachine.cpp
...
void MyVirtualMachine::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 compilation errors
          tbxOutput.Text = output;
          return;
     }
     //________________________________________________ Execute the machine code
     Cpl::VirtualMachine virtualMachine;
     Mt::BoolTs cancel;
     Mt::DecimalTs progress;
     Mt::BoolTs resetTime;
     wchar_t text[256];
     virtualMachine.Setup(compiler.variableInfo, machineCode, L"dummy");
     cancel.Set(false);
     virtualMachine.ThreadFunc(cancel, progress, resetTime);
     wchar_t * errorDescr = virtualMachine.GetErrorDescr();
     //_______________________ Display execution errors
     if (errorDescr != NULL)
     {
          tbxOutput.Text = errorDescr;
          return;
     }
     //____________________________________ Display the double values
     const int doubleCount = virtualMachine.memDouble.size();
     output.clear();
     for(int i = 0; i < doubleCount; i++)
     {
          _snwprintf_s(text, 256, L"%s = %g\r\n",
               virtualMachine.GetVariableName(LEX_DATATYPE_DOUBLE, i).c_str(),
               virtualMachine.memDouble[i]);
          output += text;
     }
     tbxOutput.Text = output;
     //____________________________________ Display int values
     //...
}

MyVirtualMachineRun

© Copyright 2000-2019 Wintempla selo. All Rights Reserved. Sep 05 2019. Home