home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 February
/
Chip_2000-02_cd.bin
/
zkuste
/
Delphi
/
navody
/
tt
/
objvm.exe
/
SAMPLE
/
ObjLangF.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1998-05-31
|
10KB
|
404 lines
unit ObjLangF;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Compiler,PrseTree,ObjCodeGeneration,Code,NodeStack,IntStack;
const NotCalled=0;
Called=1;
type
TObjLang = class(TForm)
Compiler: TCompiler;
procedure CompilerOnR1(Sender: TCompiler; b: TBush);
procedure CompilerOnR5(Sender: TCompiler; b: TBush);
procedure CompilerOnR8(Sender: TCompiler; b: TBush);
procedure CompilerOnR10(Sender: TCompiler; b: TBush);
procedure CompilerOnR11(Sender: TCompiler; b: TBush);
procedure CompilerOnR12(Sender: TCompiler; b: TBush);
procedure CompilerOnR13(Sender: TCompiler; b: TBush);
procedure CompilerOnR15(Sender: TCompiler; b: TBush);
procedure CompilerOnR17(Sender: TCompiler; b: TBush);
procedure CompilerOnR18(Sender: TCompiler; b: TBush);
procedure CompilerOnR20(Sender: TCompiler; b: TBush);
procedure CompilerOnR21(Sender: TCompiler; b: TBush);
procedure CompilerOnR22(Sender: TCompiler; b: TBush);
procedure CompilerOnR23(Sender: TCompiler; b: TBush);
procedure CompilerOnR24(Sender: TCompiler; b: TBush);
procedure CompilerOnR25(Sender: TCompiler; b: TBush);
procedure CompilerOnR27(Sender: TCompiler; b: TBush);
procedure CompilerOnR28(Sender: TCompiler; b: TBush);
procedure CompilerOnR30(Sender: TCompiler; b: TBush);
procedure CompilerOnR31(Sender: TCompiler; b: TBush);
procedure CompilerOnR32(Sender: TCompiler; b: TBush);
procedure CompilerOnR33(Sender: TCompiler; b: TBush);
procedure CompilerOnR34(Sender: TCompiler; b: TBush);
procedure CompilerOnR35(Sender: TCompiler; b: TBush);
procedure CompilerOnR39(Sender: TCompiler; b: TBush);
procedure CompilerOnR41(Sender: TCompiler; b: TBush);
procedure CompilerOnR43(Sender: TCompiler; b: TBush);
procedure CompilerOnR44(Sender: TCompiler; b: TBush);
procedure CompilerOnR45(Sender: TCompiler; b: TBush);
procedure CompilerOnR48(Sender: TCompiler; b: TBush);
procedure CompilerOnR50(Sender: TCompiler; b: TBush);
procedure CompilerOnR51(Sender: TCompiler; b: TBush);
procedure CompilerOnR54(Sender: TCompiler; b: TBush);
procedure CompilerOnR55(Sender: TCompiler; b: TBush);
procedure CompilerOnR56(Sender: TCompiler; b: TBush);
procedure CompilerOnR58(Sender: TCompiler; b: TBush);
procedure CompilerOnR59(Sender: TCompiler; b: TBush);
procedure CompilerOnR61(Sender: TCompiler; b: TBush);
procedure CompilerOnR62(Sender: TCompiler; b: TBush);
procedure CompilerOnR65(Sender: TCompiler; b: TBush);
procedure CompilerOnR64(Sender: TCompiler; b: TBush);
procedure CompilerOnR70(Sender: TCompiler; b: TBush);
procedure CompilerOnR71(Sender: TCompiler; b: TBush);
procedure CompilerOnR74(Sender: TCompiler; b: TBush);
procedure CompilerOnR75(Sender: TCompiler; b: TBush);
procedure CompilerOnR76(Sender: TCompiler; b: TBush);
procedure CompilerOnR77(Sender: TCompiler; b: TBush);
procedure CompilerOnR78(Sender: TCompiler; b: TBush);
private
{ Private declarations }
g:TObjCodeGeneration;
public
{ Public declarations }
function Gen:TObjCodeGeneration;
function CompileTo(s:TStrings;c:TCode):boolean;
end;
var
ObjLang: TObjLang;
function Compile(s:TStrings):TCode;
implementation
{$R *.DFM}
function Compile(s:TStrings):TCode;
Var f:TObjLang;
begin
f:=TObjLang.Create(nil);
Result:=TCode.Create(nil);
Result.Size:=length(s.Text)*2+8000;
f.CompileTo(s,Result);
f.Free;
end;
function TObjLang.CompileTo;
begin
g:=TObjCodeGeneration.Create(c);
try
Compiler.BeginCompile;
Compiler.AcceptStrings(0,s);
finally
Compiler.EndCompile;
g.Free;
end;
Result:=not Compiler.WasErrors;
end;
function TObjLang.Gen;
begin
end;
procedure TObjLang.CompilerOnR1(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[0]);
end;
procedure TObjLang.CompilerOnR5(Sender: TCompiler; b: TBush);
begin
g.cRoot;
Compiler.CompileNode(b[0]);
end;
procedure TObjLang.CompilerOnR8(Sender: TCompiler; b: TBush);
begin
if g.Stack.Pop=NotCalled then
begin
end
else
begin
g.cEval;
end;
Compiler.CompileNode(b[1]);
end;
procedure TObjLang.CompilerOnR10(Sender: TCompiler; b: TBush);
begin
g.Stack.Push(NotCalled);
end;
procedure TObjLang.CompilerOnR11(Sender: TCompiler; b: TBush);
begin
g.cToR;
Compiler.CompileNode(b[0]);
g.cFromR;
g.Stack.Push(Called);
end;
procedure TObjLang.CompilerOnR12(Sender: TCompiler; b: TBush);
begin
g.cStr(StrConstOf(b[0]));
g.cGetElem;
end;
procedure TObjLang.CompilerOnR13(Sender: TCompiler; b: TBush);
begin
g.cStr(NodeToStr(b[0]));
g.cGetElem;
end;
procedure TObjLang.CompilerOnR15(Sender: TCompiler; b: TBush);
begin
g.NodeStack.Push(b[0]);
Compiler.CompileNode(b[1]);
end;
procedure TObjLang.CompilerOnR17(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(g.NodeStack.Pop);
if g.Stack.Pop=NotCalled then
begin
g.cToR;
g.cInt(0);
g.cFromR;
end;
g.cExec;
end;
procedure TObjLang.CompilerOnR18(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
Compiler.CompileNode(g.NodeStack.Pop);
if g.Stack.Pop=Called then
begin
g.cEval;
end;
g.cSet;
end;
procedure TObjLang.CompilerOnR20(Sender: TCompiler; b: TBush);
begin
CompilerOnR23(Sender,b);
end;
procedure TObjLang.CompilerOnR21(Sender: TCompiler; b: TBush);
begin
CompilerOnR24(Sender,b);
end;
procedure TObjLang.CompilerOnR22(Sender: TCompiler; b: TBush);
begin
CompilerOnR25(Sender,b);
end;
procedure TObjLang.CompilerOnR23(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
g.cIF;
Compiler.CompileNode(b[3]);
Compiler.CompileNode(b[4]);
end;
procedure TObjLang.CompilerOnR24(Sender: TCompiler; b: TBush);
begin
g.cTHEN;
end;
procedure TObjLang.CompilerOnR25(Sender: TCompiler; b: TBush);
begin
g.cELSE;
Compiler.CompileNode(b[1]);
g.cTHEN;
end;
procedure TObjLang.CompilerOnR27(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
Compiler.CompileNode(b[0]);
end;
procedure TObjLang.CompilerOnR28(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[2]);
Compiler.CompileNode(b[1]);
g.cNot;
end;
procedure TObjLang.CompilerOnR30(Sender: TCompiler; b: TBush);
begin
g.cEQU;
end;
procedure TObjLang.CompilerOnR31(Sender: TCompiler; b: TBush);
begin
g.cNE;
end;
procedure TObjLang.CompilerOnR32(Sender: TCompiler; b: TBush);
begin
g.cL;
end;
procedure TObjLang.CompilerOnR33(Sender: TCompiler; b: TBush);
begin
g.cG;
end;
procedure TObjLang.CompilerOnR34(Sender: TCompiler; b: TBush);
begin
g.cLE;
end;
procedure TObjLang.CompilerOnR35(Sender: TCompiler; b: TBush);
begin
g.cGE;
end;
procedure TObjLang.CompilerOnR39(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
g.cNegate;
end;
procedure TObjLang.CompilerOnR41(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
Compiler.CompileNode(b[0]);
end;
procedure TObjLang.CompilerOnR43(Sender: TCompiler; b: TBush);
begin
g.cAdd;
end;
procedure TObjLang.CompilerOnR44(Sender: TCompiler; b: TBush);
begin
g.cSub;
end;
procedure TObjLang.CompilerOnR45(Sender: TCompiler; b: TBush);
begin
g.cOr;
end;
procedure TObjLang.CompilerOnR48(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
Compiler.CompileNode(b[0]);
end;
procedure TObjLang.CompilerOnR50(Sender: TCompiler; b: TBush);
begin
g.cMul;
end;
procedure TObjLang.CompilerOnR51(Sender: TCompiler; b: TBush);
begin
g.cDiv;
end;
procedure TObjLang.CompilerOnR54(Sender: TCompiler; b: TBush);
begin
g.cAnd;
end;
procedure TObjLang.CompilerOnR55(Sender: TCompiler; b: TBush);
Var N:extended;
C:Integer;
begin
Val(NodeToStr(b[0]),N,c);
g.cFloat(N);
end;
procedure TObjLang.CompilerOnR56(Sender: TCompiler; b: TBush);
begin
Compiler.COmpileNode(b[0]);
if g.Stack.Pop=Called then
begin
g.cEval
end
else
begin
g.cGet;
end;
end;
procedure TObjLang.CompilerOnR58(Sender: TCompiler; b: TBush);
begin
CompilerOnR59(Sender,b);
end;
procedure TObjLang.CompilerOnR59(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
g.cNot;
end;
procedure TObjLang.CompilerOnR61(Sender: TCompiler; b: TBush);
begin
g.cBEGIN;
Compiler.CompileNode(b[1]);
g.cWHILE;
Compiler.CompileNode(b[3]);
g.cREPEAT;
end;
procedure TObjLang.CompilerOnR62(Sender: TCompiler; b: TBush);
begin
CompilerOnR61(Sender,b);
end;
procedure TObjLang.CompilerOnR65(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[1]);
g.cTIMES;
Compiler.CompileNode(b[3]);
g.cLOOP;
end;
procedure TObjLang.CompilerOnR64(Sender: TCompiler; b: TBush);
begin
CompilerOnR65(Sender,b);
end;
procedure TObjLang.CompilerOnR70(Sender: TCompiler; b: TBush);
begin
g.Stack.Push(0);
Compiler.CompileNode(b[1]);
g.cInt(g.Stack.Pop);
end;
procedure TObjLang.CompilerOnR71(Sender: TCompiler; b: TBush);
begin
Compiler.CompileNode(b[0]);
g.Stack.Push(g.Stack.Pop+1);
Compiler.CompileNode(b[1]);
end;
procedure TObjLang.CompilerOnR74(Sender: TCompiler; b: TBush);
begin
g.cStr(StrConstOf(b[0]));
end;
procedure TObjLang.CompilerOnR75(Sender: TCompiler; b: TBush);
begin
g.cBool(true);
end;
procedure TObjLang.CompilerOnR76(Sender: TCompiler; b: TBush);
begin
g.cBool(false);
end;
procedure TObjLang.CompilerOnR77(Sender: TCompiler; b: TBush);
begin
g.cBool(true);
end;
procedure TObjLang.CompilerOnR78(Sender: TCompiler; b: TBush);
begin
g.cBool(false);
end;
end.