This repository was archived by the owner on Jul 19, 2019. It is now read-only.
forked from fsharp/fsharp-compiler-docs
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompiler.fsx
More file actions
94 lines (65 loc) · 2.42 KB
/
compiler.fsx
File metadata and controls
94 lines (65 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(*** hide ***)
#I "../../bin/v4.5/"
(**
Hosted Compiler
===============
This tutorial demonstrates how to host the F# compiler.
> **NOTE:** The FSharp.Compiler.Service API is subject to change when later versions of the nuget package are published
*)
(**
> **NOTE:** There are several options for hosting the F# compiler. The easiest one is to use the
`fsc.exe` process and pass arguments.
*)
(**
> **NOTE:** By default [compilations using FSharp.Compiler.Service reference FSharp.Core 4.3.0.0](https://github.com/fsharp/FSharp.Compiler.Service/issues/156) (matching F# 3.0). You can override
this choice by passing a reference to FSharp.Core for 4.3.1.0 or later explicitly in your command-line arguments.
*)
(**
---------------------------
First, we need to reference the libraries that contain F# interactive service:
*)
#r "FSharp.Compiler.Service.dll"
open System.IO
open Microsoft.FSharp.Compiler.SourceCodeServices
// Create an interactive checker instance
let checker = FSharpChecker.Create()
(**
Now write content to a temporary file:
*)
let fn = Path.GetTempFileName()
let fn2 = Path.ChangeExtension(fn, ".fsx")
let fn3 = Path.ChangeExtension(fn, ".dll")
File.WriteAllText(fn2, """
module M
type C() =
member x.P = 1
let x = 3 + 4
""")
(**
Now invoke the compiler:
*)
let errors1, exitCode1 = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
(**
If errors occur you can see this in the 'exitCode' and the returned array of errors:
*)
File.WriteAllText(fn2, """
module M
let x = 1.0 + "" // a type error
""")
let errors1b, exitCode1b = checker.Compile([| "fsc.exe"; "-o"; fn3; "-a"; fn2 |])
(**
Compiling to a dynamic assembly
===============================
You can also compile to a dynamic assembly, which uses the F# Interactive code generator.
This can be useful if you are, for example, in a situation where writing to the file system
is not really an option.
You still have to pass the "-o" option to name the output file, but the output file is not actually written to disk.
The 'None' option indicates that the initiatlization code for the assembly is not executed.
*)
let errors2, exitCode2, dynAssembly2 =
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], execute=None)
(*
Passing 'Some' for the 'execute' parameter executes the initiatlization code for the assembly.
*)
let errors3, exitCode3, dynAssembly3 =
checker.CompileToDynamicAssembly([| "-o"; fn3; "-a"; fn2 |], Some(stdout,stderr))