Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-09

PasJS

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jorge Aldo G. de F. Junior

Hi !

Im working on the PasJS translator but im having some problems :

How to map ObjectPascal object orientation philosophy into JavaScript object orientation philoshopy ?

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 !

Reply by email

Michael Van Canneyt
to Jorge Aldo G. de F. Junior
Jorge Aldo G. de F. Junior wrote 5 lines
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 ?

Show quoted text 7 lines
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.

Michael.

Reply by email

Jorge Aldo G. de F. Junior
to Michael Van Canneyt

2009/9/26 Michael Van Canneyt

Show quoted text 17 lines
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)

Show quoted text 18 lines
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)
Public
  Constructor Create(aSource : TTokenIterator; aOwner : TTreeElement);
End

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 :
TTreeElement);
Begin
   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
    aSource.Consume('then');
    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...
End;

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 Classes)

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

Reply by email