Recent

Author Topic: Reusing the internals  (Read 3879 times)

int-index

  • Guest
Reusing the internals
« on: February 01, 2015, 10:44:45 pm »
I am developing a translator from Pascal to Haskell (source here: https://github.com/int-index/sodium) and instead of reimplementing a parser and a typechecker for Pascal, I would really like to reuse the ones in FPC. So I need to modify FPC somehow to make it emit parsed type-annotated version of source code in some easy to parse format (such as XML).

Basically the idea is to get from this:
Code: [Select]
var x: Integer; begin x := ...; WriteLn(x); end;to something like this:
Code: [Select]
<source>
       <variables>
              <variable name="x" type="Integer"></variable>
       </variables>
       <body>
              <assignment>
                     <lhs><variable name="x"></variable></lhs>
                     <rhs>...</rhs>
              </assignment>
              <procedure-call name="WriteLn">
                     <argument>
                       <function-call special="Integer-to-String">
                           <variable-access name="x"></variable-access>
                       </function-call>
                     </argument>
              </procedure-call>
          </body>
   </source>

Note the added type-conversion (reusing FPC typechecker and semantic analysis).

My questions are: is this possible? Is it difficult? Where do I start if I want to implement it?
« Last Edit: February 01, 2015, 10:46:52 pm by int-index »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Reusing the internals
« Reply #1 on: February 01, 2015, 11:23:01 pm »
Maybe you should ask on FPC-Pascal or FPC-Devel mailing list, you have better chance to meet freepascal developers there.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Reusing the internals
« Reply #2 on: February 02, 2015, 01:28:05 am »
Take a look here. It advices using fcl-passrc and the example test_parser in packages\fcl-passrc\examples.

One more thing: WriteLn is not a real procedure call. The compiler generates suitable set of calls based on the number and types of parameters passed to WriteLn.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Reusing the internals
« Reply #3 on: February 02, 2015, 07:19:24 am »
I am developing a translator from Pascal to Haskell

Heh, what will you do with Pascal variables and loops?
Anyway, good luck!
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Reusing the internals
« Reply #4 on: February 02, 2015, 07:44:35 am »
is this possible?
certainly
Is it difficult?
depending on your knowledge and skill
Where do I start if I want to implement it?
fpc -vp <your program or unit> produces tree.log in a LISP-like notation, though I'm afraid not enough for your approach for the purpose. Using fcl-passrc classes and extending them with appropriate backend for XML/Haskell (whatever you want) as engkin said will be a better one.

 

TinyPortal © 2005-2018