Pit - Build libraries
We decided to release the Pit library assemblies so you could also play with the AST and Javascript directly. Download the assemblies + sample project from this link. There are three ways in which you can test the F# code,
Using Pit executable “pfc.exe”
The pfc compiler executable is used to compile F# project/DLL from command line. Supported commmand for Pit compiler is as stated below:
· pfc.exe “projectFilePath” /o:“jsOutputFilePath” /ft:true
· pfc.exe “dllFilePath” /o:“jsOutputFilePath” /ft:false
pfc executable accepts fsproj file or dll file as input and generated javascript to the specified path. To format Javascript use”/ft” (format) with true or false.
Compiling F# code to JS
Pit.Compiler.Core.dll contains all the AST and JS generation code, Add a reference to this dll and start testing out the code.
let arrayAst = ModuleCompiler.getAstFromTypes ty
let sb = new System.Text.StringBuilder()
for n in arrayAst do
let js = JavaScriptWriter.getJS n
sb.Append(js) |> ignore
Pass the array of types for which you need the AST and then get the JS using the JavaScript writer.
Using quotation expressions
let e =
<@
let x = 10
let y = 20
x + y
@>
let ast = AstParser.getAst e
let js = JavaScriptWriter.getJS [|ast|]
printfn "%A" js
Quotation expressions can be used to quickly check the AST and Javascript.
Note: The zipped files also has a python script that beautifies the JS, if you run “output.bat” you will get the formatted JS, would be easier to read through the generated JS. Make sure to have Python installed.
F# Core Mapping
All F# language constructs is mapped directly based on the namespace that is present in the F# core library. For example, F# Seq is under “Microsoft.FSharp.Collections.SeqModule”, is mapped to “Pit.FSharp.Collections.SeqModule”
Note: We replace anything with “Microsoft” / “System” to “Pit” and have an equivalent implementation under that namespace.
Attributes for JS generation
In general, there are 5 attributes we use for code generation; this mostly is used in JavaScript mapping code as it requires some special case handling,
Attribute Name | Usage |
JsAttribute | Type Alias for ReflectedDefinitionAttribute. Used to declare a function / member/ constructor for JS code generation. |
AliasAttribute | Alias name for a type. Usually used to name a different variable than the declared variable. This is used when JS is generated for the type. |
CompileToAttribute | Used in member functions which are wrappers for Javascript methods. Ex: *<CompileTo(“getElementById”)>+ member x.GetElementById(…) |
JsObjectAttribute | Used to generate Javascript-Objects for F# records. Ex: [<JsObject>] type t = { id : int name : string } |
JsIgnoreAttribute | Used to ignore certain aspects of code generation when mapping to JS. |
Note: We may have some APIs not cleaned up yet in our parser code, so just use these 2 APIs as mentioned.
Hope this post helps you dig in to some more aspects of the Pit compiler.
-Fahad

