Forum > FPC development

Reusing the internals

(1/1)

int-index:
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: ---var x: Integer; begin x := ...; WriteLn(x); end;
--- End code ---
to something like this:

--- Code: ---<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>
--- End code ---

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?

Blaazen:
Maybe you should ask on FPC-Pascal or FPC-Devel mailing list, you have better chance to meet freepascal developers there.

engkin:
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:

--- Quote from: int-index on February 01, 2015, 10:44:45 pm ---I am developing a translator from Pascal to Haskell

--- End quote ---

Heh, what will you do with Pascal variables and loops?
Anyway, good luck!

Leledumbo:

--- Quote from: int-index on February 01, 2015, 10:44:45 pm ---is this possible?

--- End quote ---
certainly

--- Quote from: int-index on February 01, 2015, 10:44:45 pm ---Is it difficult?

--- End quote ---
depending on your knowledge and skill

--- Quote from: int-index on February 01, 2015, 10:44:45 pm ---Where do I start if I want to implement it?

--- End quote ---
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.

Navigation

[0] Message Index

Go to full version