Recent

Author Topic: Reusing the internals  (Read 2780 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: 2782
  • 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.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
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: 2513
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: 3616
  • 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!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8103
  • 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.