[fpc-devel] Some compiler changes...

Thorsten Engler thorsten.engler at gmx.net
Tue Jan 23 03:00:03 CET 2007


While playing around with the compiler source to familiarize myself I've
produced the attached patch. While I didn't set out to produce anything for
inclusion into the official codebase at least some of the changes might be
generally useful.

a) now modeswitch: m_ident_escape

Allows escaping of identifiers which might otherwise be recognized as
keywords. This mirrors Delphi's (and Chrome's) implementation.

type
  TMyRecord = record
    &unit: Integer;
  end;

No space is allowed betweehn & and identifier. As identifiers are not
allowed to start with digits there is no collision with the &123 support for
base 8 integer constants. 

b) new modeswitch: m_always_ident_after_dot

There are no valid cases in the pascal grammer where a keyword follows a
single dot. As a) makes it now possible to declare identifiers which are
identical to keywords qualified access to these fields can now be simplified
by always recognizing an identifier/keyword following a dot as a simple
identifier, no matter if it matches a keyword or not.

I'm not sure if Delphi implements this. Chrome does.

var
  myRecord: TMyRecord;
begin
  myRecord.unit := 123;
end;


These 2 switches should not affect compatibility to existing code. I can run
a complete make all / make install with both of these generally active. It
could be considered to add them to {$mode objfpc}

c) new $mode: m_chrome

Currently baseed on objfpc with the addition of all 3 new mode switches. In
addition to a) and b) m_chrome allows the use of "method" as an alternative
to both procedure and function (if the method is a procedure or function can
simply be determined by the presence of a defined result type).

A slightly extreme example follows:

>>>>
{$mode chrome}

program ChromeTest;

type
  TMyObject = class
    method Foo;
    method Bar: Integer;
    class method ClassFoo;
    class method ClassBar: Integer;

    method &class;
    method &method(&string: string):string;
  end;

type
  &method = method(&string: string):string of object;

{ TMyObject }

method TMyObject.Bar: Integer;
begin
  WriteLn('TMyObject.Bar');
  Result := 0;
end;

method TMyObject.Foo;
begin
  WriteLn('TMyObject.Foo');
end;

class method TMyObject.ClassBar: Integer;
begin
  WriteLn('TMyObject.ClassBar');
  Result := 0;
end;

class method TMyObject.ClassFoo;
begin
  WriteLn('TMyObject.ClassFoo');
end;

method TMyObject.class;
begin
  WriteLn('Class');
end;

method TMyObject.method(&string: string):string;
begin
  Result := '*** ' + &string + ' ***';
end;

method Foo;
begin
  WriteLn('Foo');
end;

method Bar: Integer;
begin
  WriteLn('Bar');
  Result := 0;
end;

var
  &object: TMyObject;
  amethod: &method;

begin
  Foo;
  Bar;
  TMyObject.ClassFoo;
  TMyObject.ClassBar;

  &object := TMyObject.Create;

  &object.Class;

  amethod := @&object.method;
  WriteLn(amethod('method'));

  with &object do try
    Foo;
    Bar;
    &class;
  finally
    Free;
  end;
end.
<<<<


Cheers,
Thorsten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chrome.patch
Type: application/octet-stream
Size: 16311 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20070123/b3c63773/attachment.obj>


More information about the fpc-devel mailing list