home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / Chip_2000-02_cd.bin / zkuste / Delphi / navody / tt / objvm.exe / UNITS / ObjOps.pas < prev    next >
Pascal/Delphi Source File  |  1999-05-29  |  14KB  |  509 lines

  1. unit ObjOps;
  2.  
  3. interface
  4. uses ObjOp,
  5.      OpList,OpCodes,SysUtils;
  6. procedure Register(a:TOpList);
  7. implementation
  8. uses
  9.     uExecution,LangValue,VarLangValue;
  10. //ocNop    =-40;
  11. type TNoOp=class(TObjOp)
  12.         procedure Execute(e:IExecution);override;
  13.      end;
  14. procedure TNoOp.Execute;
  15.           begin
  16.             e.IP:=e.IP+SIzeOf(OpCode);
  17.           end;
  18. //Arithmetic operations
  19. type TBinOp=class(TNoOp)
  20.      protected
  21.        function DoOp(a,b:ILangValue):ILangValue;virtual;
  22.      public
  23.        procedure Execute(e:IExecution);override;
  24.      end;
  25. procedure TBinOp.Execute;
  26.           Var a,b:ILangValue;
  27.           begin
  28.             Inherited Execute(e);
  29.             a:=e.Data.Pop;
  30.             b:=e.Data.Pop;
  31.             e.Data.Push(DoOp(a,b));
  32.             a.Free;
  33.             b.Free;
  34.           end;
  35. function  TBinOp.DoOp;
  36.           begin
  37.             Result:=TVarLangValue.Create(nil);
  38.           end;
  39.  
  40. //ocAdd    =-1;
  41. type TAddOp=class(TBinOp)
  42.        function DoOp(a,b:ILangValue):ILangValue;override;
  43.      protected
  44.      end;
  45. function TAddOp.DoOp;
  46.          begin
  47.            Result:=Inherited DoOp(a,b);
  48.            Result.AsVariant:=b.AsVariant+a.AsVariant;
  49.          end;
  50. //ocSub    =-2;
  51. type TSubOp=class(TBinOp)
  52.        function DoOp(a,b:ILangValue):ILangValue;override;
  53.      protected
  54.      end;
  55. function TSubOp.DoOp;
  56.          begin
  57.            Result:=Inherited DoOp(a,b);
  58.            Result.AsVariant:=b.AsVariant-a.AsVariant;
  59.          end;
  60. //ocMul    =-3;
  61. type TMulOp=class(TBinOp)
  62.        function DoOp(a,b:ILangValue):ILangValue;override;
  63.      protected
  64.      end;
  65. function TMulOp.DoOp;
  66.          begin
  67.            Result:=Inherited DoOp(a,b);
  68.            Result.AsVariant:=a.AsVariant*b.AsVariant;
  69.          end;
  70. //ocDiv    =-4;
  71. type TDivOp=class(TBinOp)
  72.        function DoOp(a,b:ILangValue):ILangValue;override;
  73.      protected
  74.      end;
  75. function TDivOp.DoOp;
  76.          begin
  77.            Result:=Inherited DoOp(a,b);
  78.            Result.AsVariant:=b.AsVariant/a.AsVariant;
  79.          end;
  80. //ocNegate
  81. type TNegateOp=class(TNoOp)
  82.      public
  83.        procedure Execute(e:IExecution);override;
  84.      end;
  85.  procedure TNegateOp.Execute;
  86.            Var a:ILangValue;
  87.            begin
  88.              inherited Execute(e);
  89.              a:=e.Data.Pop;
  90.              a.AsVariant:=-a.AsVariant;
  91.              e.Data.Push(a);
  92.            end;
  93. //Boolean operations
  94. //ocAnd    =-11;
  95. type TAndOp=class(TBinOp)
  96.        function DoOp(a,b:ILangValue):ILangValue;override;
  97.      protected
  98.      end;
  99. function TAndOp.DoOp;
  100.          begin
  101.            Result:=Inherited DoOp(a,b);
  102.            Result.AsBoolean:=a.AsBoolean and b.AsBoolean;
  103.          end;
  104. //ocOr     =-12;
  105. type TOrOp=class(TBinOp)
  106.        function DoOp(a,b:ILangValue):ILangValue;override;
  107.      protected
  108.      end;
  109. function TOrOp.DoOp;
  110.          begin
  111.            Result:=Inherited DoOp(a,b);
  112.            Result.AsBoolean:=a.AsBoolean or b.AsBoolean;
  113.          end;
  114. //ocNot    =-13;
  115. type TNotOp=class(TNoOp)
  116.      public
  117.        procedure Execute(e:IExecution);override;
  118.      end;
  119.  procedure TNotOp.Execute;
  120.            Var a:ILangValue;
  121.            begin
  122.              inherited Execute(e);
  123.              a:=e.Data.Pop;
  124.              a.AsBoolean:=not a.AsBoolean;
  125.              e.Data.Push(a);
  126.            end;
  127.  
  128. {Comparison ops}
  129. //ocEqu    =-14
  130. type TEquOp=class(TBinOp)
  131.        function DoOp(a,b:ILangValue):ILangValue;override;
  132.      protected
  133.      end;
  134. function TEquOp.DoOp;
  135.          begin
  136.            Result:=Inherited DoOp(a,b);
  137.            Result.AsBoolean:=a.AsVariant=b.AsVariant;
  138.          end;
  139. //ocNE     =-15
  140. type TNEOp=class(TBinOp)
  141.        function DoOp(a,b:ILangValue):ILangValue;override;
  142.      protected
  143.      end;
  144. function TNEOp.DoOp;
  145.          begin
  146.            Result:=Inherited DoOp(a,b);
  147.            Result.AsBoolean:=b.AsVariant<>a.AsVariant;
  148.          end;
  149. //ocG      =-16
  150. type TGOp=class(TBinOp)
  151.        function DoOp(a,b:ILangValue):ILangValue;override;
  152.      protected
  153.      end;
  154. function TGOp.DoOp;
  155.          begin
  156.            Result:=Inherited DoOp(a,b);
  157.            Result.AsBoolean:=b.AsVariant>a.AsVariant;
  158.          end;
  159. //ocL      =-17
  160. type TLOp=class(TBinOp)
  161.        function DoOp(a,b:ILangValue):ILangValue;override;
  162.      protected
  163.      end;
  164. function TLOp.DoOp;
  165.          begin
  166.            Result:=Inherited DoOp(a,b);
  167.            Result.AsBoolean:=b.AsVariant<a.AsVariant;
  168.          end;
  169. //ocLE     =-18
  170. type TLEOp=class(TBinOp)
  171.        function DoOp(a,b:ILangValue):ILangValue;override;
  172.      protected
  173.      end;
  174. function TLEOp.DoOp;
  175.          begin
  176.            Result:=Inherited DoOp(a,b);
  177.            Result.AsBoolean:=b.AsVariant<=a.AsVariant;
  178.          end;
  179. //ocGE     =-19
  180. type TGEOp=class(TBinOp)
  181.        function DoOp(a,b:ILangValue):ILangValue;override;
  182.      protected
  183.      end;
  184. function TGEOp.DoOp;
  185.          begin
  186.            Result:=Inherited DoOp(a,b);
  187.            Result.AsBoolean:=b.AsVariant>=a.AsVariant;
  188.          end;
  189. //Value operations
  190. //ocGet    =-21;
  191. type TGetOp=class(TNoOp)
  192.      public
  193.        procedure Execute(e:IExecution);override;
  194.      end;
  195.  procedure TGetOp.Execute;
  196.            Var a,b:ILangValue;
  197.  
  198.            begin
  199.              inherited Execute(e);
  200.              a:=e.Data.Pop;
  201.              b:=TVarLangValue.Create(nil);
  202.              b.AsInteger:=0;
  203.              e.Data.Push(b);
  204.              a.Exec(e.Data,true);
  205.              a.Free;
  206.            end;
  207. //ocSet    =-22;
  208. type TSetOp=class(TNoOp)
  209.      public
  210.        procedure Execute(e:IExecution);override;
  211.      end;
  212.  procedure TSetOp.Execute;
  213.            Var a,b:ILangValue;
  214.            begin
  215.              inherited Execute(e);
  216.              a:=e.Data.Pop;
  217.              b:=TVarLangValue.Create(nil);
  218.              b.AsInteger:=1;
  219.              e.Data.Push(b);
  220.              a.Exec(e.Data,false);
  221.              a.Free;
  222.            end;
  223.  
  224. //ocExec   =-23;
  225. type TExecOp=class(TNoOp)
  226.      public
  227.        procedure Execute(e:IExecution);override;
  228.      end;
  229.  procedure TExecOp.Execute;
  230.            Var a,b:ILangValue;
  231.            begin
  232.              inherited Execute(e);
  233.              a:=e.Data.Pop;
  234.              a.Exec(e.Data,false);
  235.              a.Free;
  236.            end;
  237. //ocEval   =-24;
  238. type TEvalOp=class(TNoOp)
  239.      public
  240.        procedure Execute(e:IExecution);override;
  241.      end;
  242.  procedure TEvalOp.Execute;
  243.            Var a,b:ILangValue;
  244.            begin
  245.              Inherited Execute(e);
  246.              a:=e.Data.Pop;
  247.              a.Exec(e.Data,true);
  248.              a.Free;
  249.            end;
  250. //ocGetElem=-25;
  251. type TGetElemOp=class(TNoOp)
  252.      public
  253.        procedure Execute(e:IExecution);override;
  254.      end;
  255.  procedure TGetElemOp.Execute;
  256.            Var a,b,c:ILangValue;
  257.                S:string;
  258.            begin
  259.              inherited Execute(e);
  260.              a:=e.Data.Pop;
  261.              b:=e.Data.Pop;
  262.              s:=a.AsSTring;
  263.              c:=b.Values[a.AsString];
  264.              e.Data.Push(c);
  265.              b.Free;
  266.              a.Free;
  267.              if c=nil then
  268.                 Raise Exception.Create('╚Σσφ≥Φ⌠ΦΩα≥ε≡ φσ φαΘΣσφ - '+s);
  269.            end;
  270. //ocRoot   =-26;
  271. type TRootOp=class(TNoOp)
  272.      public
  273.        procedure Execute(e:IExecution);override;
  274.      end;
  275.  procedure TRootOp.Execute;
  276.            begin
  277.              inherited Execute(e);
  278.              e.Data.Push(e.Root.CreateEqu);
  279.            end;
  280. //Constant operations
  281. //ocStr    =-31;
  282. type TStrOp=class(TNoOp)
  283.      public
  284.        procedure Execute(e:IExecution);override;
  285.      end;
  286.  procedure TStrOp.Execute;
  287.            Var a:ILangValue;
  288.            begin
  289.              inherited Execute(e);
  290.              a:=TVarLangValue.Create(nil);
  291.              a.AsString:=e.Code.Str[e.IP];
  292.              e.Data.Push(a);
  293.              e.IP:=e.IP+Length(a.AsString)+1;
  294.            end;
  295. //ocFloat  =-32;
  296. type TFloatOp=class(TNoOp)
  297.      public
  298.        procedure Execute(e:IExecution);override;
  299.      end;
  300.  procedure TFloatOp.Execute;
  301.            Var a:ILangValue;
  302.            begin
  303.              inherited Execute(e);
  304.              a:=TVarLangValue.Create(nil);
  305.              a.AsFloat:=e.Code.Num[e.IP];
  306.              e.Data.Push(a);
  307.              e.IP:=e.IP+SizeOf(Extended);
  308.            end;
  309. //ocInt    =-33;
  310. type TIntOp=class(TNoOp)
  311.      public
  312.        procedure Execute(e:IExecution);override;
  313.      end;
  314.  procedure TIntOp.Execute;
  315.            Var a:ILangValue;
  316.            begin
  317.              inherited Execute(e);
  318.              a:=TVarLangValue.Create(nil);
  319.              a.AsInteger:=e.Code.Int[e.IP];
  320.              e.Data.Push(a);
  321.              e.IP:=e.IP+SizeOf(Integer);
  322.            end;
  323. //ocBool   =-34;
  324. type TBoolOp=class(TNoOp)
  325.      public
  326.        procedure Execute(e:IExecution);override;
  327.      end;
  328.  procedure TBoolOp.Execute;
  329.            Var a:ILangValue;
  330.            begin
  331.              inherited Execute(e);
  332.              a:=TVarLangValue.Create(nil);
  333.              a.AsBoolean:=e.Code.Elements[e.IP]<>0;
  334.              e.Data.Push(a);
  335.              inc(e.IP,SIzeof(Byte));
  336.            end;
  337. //Execution flow operations
  338. //ocHalt   =-41;
  339. type THaltOp=class(TObjOp)
  340.      public
  341.        procedure Execute(e:IExecution);override;
  342.      end;
  343.  procedure THaltOp.Execute;
  344.            begin
  345.              e.IsEnd:=true;
  346.            end;
  347. //ocJZ     =-42;
  348. type TJZOp=class(TNoOp)
  349.      public
  350.        procedure Execute(e:IExecution);override;
  351.      end;
  352.  procedure TJZOp.Execute;
  353.            Var a:ILangValue;
  354.            begin
  355.              Inherited Execute(e);
  356.              a:=e.Data.Pop;
  357.              if a.AsBoolean then
  358.              begin
  359.                e.IP:=e.Code.Int[e.IP];
  360.              end
  361.              else
  362.                e.IP:=e.IP+SizeOf(Integer);
  363.            end;
  364.  
  365. //ocJNZ    =-43;
  366. type TJNZOp=class(TNoOp)
  367.      public
  368.        procedure Execute(e:IExecution);override;
  369.      end;
  370.  procedure TJNZOp.Execute;
  371.            Var a:ILangValue;
  372.            begin
  373.              Inherited Execute(e);
  374.              a:=e.Data.Pop;
  375.              if not a.AsBoolean then
  376.              begin
  377.                e.IP:=e.Code.Int[e.IP];
  378.              end
  379.              else
  380.                e.IP:=e.IP+SizeOf(Integer);
  381.              a.Free;
  382.            end;
  383. //ocJMP    =-44;
  384. type TJMPOp=class(TNoOp)
  385.      public
  386.        procedure Execute(e:IExecution);override;
  387.      end;
  388.  procedure TJMPOp.Execute;
  389.            begin
  390.              Inherited Execute(e);
  391.              e.IP:=e.Code.Int[e.IP];
  392.            end;
  393. //ocJRZ
  394. type TJRZOp=class(TNoOp)
  395.      public
  396.        procedure Execute(e:IExecution);override;
  397.      end;
  398.  procedure TJRZOp.Execute;
  399.            Var a:ILangValue;
  400.            begin
  401.              Inherited Execute(e);
  402.              a:=e.Return.Pop;
  403.              if not a.AsBoolean then
  404.              begin
  405.                e.IP:=e.Code.Int[e.IP];
  406.              end
  407.              else
  408.                e.IP:=e.IP+SizeOf(Integer);
  409.              e.Return.Push(a);
  410.            end;
  411. //ocDcrR
  412. type TDcrROp=class(TNoOp)
  413.      public
  414.        procedure Execute(e:IExecution);override;
  415.      end;
  416.  procedure TDcrROp.Execute;
  417.            Var a:ILangValue;
  418.            begin
  419.              Inherited Execute(e);
  420.              a:=e.Return.Pop;
  421.              a.AsInteger:=a.AsInteger-1;
  422.              e.Return.Push(a);
  423.            end;
  424. //ocToR
  425. type TToROp=class(TNoOp)
  426.      public
  427.        procedure Execute(e:IExecution);override;
  428.      end;
  429.  procedure TToROp.Execute;
  430.            Var a:ILangValue;
  431.            begin
  432.              Inherited Execute(e);
  433.              a:=e.Data.Pop;
  434.              e.Return.Push(a);
  435.            end;
  436. //ocFromR
  437. type TFromROp=class(TNoOp)
  438.      public
  439.        procedure Execute(e:IExecution);override;
  440.      end;
  441.  procedure TFromROp.Execute;
  442.            Var a:ILangValue;
  443.            begin
  444.              Inherited Execute(e);
  445.              a:=e.Return.Pop;
  446.              e.Data.Push(a);
  447.            end;
  448. //ocRDrop
  449. type TRDropOp=class(TNoOp)
  450.      public
  451.        procedure Execute(e:IExecution);override;
  452.      end;
  453.  procedure TRDropOp.Execute;
  454.            Var a:ILangValue;
  455.            begin
  456.              Inherited Execute(e);
  457.              a:=e.Return.Pop;
  458.              a.Free;
  459.            end;
  460.  
  461. procedure Register(a:TOpList);
  462.           begin
  463.             WIth a do
  464.             begin
  465.                Register(ocNop,TNoOp);
  466.                //Arithmetic operations
  467.                Register(ocAdd,TAddOp);
  468.                Register(ocSub,TSubOp);
  469.                Register(ocMul,TMulOp);
  470.                Register(ocDiv,TDivOp);
  471.                Register(ocNegate,TNegateOp);
  472.                //Boolean operations
  473.                Register(ocAnd,TAndOp);
  474.                Register(ocOr,TOrOp);
  475.                Register(ocNot,TNotOp);
  476.                Register(ocEqu,TEquOp);
  477.                Register(ocNE,TNEOp);
  478.                Register(ocG,TGOp);
  479.                Register(ocL,TLOp);
  480.                Register(ocLE,TLEOp);
  481.                Register(ocGE,TGEOp);
  482.            //Value operations
  483.                Register(ocGet,TGetOp);
  484.                Register(ocSet,TSetOp);
  485.                Register(ocExec,TExecOp);
  486.                Register(ocEval,TEvalOp);
  487.                Register(ocGetElem,TGetElemOp);
  488.                Register(ocRoot,TRootOp);
  489.                //Constant operations
  490.                Register(ocStr,TStrOp);
  491.                Register(ocFloat,TFloatOp);
  492.                Register(ocInt,TIntOp);
  493.                Register(ocBool,TBoolOp);
  494.                //Execution flow operations
  495.                Register(ocHalt,THaltOp);
  496.                Register(ocJZ,TJZOp);
  497.                Register(ocJMP,TJMPOp);
  498.                Register(ocJNZ,TJNZOp);
  499.                Register(ocJRZ,TJRZOp);
  500.                Register(ocDcrR,TDcrROp);
  501.                Register(ocToR,TToROp);
  502.                {Moves value from return stack to data stack}
  503.                Register(ocFromR,TFromROp);
  504.                {Drops value from reaturn stack}
  505.                Register(ocRDrop,TRDropOp);
  506.             end;
  507.           end;
  508. end.
  509.