Часть кода языка программирования MLMP. - Програмування - Каталог статей - Персональный сайт
BELOSOFT
Форма входа
E-mail:
Пароль:
Поиск
Друзья сайта



Программы, новости, рефераты, скрипты, форум
Map IP Address
  Rambler's Top100
Статистика
Наш опрос
Оцените мой сайт
Всего ответов: 190
Категории каталога
Мои статьи [49]
Програмування [9]
Статті з програмування
Четверг, 17.05.2012, 00:50
Главная » Статьи » Програмування

Часть кода языка программирования MLMP.
unit MLmp;

interface

uses Counter,Memory;

const
BufSize = 250;
StackSize = 1000;

cmExit = 'Выход_из_программы';
cmGoto = 'Перейти_к';
cmEQUid = ':=';
cmvEQUid = '=';
cmbLeft = '<';
cmbRight = '>';
cmRepeat = 'Цикл';
cmWrForX = 'Записать_значение_цикла_в';
cmExWFor = 'Выход_из_цикла';

cmVar = 'Создать_переменную';

cmReal = 'Реальное_число';
cmInteger = 'Целое_число';
cmString = 'Строка_символов';
cmChar = 'Один_символ';

type
MyVar = record
Name:^String;
R:^Real;
I:^Integer;
S:^String;
C:^Char;
end;
MFinder = Array[1..StackSize] of 0..1;
BFinder = Array[1..BufSize] of 0..1;
MType = object(ThisProgram)
List:Array[1..100] of ^String;
MyMem:Array[1..1000] of ^Byte;
end;
MClass = object(ThisProgram)
List:Array[1..100] of ^String;
MyMem:Array[1..1000] of ^Byte;
Proc:Array[1..100] of ^String;
Func:Array[1..100] of ^String;
end;
COMMAND = object(ThisProgram)
ErrMsg:String;
NumRC:Byte;
Buffer:Array[1..BufSize] of String;
function GetLastCommand : String;
function GetNLastCommand(N : Byte) : String;
function RunLabel(Str:String) : Byte;
function cEQUDecode(MLT:String; S:String; var EQU:MyVar):Byte;
procedure ClearBuffer;
procedure SetCommandLine(Str:String);
procedure ReWriteBuffer(Str:String);
procedure RunBuffer;
procedure RunCommand(Str:String);
procedure MLGotoCommand(Command:String);
procedure MLForCommand(Command:String);
procedure MLWriteForIntoX(Command:String);
procedure MLExitWithFor(Command:String);
procedure MLIfCommand(Command:String);
procedure MLProcedureCommand(Command:String);
procedure MLFunctionCommand(Command:String);
procedure MLTypeCommand(Command:String);
procedure MLObjectCommand(Command:String);
procedure MLConstCommand(Command:String);
procedure MLVarCommand(Command:String);
end;
MLEQUs = object(Command)
function EQUDecode(MLT:String; S:String; var EQU:MyVar):Byte;
function RealDecode(var Str:String):Real;
function IntegerDecode(Str:String):Integer;
function BoolIfer(Str:String):Boolean;
procedure ChangeAllsVariables(var MStr:String);
procedure ChangeAllsSignAndSaves(var S:String; Ch:Char);
procedure CountAllsOperand(var S:String; R:Real);
end;
MLRepeat= record
i:Integer;
Activ:0..1;
RLabel:String;
end;
MLRep = object(ThisProgram)
RBuffer:array[1..BufSize] of MLRepeat;
function Init(S:String):Byte;
function Done(S:String):Boolean;
procedure ClearList;
end;
MMemory = object(ThisProgram)
Variables:Array[1..StackSize] of ^MyVar;
function NewVar(Name:String; NVType:String; EQU:MyVar):Word;
function NewType(Name:String; Size:Integer; List:MType):Word;
function NewClass(Name:String; Size:Integer; List:MClass):Word;
function ReWStack(Name:String; NVType:String; EQU:MyVar):Word;
procedure FindInMemory(Name:String; var F:MFinder);
procedure FindInBuffer(Name:String; var F:BFinder);
procedure ClearMem;
end;

var
MLMemory:^MMemory;
C :^COMMAND;
EQUs :^MLEQUs;
MLR :^MLRep;
implementation

uses Module15;

function COMMAND.GetLastCommand : String;
var i : Byte;
begin
i:=1;
while Buffer[i]<>'' do begin
Inc(i);
end; i:=i-2;
GetLastCommand:=Buffer[i];
end;

function COMMAND.GetNLastCommand(N : Byte) : String;
var i : Byte;
begin
i:=1;
while Buffer[i]<>'' do begin
Inc(i);
end;
if N>BufSize-2 then GetNLastCommand:='КОММАНДА УТЕРЕНА(Buffer=100) (N>98)!'
else
begin
Inc(N); i:=i-N;
GetNLastCommand:=Buffer[i];
end;
if N>BufSize-2 then ErrMsg:='КОММАНДА УТЕРЕНА(Buffer=100) (N>98)!'
end;

function COMMAND.RunLabel(Str:String) : Byte;
var i,L : Byte;
begin
L:=Length(Str);
Delete(Str,1,1);
Delete(Str,L,1);
i:=0;
repeat
Inc(i);
until (Pos('{',Buffer[i])=1)
and (Pos(Str,Buffer[i])=2);
RunLabel:=i-1;
end;

function COMMAND.cEQUDecode(MLT:String; S:String; var EQU:MyVar):Byte;
var P:Pointer;
begin
GetMem(P,SizeOf(EQUs));
FreeMem(P,SizeOf(EQUs));
cEQUDecode:=EQUs^.EQUDecode(MLT,S,EQU);
end;

procedure COMMAND.ClearBuffer;
var i : Byte;
begin
for i:=1 to BufSize do begin
Buffer[i]:='';
end;
end;

procedure COMMAND.SetCommandLine(Str:String);
var i : Byte;
begin
i:=1;
while Buffer[i]<>'' do begin
Inc(i);
end;
if i=BufSize+1 then ReWriteBuffer(Str)
else Buffer[i]:=Str;
end;

procedure COMMAND.ReWriteBuffer(Str:String);
var i : Byte;
begin
for i:=1 to BufSize-1 do begin
Buffer[i]:=Buffer[i+1];
end;
Buffer[i+1]:=Str;
end;

procedure COMMAND.RunBuffer;
var i:Byte;
begin
NumRC:=1; New(MLR); MLR^.ClearList;
New(EQUs);
while Pos(cmExit,Buffer[NumRC])=0 do begin
i:=NumRC;
RunCommand(Buffer[i]);
MLGotoCommand(Buffer[i]);
MLForCommand(Buffer[i]);
MLWriteForIntoX(Buffer[i]);
MLExitWithFor(Buffer[i]);
MLIfCommand(Buffer[i]);
MLProcedureCommand(Buffer[i]);
MLFunctionCommand(Buffer[i]);
MLTypeCommand(Buffer[i]);
MLObjectCommand(Buffer[i]);
MLConstCommand(Buffer[i]);
MLVarCommand(Buffer[i]);
Inc(NumRC);
if NumRC>BufSize then begin
NumRC:=1;
Buffer[NumRC]:=cmExit;
end;
end;
end;

procedure COMMAND.RunCommand(Str:String);
begin
WriteLn(Str);
end;

procedure COMMAND.MLGotoCommand(Command:String);
var i,di : Byte;
SGoto,SL : String;
Len : Word;
i1,j1 : Byte;
begin
SGoto:='';
i:=Pos(cmGoto,Command);
if i<>0 then begin
Len:=Length(Command);
SGoto:=Copy(Command,i,Len);
i1:=Pos('[',SGoto);
j1:=Pos(']',SGoto);
di:=ABS(i1-j1);
SL:=Copy(SGoto,i1,di);
NumRC:=RunLabel(SL);
end;
end;

procedure COMMAND.MLForCommand(Command:String);
var i1,j1,i:Byte;
S1,S2:String;
Number:Byte;
begin
if Pos(cmRepeat,Command)<>0 then begin
Number:=0; i1:=0; j1:=0; S1:=''; i:=0;
i1:=Pos('[',Command); j1:=Pos(']',Command);
j1:=ABS(i1-j1); S1:=Copy(Command,i1+1,j1-1);
S2:='';
for i:=1 to BufSize do begin
if Pos(S1,MLR^.RBuffer[i].RLabel)<>0 then begin
S2:='Старый'; Number:=i;
Inc(MLR^.RBuffer[Number].i);
end;
end;
if S2='' then Number:=MLR^.Init(S1);
if MLR^.RBuffer[Number].Activ=1 then C^.NumRC:=C^.RunLabel('['+S1);
end;
end;

procedure COMMAND.MLWriteForIntoX(Command:String);
var i1,j1:Byte;
S1:String;
i2,j2:Byte;
i,j,k:Integer;
S2:String;
begin i1:=0; j1:=0; i:=0; S1:='';
i2:=0; j2:=0; j:=0; S2:='';
if Pos(cmWrForX,Command)<>0 then begin
i1:=Pos('(',Command); j1:=Pos(')',Command);
j1:=ABS(i1-j1); S1:=Copy(Command,i1+1,j1-1);
i2:=Pos('[',Command); j2:=Pos(']',Command);
j2:=ABS(i2-j2); S2:=Copy(Command,i2+1,j2-1);
for i:=1 to StackSize do begin
if Pos(S1,MLMemory^.Variables[i]^.Name^)=1 then j:=i;
end; i:=0;
for i:=1 to BufSize do begin
if MLR^.RBuffer[i].RLabel=S2 then k:=i;
end; i:=0;
MLMemory^.Variables[j]^.I^:=MLR^.RBuffer[k].I;
MLMemory^.Variables[j]^.R^:=MLR^.RBuffer[k].I;
end;
end;

procedure COMMAND.MLExitWithFor(Command:String);
var i,j:Integer; i1,j1:Byte;
i2,j2:Byte; S1,S2:String;
Activ:Boolean;
begin i:=0; j:=0; i1:=0; j1:=0;
i2:=0; j2:=0; S1:=''; S2:='';
Activ:=False;
if Pos(cmExWFor,Command)<>0 then begin
i1:=Pos('(',Command); j1:=Pos(')',Command);
j1:=ABS(i1-j1); S1:=Copy(Command,i1+1,j1-1);
i2:=Pos('[',Command); j2:=Pos(']',Command);
j2:=ABS(i2-j2); S2:=Copy(Command,i2+1,j2-1);
Activ:=EQUs^.BoolIfer(S1);
if Activ then Activ:=MLR^.Done(Command);
end;
end;

procedure COMMAND.MLIfCommand(Command:String);
begin
end;

procedure COMMAND.MLProcedureCommand(Command:String);
begin
end;

procedure COMMAND.MLFunctionCommand(Command:String);
begin
end;

procedure COMMAND.MLTypeCommand(Command:String);
begin
end;

procedure COMMAND.MLObjectCommand(Command:String);
begin
end;

procedure COMMAND.MLConstCommand(Command:String);
begin
end;

procedure COMMAND.MLVarCommand(Command:String);
var i,Len : Byte; i1,j1 : Byte;
i2,j2 : Byte; i3,j3 : Byte;
d1,d2,d3 : Byte; S1,S2,S3 : String;
S,Name,MLType : String; MyEQU : MyVar;
Logik : Byte; Number : Integer;
begin
i:=Pos(cmVar,Command);
if i<>0 then begin
i1:=Pos('+',Command); j1:=Pos('-',Command);
i2:=Pos('[',Command); j2:=Pos(']',Command);
i3:=Pos('(',Command); j3:=Pos(')',Command);
d1:=ABS(i1-j1); d2:=ABS(i2-j2); d1:=ABS(i2-j2);
S:=Command; S1:=Copy(S,i1,d1); Name:=S1;
S2:=Copy(S,i2,d2); S3:=Copy(S,i3,d3);
i1:=Pos('+',S1); j1:=Pos('-',S1);
d1:=ABS(i1-j1); Name:=Copy(S1,i1+1,d1-1);
MLType:=S2; Delete(MLType,1,1); Len:=Length(MLType);
Logik:=cEQUDecode(MLType,S3,MyEQU);
Number:=MLMemory^.NewVar(Name,MLType,MyEQU);
end;
end;

function MMemory.NewVar(Name:String; NVType:String; EQU:MyVar):Word;
var i : Word;
S : String;
begin
i:=1;
while MLMemory^.Variables[i]^.Name^<>'' do begin
Inc(i);
end;
if i>StackSize then NewVar:=ReWStack(Name,NVType,EQU)
else
begin
MLMemory^.Variables[i]^.Name^:=Name+' {'+NVType+'}';
NewVar:=i; S:=NVType;

if S=cmReal then New(MLMemory^.Variables[i]^.R);
if S=cmInteger then New(MLMemory^.Variables[i]^.I);
if S=cmString then New(MLMemory^.Variables[i]^.S);
if S=cmChar then New(MLMemory^.Variables[i]^.C);

if S=cmReal then MLMemory^.Variables[i]^.R^:=EQU.R^;
if S=cmInteger then MLMemory^.Variables[i]^.I^:=EQU.I^;
if S=cmString then MLMemory^.Variables[i]^.S^:=EQU.S^;
if S=cmChar then MLMemory^.Variables[i]^.C^:=EQU.C^;
end;
end;

function MMemory.NewType(Name:String; Size:Integer; List:MType):Word;
begin
end;

function MMemory.NewClass(Name:String; Size:Integer; List:MClass):Word;
begin
end;

procedure MMemory.ClearMem;
var i : Word;
P : Pointer;
begin
New(MLMemory);
for i:=1 to StackSize do begin
New(MLMemory^.Variables[i]);
New(MLMemory^.Variables[i]^.Name);
MLMemory^.Variables[i]^.Name^:='';
GetMem(P,SizeOf(MLMemory^.Variables[i]));
FreeMem(P,SizeOf(MLMemory^.Variables[i]));
end;
end;

function MMemory.ReWStack(Name:String; NVType:String; EQU:MyVar):Word;
var i:Word;
S:String;
begin
for i:=1 to StackSize-1 do begin
MLMemory^.Variables[i]^:=MLMemory^.Variables[i+1]^;
end;
ReWStack:=StackSize; i:=StackSize;
MLMemory^.Variables[i]^.Name^:=Name+' {'+NVType+'}';
S:=NVType;
if S=cmReal then New(MLMemory^.Variables[i]^.R);
if S=cmInteger then New(MLMemory^.Variables[i]^.I);
if S=cmString then New(MLMemory^.Variables[i]^.S);
if S=cmChar then New(MLMemory^.Variables[i]^.C);

if S=cmReal then MLMemory^.Variables[i]^.R^:=EQU.R^;
if S=cmInteger then MLMemory^.Variables[i]^.I^:=EQU.I^;
if S=cmString then MLMemory^.Variables[i]^.S^:=EQU.S^;
if S=cmChar then MLMemory^.Variables[i]^.C^:=EQU.C^;
end;

procedure MMemory.FindInMemory(Name:String; var F:MFinder);
var i:Integer;
begin
for i:=1 to StackSize do begin
if Pos(Name,MLMemory^.Variables[i]^.Name^)<>0 then F[i]:=1
else F[i]:=0
end;
end;

procedure MMemory.FindInBuffer(Name:String; var F:BFinder);
var i:Integer;
begin
for i:=1 to BufSize do begin
if Pos(Name,C^.Buffer[i])<>0 then F[i]:=1
else F[i]:=0
end;
end;

function MLEQUs.EQUDecode(MLT:String; S:String; var EQU:MyVar):Byte;
var i,L:Integer;
i1,j1:Integer;
Ch:Char;
B:MFinder;
begin
if Pos(cmEQUid,S)<>0 then begin
Delete(S,1,1); L:=Length(S);
Delete(S,L,1); i:=Pos(cmEQUid,S);
Delete(S,i,2);
if MLT=cmChar then begin
New(EQU.C); Ch:=S[1];
EQU.C^:=Ch;
end;
if MLT=cmString then begin
New(EQU.S); EQU.S^:=S;
end;
if MLT=cmReal then begin
New(EQU.R); EQU.R^:=RealDecode(S);
end;
if MLT=cmInteger then begin
New(EQU.I); EQU.I^:=IntegerDecode(S);
end;
end;
if Pos(cmvEQUid,S)<>0 then begin
Delete(S,1,1); L:=Length(S);
Delete(S,L,1); i:=Pos(cmvEQUid,S);
Delete(S,i,1);
MLMemory^.FindInMemory(S,B);
for i:=1 to StackSize do begin
if B[i]=1 then begin
if MLT=cmChar then begin
New(EQU.C); EQU.C^:=MLMemory^.Variables[i]^.C^;
end;
if MLT=cmString then begin
New(EQU.S); EQU.S^:=MLMemory^.Variables[i]^.S^;
end;
if MLT=cmReal then begin
New(EQU.R); EQU.R^:=MLMemory^.Variables[i]^.R^;
end;
if MLT=cmInteger then begin
New(EQU.I); EQU.I^:=MLMemory^.Variables[i]^.I^;
end;
end;
end;
end;
end;

function MLEQUs.RealDecode(var Str:String):Real;
var L,L1,i,j:Integer;
Result:Real;
RRes,St:Real;
begin
L:=Pos('.',Str)-1; Result:=0;
for i:=1 to L do begin
j:=Ord(Str[i])-48; Result:=Result*10+j;
end; St:=1; RRes:=0;
L1:=Length(Str); L:=Pos('.',Str)+1;
for i:=L to L1 do begin
St:=St*10; j:=Ord(Str[i])-48;
RRes:=RRes+j/St;
end; Result:=Result+RRes;
RealDecode:=Result;
end;


function MLEQUs.IntegerDecode(Str:String):Integer;
var L,i,j:Integer;
Result:Integer;
begin
L:=Length(Str);
Result:=0;
for i:=1 to L do begin
j:=Ord(Str[i])-48;
Result:=Result*10+j;
end;
IntegerDecode:=Result;
end;

function MLEQUs.BoolIfer(Str:String):Boolean;
var i:Byte; ch:Char; Test:Boolean;
begin
ChangeAllsVariables(Str);
ChangeAllsSignAndSaves(Str,Ch);
CountAllsOperand(Str,i);
if (i>0) and (Ch=cmbRight) then Test:=True;
if (i=0) and (Ch=cmvEQUid) then Test:=True;
if (i<0) and (Ch=cmbLeft) then Test:=True;
end;

procedure MLEQUs.CountAllsOperand(var S:String; R:Real);
begin
end;

procedure MLEQUs.ChangeAllsSignAndSaves(var S:String; Ch:Char);
var j,i,L:Byte; Sign:Boolean; Position:Byte; S1:String;
begin
j:=0; i:=0; L:=0; S1:=''; Sign:=False;
L:=Length(S); Ch:=Chr(0); Position:=0;
for i:=1 to L do begin
if (S[i]='>') or (S[i]='=') or (S[i]='<') then begin
Ch:=S[i]; Position:=i; Sign:=True;
end;
end; i:=Position; j:=ABS(L-i);
S1:=Copy(S,i+1,j); S1:='-('+S1+')';
Delete(S,i,j+1); S:=S+S1;
end;

procedure MLEQUs.ChangeAllsVariables(var MStr:String);
var i,j:Integer; a,b,c,d:Byte;
S,MLocType:String; i1,j1:Byte;
begin i:=0; j:=0; S:='';
for i:=1 to StackSize do begin
j:=Pos(' ',MLMemory^.Variables[i]^.Name^);
S:=Copy(MLMemory^.Variables[i]^.Name^,1,j-1);

i1:=Pos('{',MLMemory^.Variables[i]^.Name^);
j1:=Pos('}',MLMemory^.Variables[i]^.Name^);
j1:=ABS(i1-j1);
MLocType:=Copy(MLMemory^.Variables[i]^.Name^,i1+1,j1-1);

j:=Pos(S,MStr);
if j<>0 then begin
if MLocType=cmReal then begin
A:=Length(S); Delete(MStr,j,A); S:='';
Str(MLMemory^.Variables[i]^.R^,S);
Insert(S,MStr,j);
end;
if MLocType=cmInteger then begin
A:=Length(S); Delete(MStr,j,A); S:='';
Str(MLMemory^.Variables[i]^.i^,S);
Insert(S,MStr,j);
end;
if MLocType=cmString then begin
A:=Length(S); Delete(MStr,j,A); S:='';
Insert(MLMemory^.Variables[i]^.S^,MStr,j);
end;
if MLocType=cmChar then begin
A:=Length(S); Delete(MStr,j,A); S:='';
Insert(MLMemory^.Variables[i]^.C^,MStr,j);
end;
end;
end;
end;

function MLRep.Init(S:String):Byte;
var i,j:Byte;
Detect:String;
begin Detect:=''; i:=0; j:=0;
for j:=1 to BufSize do begin
if Length(MLR^.RBuffer[i].RLabel)>12 then MLR^.RBuffer[i].RLabel:='';
if MLR^.RBuffer[i].RLabel=S then begin
Detect:='Найден!'; Init:=j;
Inc(MLR^.RBuffer[j].i);
MLR^.RBuffer[i].Activ:=1;
end;
end;
if Detect='' then begin i:=1;
while MLR^.RBuffer[i].RLabel<>'' do begin
Inc(i);
end;
if i>BufSize then begin
for j:=1 to BufSize-1 do begin
MLR^.RBuffer[i].i:=MLR^.RBuffer[i+1].i;
MLR^.RBuffer[i].Activ:=MLR^.RBuffer[i+1].Activ;
MLR^.RBuffer[i].RLabel:=MLR^.RBuffer[i+1].RLabel;
end;
end;
MLR^.RBuffer[i].RLabel:=S;
MLR^.RBuffer[i].Activ:=1;
MLR^.RBuffer[i].i:=1;
Init:=i;
end;
end;

function MLRep.Done(S:String):Boolean;
var i:Byte;
begin
end;

procedure MLRep.ClearList;
var i:Byte;
begin
for i:=1 to BufSize do begin
MLR^.RBuffer[i].i:=0;
MLR^.RBuffer[i].Activ:=0;
MLR^.RBuffer[i].RLabel:='';
end;
end;

end.



Частковий переклад на Сі. (В процесі розробки)

//--------------------
//   MLMP Library
//--------------------

#include "Counter.h"
#include "Memory.h"
#include "Module15.h"

#define BufSize 250
#define StackSize 1000

extern String cmExit = "Выход_из_программы";
extern String cmGoto = "Перейти_к";
extern String cmEQUid = ":=";
extern String cmvEQUid = "=";
extern String cmbLeft = "<";
extern String cmbRight = ">";
extern String cmRepeat = "Цикл";
extern String cmWrForX = "Записать_значение_цикла_в";
extern String cmExWFor = "Выход_из_цикла";
extern String cmVar = "Создать_переменную";
extern String cmReal = "Реальное_число";
extern String cmInteger = "Целое_число";
extern String cmString = "Строка_символов";
extern String cmChar = "Один_символ";

class MyVar
{
public:
  String* Name;
  float*  R;
  int*    I;
  String* S;
  char*   C;
};

int MFinder[StackSize+1];
int BFinder[BufSize+1];

class(ThisProgram) MType
{  
public:       
  String* List[100+1];
  int*    MyMem[1000+1];
};

class(ThisProgram) MClass
{
public:
  String* List[100+1];
  int*    MyMem[1000+1];
  String* Proc[100+1];
  String* Func[100+1];
};

class(ThisProgram) COMMAND
{
public:
  String ErrMsg;            
  int NumRC;
  String Buffer[BufSize+1];
  String GetLastCommand();
  String GetNLastCommand(int N);
  int RunLabel(String Str);
  int cEQUDecode(String MLT, String S, var EQU:MyVar);
  void ClearBuffer();
  void SetCommandLine(String Str);
  void ReWriteBuffer(String Str);
  void RunBuffer();
  void RunCommand(String Str);
  void MLGotoCommand(String Command);
  void MLForCommand(String Command);
  void MLWriteForIntoX(String Command);
  void MLExitWithFor(String Command);
  void MLIfCommand(String Command);
  void MLProcedureCommand(String Command);
  void MLFunctionCommand(String Command);
  void MLTypeCommand(String Command);
  void MLObjectCommand(String Command);
  void MLConstCommand(String Command);
  void MLVarCommand(String Command);
};

class(Command) MLEQUs
{
public:
  int EQUDecode(String MLT, String S, var EQU:MyVar);
  float RealDecode(var String Str);
  int IntegerDecode(String Str);
  bool BoolIfer(String Str);
  void ChangeAllsVariables(var String MStr);
  void ChangeAllsSignAndSaves(var String S, char Ch);
  void CountAllsOperand(var String S, float R);
};

class MLRepeat
{
public:
  int i;
  int Activ;
  String RLabel;
};

class(ThisProgram) MLRep
{
public:
  MLRepeat RBuffer[BufSize+1];
  int Init(String S);
  bool Done(String S);
  void ClearList();
};

class(ThisProgram) MMemory
{
public:            
  MyVar* Variables[StackSize+1];
  int NewVar(String Name, String NVType, MyVar EQU);
  int NewType(String Name, int Size, MType List);
  int NewClass(String Name, int Size, MClass List);
  int ReWStack(String Name, String NVType, MyVar EQU);
  void FindInMemory(String Name, var MFinder F);
  void FindInBuffer(String Name, var BFinder F);
  void ClearMem();
};

MMemory* MLMemory;
COMMAND* C;
MLEQUs*  EQUs;
MLRep*   MLR;

String COMMAND::GetLastCommand()
{
  int i = 1;
  while (Buffer[i]!="")
  {
         i++;
  };
  i = i - 2;
  GetLastCommand = Buffer[i];
};


String COMMAND::GetNLastCommand(int N)
{
  int i = 1;
  while(Buffer[i]!="")
  {
        i++;
  };
  if(N>BufSize-2)
  {
      GetNLastCommand = "КОММАНДА УТЕРЕНА(Buffer=100) (N>98)!";
  } else
  {
      N++;
      i = i - N;
      GetNLastCommand = Buffer[i];
  };
  if(N>BufSize-2)
  {
      ErrMsg = "КОММАНДА УТЕРЕНА(Buffer=100) (N>98)!";
  };
};

int COMMAND::RunLabel(String Str)
{
  int i;
  int L;
  L = Length(Str);
  Delete(Str,1,1);
  Delete(Str,L,1);
  i = 0;
  while((Pos("{",Buffer[i])!=1)||(Pos(Str,Buffer[i])!=2))
  {
    i++;
  };
  RunLabel = i-1;
};

int COMMAND::cEQUDecode(String MLT, String S, var MyVar EQU)
{
  Pointer P;
  GetMem(P,SizeOf(EQUs));
  FreeMem(P,SizeOf(EQUs));
  cEQUDecode = EQUs->EQUDecode(MLT,S,EQU);
};

void COMMAND::ClearBuffer()
{
  int i;
  for(i=1; i<=BufSize; i++)
  {
       Buffer[i] = "";
  };
};

void COMMAND::SetCommandLine(String Str)
{
  int i = 1;
  while(Buffer[i]!="")
  {
         i++;
  };
  if(i==BufSize+1)
  {
      ReWriteBuffer(Str);
  } else
  {
      Buffer[i] = Str;
  };
};

void COMMAND::ReWriteBuffer(String Str)
{
  int i;
  for(i=1; i<=BufSize-1; i++)
  {
       Buffer[i] = Buffer[i+1];
  };
  Buffer[i+1] = Str;
};

void COMMAND::RunBuffer()
{
  int i;
  NumRC = 1;
  new(MLR);
  MLR->ClearList();
  new(EQUs);
  while(Pos(cmExit,Buffer[NumRC])==0)
  {
    i = NumRC;
    RunCommand(Buffer[i]);
    MLGotoCommand(Buffer[i]);
    MLForCommand(Buffer[i]);
    MLWriteForIntoX(Buffer[i]);
    MLExitWithFor(Buffer[i]);
    MLIfCommand(Buffer[i]);
    MLProcedureCommand(Buffer[i]);
    MLFunctionCommand(Buffer[i]);
    MLTypeCommand(Buffer[i]);
    MLObjectCommand(Buffer[i]);
    MLConstCommand(Buffer[i]);
    MLVarCommand(Buffer[i]);
    NumRC++;
    if(NumRC>BufSize)
    {
        NumRC = 1;
        Buffer[NumRC] = cmExit;
    };
  };
};

void COMMAND::RunCommand(String Str)
{
  WriteLn(Str);
};

void COMMAND::MLGotoCommand(String Command)
{
  int i,di;
  int Len;
  int i1,j1;

  String SGoto;
  String SL;

  SGoto = "";
  i = Pos(cmGoto,Command);
  if(i!=0)
  {
      Len   = Length(Command);
      SGoto = Copy(Command,i,Len);
      i1    = Pos("[",SGoto);
      j1    = Pos("]",SGoto);
      di    = ABS(i1-j1);
      SL    = Copy(SGoto,i1,di);
      NumRC = RunLabel(SL);
  };
};

void COMMAND::MLForCommand(String Command)
{
  int i1,j1,i;
  int Number;

  String S1,S2;

  if(Pos(cmRepeat,Command)!=0)
  {
      Number = 0;
      i1 = 0;
      j1 = 0;
      S1 = "";
      i  = 0;
      i1 = Pos("[",Command);
      j1 = Pos("]",Command);
      j1 = ABS(i1-j1);
      S1 = Copy(Command,i1+1,j1-1);
      S2 = "";
      for(i=1; i<=BufSize; i++)
      {
           if(Pos(S1,MLR->RBuffer[i].RLabel)!=0)
           {
               S2 = "Старый";
               Number = i;
               MLR->RBuffer[Number].i++;
           };
      };
      if(S2=="")
      {
          Number = MLR->Init(S1);
          if(MLR->RBuffer[Number].Activ==1)
          {
              C->NumRC = C->RunLabel("["+S1);
          };
      };
  };
};

void COMMAND::MLWriteForIntoX(String Command)
{
  int k;
  int i1 = 0;
  int j1 = 0;
  int i  = 0;
  int j  = 0;
  int i2 = 0;
  int j2 = 0;

  String S1;
  String S2;

  S1 = "";
  S2 = "";
  if(Pos(cmWrForX,Command)!=0) then
  {
    i1 = Pos("(",Command);
    j1 = Pos(")",Command);
    j1 = ABS(i1-j1);
    S1 = Copy(Command,i1+1,j1-1);
    i2 = Pos("[",Command);
    j2 = Pos("]",Command);
    j2 = ABS(i2-j2);
    S2 = Copy(Command,i2+1,j2-1);
    for(i=1; i<=StackSize; i++)
    {
      if(Pos(S1,MLMemory->Variables[i]^.Name^)==1)
      {
          j = i;
      };
    };
    i = 0;
    for(i=1; i<=BufSize; i++)
    {
      if(MLR->RBuffer[i].RLabel==S2)
      {
          k = i;
      };
    };
    i = 0;
    MLMemory->Variables[j]->I^ = MLR->RBuffer[k].I;
    MLMemory->Variables[j]->R^ = MLR->RBuffer[k].I;
  };
};

void COMMAND::MLExitWithFor(String Command)
{
  int i = 0;
  int j = 0;

  int i1 = 0;
  int j1 = 0;

  int i2 = 0;
  int j2 = 0;

  bool Activ;

  String S1,S2;

  S1 = "";
  S2 = "";
  Activ = false;
  if (Pos(cmExWFor,Command)!=0)
  {
      i1 = Pos("(",Command);
      j1 = Pos(")",Command);
      j1 = ABS(i1-j1);
      S1 = Copy(Command,i1+1,j1-1);
      i2 = Pos("[",Command);
      j2 = Pos("]",Command);
      j2 = ABS(i2-j2);
      S2 = Copy(Command,i2+1,j2-1);
      Activ = EQUs->BoolIfer(S1);
      if (Activ)
      {
          Activ = MLR->Done(Command);
      };
  };
};

void COMMAND::MLIfCommand(String Command)
{
};

void COMMAND::MLProcedureCommand(String Command)
{
};

void COMMAND::MLFunctionCommand(String Command)
{
};

void COMMAND::MLTypeCommand(String Command)
{
};

void COMMAND::MLObjectCommand(String Command)
{
};

void COMMAND::MLConstCommand(String Command)
{
};

void COMMAND::MLVarCommand(String Command)
{
  int i,Len;
  int i1,j1;
  int i2,j2;
  int i3,j3;
  int d1,d2,d3;

  int Logik;
  int Number;

  String S1,S2,S3;
  String S,Name,MLType;
  MyVar  MyEQU;

  i = Pos(cmVar,Command);

  if (i!=0)
  {
      i1 = Pos("+",Command);
      j1 = Pos("-",Command);

      i2 = Pos("[",Command);
      j2 = Pos("]",Command);

      i3 = Pos("(",Command);
      j3 = Pos(")",Command);

      d1 = ABS(i1-j1);
      d2 = ABS(i2-j2);
      d1 = ABS(i2-j2);

      S    = Command;
      S1   = Copy(S,i1,d1);
      Name = S1;

      S2 = Copy(S,i2,d2);
      S3 = Copy(S,i3,d3);

      i1     = Pos("+",S1);
      j1     = Pos("-",S1);
      d1     = ABS(i1-j1);
      Name   = Copy(S1,i1+1,d1-1);
      MLType = S2;
  
      Delete(MLType,1,1);

      Len    = Length(MLType);
      Logik  = cEQUDecode(MLType,S3,MyEQU);
      Number = MLMemory->NewVar(Name,MLType,MyEQU);
  };
};

int MMemory::NewVar(String Name, String NVType, MyVar EQU)
{
  String S;
  int i = 1;

  while(MLMemory->Variables[i]->Name^!="")
  {
    i++;
  };

  if(i>StackSize)
  {
      NewVar = ReWStack(Name,NVType,EQU);
  } else
  {
 
      MLMemory->Variables[i]->Name^ = Name+" {"+NVType+"}";
  
      NewVar = i;
      S      = NVType;

      if(S==cmReal)
      {
          new(MLMemory->Variables[i]->R);
          MLMemory->Variables[i]->R^ = EQU.R^;
      };
      if(S==cmInteger)
      {
          new(MLMemory^.Variables[i]->I);
          MLMemory->Variables[i]->I^ = EQU.I^;
      };
      if(S==cmString)
      {
          new(MLMemory->Variables[i]->S);
          MLMemory->Variables[i]->S^ = EQU.S^;
      };
      if(S==cmChar)
      {
          new(MLMemory->Variables[i]->C);
          MLMemory->Variables[i]->C^ = EQU.C^;
      };
  };
};

int MMemory::NewType(String Name, int Size, MType List)
{
};

int MMemory::NewClass(String Name, int Size, MClass List)
{
};

void MMemory::ClearMem()
{
  int i;
  Pointer P;

  New(MLMemory);
  for(i=1; i<=StackSize; i++)
  {
       new(MLMemory->Variables[i]);
       new(MLMemory->Variables[i]->Name);
       MLMemory->Variables[i]->Name^ = "";
       GetMem(P,SizeOf(MLMemory->Variables[i]));
       FreeMem(P,SizeOf(MLMemory->Variables[i]));
  };
};

int MMemory::ReWStack(String Name, NVType:String, EQU:MyVar)
{
  int i;
  String S;

  for(i=1; i<=StackSize-1; i++)
  {
       MLMemory->Variables[i]^ = MLMemory->Variables[i+1]^;
  };

  ReWStack = StackSize;
  i        = StackSize;

  MLMemory->Variables[i]->Name^:=Name+' {'+NVType+'}';

  S = NVType;

  if(S==cmReal)
  {
      new(MLMemory->Variables[i]->R);
      MLMemory->Variables[i]->R^ = EQU.R^;
  };
  if(S==cmInteger)
  {
      new(MLMemory->Variables[i]->I);
      MLMemory->Variables[i]->I^ = EQU.I^;
  };
  if(S==cmString)
  {
      new(MLMemory->Variables[i]->S);
      MLMemory->Variables[i]->S^ = EQU.S^;
  };
  if(S==cmChar)
  {
      new(MLMemory->Variables[i]->C);
      MLMemory->Variables[i]->C^ = EQU.C^;
  };
};

void MMemory::FindInMemory(String Name, var MFinder F)
{
  int i;
  for(i=1; i<=StackSize; i++)
  {
       if(Pos(Name,MLMemory->Variables[i]->Name^)!=0)
       {
           F[i] = 1;
       } else
       {
           F[i] = 0;
       };
  };
};

void MMemory::FindInBuffer(String Name, var BFinder F)
{
  int i;
  for(i=1; i<=BufSize; i++)
  {
       if (Pos(Name,C->Buffer[i])!=0)
       {
           F[i] = 1;
       } else
       {
           F[i] = 0;
       };
  };
};

int MLEQUs::EQUDecode(String MLT, String S, var MyVar EQU)
{
  int i,L;
  int i1,j1;
  char Ch;

  MFinder B;

  if(Pos(cmEQUid,S)!=0)
  {
      Delete(S,1,1);
      L = Length(S);
      Delete(S,L,1);
      i = Pos(cmEQUid,S);
      Delete(S,i,2);
      if(MLT==cmChar)
      {
          new(EQU.C);
          Ch     = S[1];
          EQU.C^ = Ch;
      };
      if(MLT==cmString)
      {
          new(EQU.S);
          EQU.S^ = S;
      };
      if(MLT==cmReal)
      {
          new(EQU.R);
          EQU.R^ = RealDecode(S);
      };
      if(MLT==cmInteger)
      {
          new(EQU.I);
          EQU.I^ = IntegerDecode(S);
      };
  };
  if(Pos(cmvEQUid,S)!=0)
  {
      Delete(S,1,1);
      L = Length(S);
      Delete(S,L,1);
      i = Pos(cmvEQUid,S);
      Delete(S,i,1);
      MLMemory->FindInMemory(S,B);
      for(i=1; i<=StackSize; i++)
      {
           if(B[i]==1)
           {
               if(MLT==cmChar)
               {
                   new(EQU.C);
                   EQU.C^ = MLMemory->Variables[i]->C^;
               };
               if(MLT==cmString)
               {
                   new(EQU.S);
                   EQU.S^ = MLMemory->Variables[i]->S^;
               };
               if(MLT==cmReal)
               {
                   new(EQU.R);
                   EQU.R^ = MLMemory->Variables[i]->R^;
               };
               if(MLT==cmInteger)
               {
                   new(EQU.I);
                   EQU.I^ = MLMemory->Variables[i]->I^;
               };
           };
      };
  };
};

float MLEQUs::RealDecode(var String Str)
{
  int L,L1,i,j;
  float Result = 0;
 
  float RRes,St;

  L = Pos(".",Str)-1;

  for(i=1; i<=L; i++)
  {
       j = Ord(Str[i])-48;
       Result = Result*10+j;
  };
 
  St = 1;
  RRes = 0;
  L1 = Length(Str);
  L = Pos(".",Str)+1;
 
  for(i=L; i<=L1; i++)
  {
       St   = St*10;
       j    = Ord(Str[i])-48;
       RRes = RRes+j/St;
  };

  Result = Result + RRes;
  RealDecode = Result;
};


int MLEQUs::IntegerDecode(String Str)
{
  int L,i,j;
  int Reault = 0;

  L = Length(Str);

  for (i=1; i<=L; i++)
  {
       j      = Ord(Str[i])-48;
       Result = Result*10+j;
  };

  IntegerDecode = Result;
};

bool MLEQUs::BoolIfer(String Str);
{
  int i;
  bool Test;
  char ch;

  ChangeAllsVariables(Str);
  ChangeAllsSignAndSaves(Str,Ch);
  CountAllsOperand(Str,i);
  if ((i>0)&&(Ch==cmbRight))
  {
       Test = true;
  };
  if ((i==0)&&(Ch==cmvEQUid))
  {
       Test = true;
  };
  if ((i<0)&&(Ch==cmbLeft))
  {
       Test = true;
  };
};

void MLEQUs::CountAllsOperand(var String S, float R);
{
};

void MLEQUs::ChangeAllsSignAndSaves(var String S, char Ch);
{
  String S1;
  int j = 0;
  int i = 0;
  int L = 0;
  int Position = 0;

  bool Sign;

  S1 = "";
 
  Sign = false;

  L  = Length(S);
  Ch = Chr(0);

  for(i=1; i<=L; i++)
  {
       if ((S[i]==">")||(S[i]=="=")||(S[i]=="<"))
       {
            Ch = S[i];
            Position = i;
            Sign = true;
       };
  };
 
  i = Position;
  j = ABS(L-i);
  S1 = Copy(S,i+1,j);
  S1 = "-("+S1+")";
  Delete(S,i,j+1);

  S = S + S1;
};

void MLEQUs::ChangeAllsVariables(var String MStr)
{
  int i = 0;
  int j = 0;
  int i1,j1;
  int a,b,c,d;

  String S;
  String MLocType;

  S = "";

  for(i=1; i<=StackSize; i++)
  {
       j = Pos(" ",MLMemory->Variables[i]->Name^);
       S = Copy(MLMemory->Variables[i]->Name^,1,j-1);

       i1 = Pos("{",MLMemory->Variables[i]->Name^);
       j1 = Pos("}",MLMemory->Variables[i]->Name^);
       j1 = ABS(i1-j1);
       MLocType = Copy(MLMemory->Variables[i]->Name^,i1+1,j1-1);

       j = Pos(S,MStr);
       if(j!=0) 
       {
           if(MLocType==cmReal)
           {
               A = Length(S);
               Delete(MStr,j,A);
               S = "";
               Str(MLMemory->Variables[i]->R^,S);
               Insert(S,MStr,j);
           };
           if(MLocType==cmInteger)
           {
               A = Length(S);
               Delete(MStr,j,A);
               S = "";
               Str(MLMemory->Variables[i]->i^,S);
               Insert(S,MStr,j);
           };
           if(MLocType==cmString)
           {
               A = Length(S);
               Delete(MStr,j,A);
               S = "";
               Insert(MLMemory->Variables[i]->S^,MStr,j);
           };
           if(MLocType==cmChar)
           {
               A = Length(S);
               Delete(MStr,j,A);
               S = "";
               Insert(MLMemory->Variables[i]->C^,MStr,j);
           };
       };
  };
};

int MLRep::Init(String S)
{
  int i = 0;
  int j = 0;

  String Detect;

  Detect = "";

  for (j=1; j<=BufSize; j++)
  {
       if(Length(MLR->RBuffer[i].RLabel)>12)
       {
           MLR->RBuffer[i].RLabel = "";
       };
       if(MLR->RBuffer[i].RLabel==S)
       {
           Detect = "Найден!";
           Init = j;
           MLR->RBuffer[j].i++;
           MLR->RBuffer[i].Activ = 1;
       };
  };

  if (Detect=="")
  {
      i = 1;
      while(MLR->RBuffer[i].RLabel!="")
      {
             i++;
      };
      if(i>BufSize)
      {
          for (j=1; j<=BufSize-1; j++)
          {
               MLR->RBuffer[i].i = MLR->RBuffer[i+1].i;
               MLR->RBuffer[i].Activ = MLR->RBuffer[i+1].Activ;
               MLR->RBuffer[i].RLabel = MLR->RBuffer[i+1].RLabel;
          };
      };
      MLR->RBuffer[i].RLabel = S;
      MLR->RBuffer[i].Activ = 1;
      MLR->RBuffer[i].i = 1;
      Init = i;
  };
};

bool MLRep::Done(String S);
{
  int i;
};

void MLRep::ClearList();
{
  int i;
  for (i=1; i<=BufSize; i++)
  {
       MLR->RBuffer[i].i = 0;
       MLR->RBuffer[i].Activ = 0;
       MLR->RBuffer[i].RLabel = "";
  };
};




Категория: Програмування | Добавил: Beloshenko (06.01.2011) | Автор: Белошенко Никита Анатолиевич
Просмотров: 75 | Рейтинг: 5.0/1 |

Всего комментариев: 0
Имя *:
Email:
Код *:

Beloshenko Copyright © 2012Создать сайт бесплатно