Add paxPascal


paxPascal implements a subset of Object Pascal. For example, the following script is admissible:

  1. program P;
  2. type
  3.   TMyClass = class;
  4.  
  5.   TMyArray = array[1..3] of String;
  6.  
  7.   TMyClass = class(TObject)
  8.   private
  9.     function GetProp: Integer;
  10.   public
  11.     function MyFunc(U: Integer; V: Integer): Integer;
  12.     procedure MyProc;
  13.     property MyProp: Integer read GetProp;
  14.   end;
  15.  
  16.   TMyRec = record
  17.     X, Y: Integer;
  18.   end;
  19.  
  20. function GlobalFunc: Integer; forward;
  21.  
  22. function TMyClass.MyFunc(U: Integer; V: Integer): Integer;
  23. begin
  24.   result := U + V + GlobalFunc;
  25. end;
  26.  
  27. procedure TMyClass.MyProc;
  28. begin
  29.   writeln('here');
  30. end;
  31.  
  32. function TMyClass.GetProp: Integer;
  33. begin
  34.   result := 11;
  35. end;
  36.  
  37. function GlobalFunc: Integer;
  38. begin
  39.   result := 700;
  40. end;
  41.  
  42. var
  43.   X: TMyClass;
  44.   A: TMyArray;
  45.   R: TMyRec;
  46. begin
  47.   X := TMyClass.Create;
  48.   A[1] := 'abc';
  49.   A[2] := 'pqr';
  50.   A[3] := 'uv';
  51.   R.X := 100;
  52.   writeln(X.MyFunc(4, 5));
  53.   X.MyProc;
  54.   writeln(X.MyProp);
  55.   writeln(R.X);
  56. end;


From another hand, paxPascal has a few extra features which are absent in the Object Pascal.


Program Structure

A paxPascal program is the list of statements. In this relation, paxPascal is more similar with
VBScript or JavaScript.

For example, the following script

  1. program P;
  2. type
  3.   TMyRec = record
  4.     X, Y: Integer;
  5.   end;
  6. var
  7.   R: TMyRec;
  8. begin
  9.   print R;
  10. end.


contains four executable statements:

program P;

Creates namespace P.

  1. type
  2.   TMyRec = record
  3.     X, Y: Integer;
  4.   end;


Creates record type TMyRec.

  1. var
  2.   R: TMyRec;


Creates variable R.

print R;

Printes variable R.


Declaration of Variables

You can omit declaration of a variable. In such case, the variable obtains Variant type.
For example, the following script

  1. function MyFunc(X);
  2. var Y;
  3. begin
  4.   Y := Random(100);
  5.   result := X + Y + Y;
  6. end;


is equivalent to

  1. function MyFunc(X: Variant): Variant;
  2. var Y: Variant;
  3. begin
  4.   Y := Random(100);
  5.   result := X + Y + Y;
  6. end;


You can join declaration of a variable with its initialization. Local variables are available for
initialization too:

  1. procedure MyProc;
  2. var
  3.   X: Integer = Random(100);
  4.   Y = 54.3;
  5. begin
  6. ..................
  7. end;



Class Types

paxPascal supports shared(static) fields of classes. The definition of a static member must begin with the reserved word class. For example,

  1. type
  2.   TFoo = class (TObject)
  3.     class var X: String;
  4.     class function F(): Integer;
  5.     class procedure P();
  6.   end;


You can initialize fields of classes:

  1. type
  2.   TMyClass = class(TObject)
  3.     X: Integer = 100;
  4.     S: String = 'abc';
  5.     constructor Create;
  6. ........................
  7.    end;


You can define a method's body directly in the class declaration:

  1. type
  2.   TMyForm = class(TForm)
  3.      constructor Create(Owner: TComponent);
  4.      begin
  5.        inherited;
  6.        Top := 100;
  7.        Left := 200;
  8.        Caption := 'MyForm';
  9.      end;
  10.   end;


Note, that you cannot use nested routines in such case. If you want to use the nested routines
inside of the method body, you must define such method outside of the class declaration.



Record Types

You can use methods and properties of records:

  1. type
  2.   TPoint = record
  3.   private
  4.     function GetNorm: Integer;
  5.     begin
  6.       result := X*X + Y*Y;
  7.     end;
  8.   public
  9.     X, Y: Integer;
  10.     property Norm: Integer read GetNorm;
  11.   end;


Note, that you cannot use nested routines in such case. If you want to use the nested routines
inside of the method body, you must define such method outside of the record declaration.


You can initialize fields of records:

  1. type
  2.   TRandomPoint = record
  3.     X: Integer = Random(100);
  4.     Y: Integer = Random(100);
  5.   end;


Method Initialize gives you one more way to initialize the record fields:

  1. type
  2.   TRandomPoint = record
  3.     X, Y: Integer;
  4.     procedure Initialize;
  5.     begin
  6.       X := Random(100);
  7.       Y := Random(100);
  8.     end;
  9.   end;


You can inherit the record types:

  1. type
  2.   TRandomCircle = record (TRandomPoint)
  3.     R: Integer = Random(100);
  4.   end;



Array Types

paxPascal supports 2 kinds of arrays:

Native arrays:

  1. var A[10, 2];


The native array is multi-dimensional zero-based array with elements of Variant type.

Object Pascal arrays:

  1. type
  2.   TMyArray[1..2, 3..5] of TPoint;


This kind of arrays is derived from native arrays at compile-time. For example, declaration

  1. type
  2.   TMyArray = array [3..5] of Integer;


is equivalent to

  1. type TMyArray = record
  2.   var L = 3;
  3.   var H = 5;
  4.   var fItems[H - L];
  5.   procedure Initialize;
  6.   var
  7.     I: Integer;
  8.   begin
  9.     for I:=L to H do
  10.      fItems[I - L] := 0;
  11.   end;
  12.   function GetItem(I: Integer): Integer;
  13.   begin
  14.     result := @ fItems[I - L];
  15.   end;
  16.   procedure SetItem(I: Integer; Value: Integer);
  17.   begin
  18.     fItems[I - L] := Value;
  19.   end;
  20.   property Items[I: Integer]: Integer; read GetItem write SetItem; default;
  21. end;


You can allocate your arrays dynamically:

  1. procedure P(N: Integer);
  2. var
  3.   A: array[3..N] of Double;
  4. begin
  5. ...............
  6. end;



Namespaces

paxPascal supports namespaces. For example:

  1. namespace Shapes
  2.   type
  3.     TPoint = class(TObject)
  4.        X, Y: Integer;
  5.        constructor Create(X, Y: Integer);
  6.        begin
  7.          inherited Create;
  8.          Self.X := X;
  9.          Self.Y := Y;
  10.        end;
  11.     end;
  12.  
  13.     TCircle = class(TPoint)
  14.       R: Integer;
  15.       constructor Create(X, Y, R);
  16.     end;
  17.    
  18.   constructor TCircle.Create(X, Y, R);
  19.   begin
  20.     inherited Create(X, Y);
  21.     Self.R := R;
  22.   end;
  23.  
  24. end;
  25.  
  26. var
  27.   Point = Shapes.TPoint.Create(3, 5);
  28.   Circle = Shapes.TCircle.Create(3, 5, 7);
  29. begin
  30.   print Point, Circle;
  31. end.



Forward Declarations

You need no to use forward declarations in paxPascal. In another words, you can use a function
call before declaration of the function. For example:

Fact(5);
  1. function Fact(N: Integer): Integer;
  2. begin
  3.   if N = 1 then
  4.     result := 1
  5.   else
  6.     result := N * Fact(N - 1);
  7. end;



Syntax

Remember, that paxPascal program is list of statements. Therefore you can disregard Object Pascal synax rules to make your scripts more laconic. For example, the following scripts are admissible:
  1.  
  2. print "Hello, world!";
  3.  
  4. type
  5.   TMyRec = record
  6.     X, Y: Integer;
  7.   end;
  8. var
  9.   R: TMyRec;
  10. begin
  11.   R.X := 10;
  12.   Y.Y := 12;
  13. end;
  14.  
  15. record TMyRec
  16.   X, Y: Integer;
  17. end;
  18. var
  19.   R: TMyRec;
  20.   R.X := 10;
  21.   Y.Y := 12;

(this is equivalent of the script above).
Latest News
Submit News Form Past News
Latest Forum Entries