Using C-ish code in FreePascal

I really like the pascal syntax and I really like C# syntax, so how can I mix those two together? I wrote a parser which can manage that task.

The following snippet looks like a mixture between C#, C++, Objective-C and JavaScript:

namespace testunit;

@interface

using
  SysUtils;

type
  MyClass : class {
  private
    fMyString: String;

    void addMyself();
  public
    function addThat(): int;
  published
    property MyString: String get fMyString;
  }

@implementation

  void MyClass::addMyself();
  {
  	if (fMyString == "") then
  	{
  	  fMyString = "This is my string.";
  	}
  	else
  	{
  	  fMyString = "This is my new string.";
  	}
  }

  function MyClass::addThat(): int;
  {

  }

@end

Using my little parser I created last night the above code will be converted into:

unit testunit;

interface

uses
  SysUtils;

type
  MyClass = class
  private
    fMyString: String;

    procedure addMyself();
  public
    function addThat(): Integer;
  published
    property MyString: String read fMyString;
  end;

implementation

  procedure MyClass.addMyself();
  begin
  	if (fMyString = '') then
  	begin
  	  fMyString := 'This is my string.';
  	end
  	else
  	begin
  	  fMyString := 'This is my new string.';
  	end;
  end;

  function MyClass.addThat(): Integer;
  begin

  end;

end.

Now the code can be compiled with FreePascal :)
Well, it’s not really something sophicated or special, just a little toy. There are still some bugs in my parser, well, maybe I might release it some day as open source.

Leave a Reply