[fpc-pascal] PasJS

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Mon Sep 28 10:45:36 CEST 2009

2009/9/26 Michael Van Canneyt <michael at freepascal.org>

> On Fri, 25 Sep 2009, Jorge Aldo G. de F. Junior wrote:
>  Hi !
>> Im working on the PasJS translator but im having some problems :
>> How to map ObjectPascal object orientation philosophy into JavaScript
>> object orientation philoshopy ?
> Do you think there is a choice ? In JS, everything is an object. As far as
> I can see, there is not really much of a choice ? You should add the
> methods, fields and properties to the object's prototype ?
Yep, but this would mean that :

1 - Records are objects
2 - How to map arrays ?
3 - How to deal with class inheritance ? (JS is a freak one at this)

>  Im trying a direct Syntax Tree -> Output approach, while outputting (if a
>> debug flag is enabled) comments containning original line of code, original
>> line
>> number and original column number as to allow easier debugging.
>> I gave up trying to do semantic analisys at the translator because its too
>> hard (its like almost creating a new FPC compiler from scratch, so i believe
>> letting
>> the browser debug for us - and having the line numbers to check for
>> mistakes - its the best approach).
>> But i need volunteers !
> A Pascal -> JS translator is on my todo list, but for that I need to extend
> the
> pparser unit in fcl-passrc to read complete units, not just the interface.

I dont know your pparser, but i have a working parser here, thats not the
current problem.

Im working at the Syntax Tree of Object pascal (Im currently lazy, cant
concentrate to write this)

but the model is quite simple :

Theres a root class called TTreeElement

Each pascal construction is an descendant of TTreeElement

For one :

TIdentifierSyntax = Class(TTreeElement)
  Constructor Create(aSource : TTokenIterator; aOwner : TTreeElement);

The constructor simply reads tokens from TTokenIterator, creating new
objects (Childs in the tree) as its needed. Nothing fancy.

Something like

Constructor TIfSyntax.Create(aSource : TTokenIterator; aOwner :
   Inherited Create(aSource, aOwner);
   aSource.Consume('if'); // <- consumes the "if" token
    AddChild(TExpressionSyntax.Create(aSource, Self)); // <- makes a child
that will consume the expression that must follow the if
    AddChild(TStamentSyntax.Create(aSource, Self)); <- consume the
stament/codeblock that follows the "then"
    If aSource.Consume('else') Then <- consume results true if its able to
consume "else"
       AddChild(TElseStamentSyntax.Create(aSource, Self)); <- If theres an
else, theres a stament...

And so on...

Each TTreeElement has its corresponding token (even the ones that doenst
corresponds to a token in the source)

so its easy to add a Function GenerateJS : String; to each one that
translates it on the fly into javascript commands... (Well, i will need a
symbol tree to make more complex things like translating Classes into JS

My parser is able to do some pre-processing, like {$ifdef} {$else} {$endif}
{$define} {$undefine} etc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20090928/442e6b4c/attachment.html>

More information about the fpc-pascal mailing list