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