Skip to content

Commit 3ed20ba

Browse files
committed
add evaluation of binding
now work eval of: let add x y = x + y add 1 5 add
1 parent 87a9621 commit 3ed20ba

File tree

14 files changed

+252
-37
lines changed

14 files changed

+252
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ obj
44
bin
55

66
*.suo
7+
*.sln.DotSettings.user
78

89
fs-src/ltfsclient/log.txt
910

fs-src/ltfsclient.Tests/Library1.fs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module ltfsclient.Tests
2+
3+
open System
4+
open System.IO
5+
open System.Net
6+
open System.Net.Sockets
7+
open System.Threading
8+
open FSharp.Data.Json
9+
open FSharp.Data
10+
open Microsoft.FSharp.Compiler.Interactive.Shell
11+
open NUnit.Framework
12+
open FsUnit
13+
14+
let fsiPath = @"C:\Program Files (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0\Fsi.exe"
15+
16+
let init (fsiPath: string) =
17+
let sbOut = new Text.StringBuilder()
18+
let sbErr = new Text.StringBuilder()
19+
let inStream = new StringReader("")
20+
let outStream = new StringWriter(sbOut)
21+
let errStream = new StringWriter(sbErr)
22+
23+
let argv = [| fsiPath |]
24+
let allArgs = Array.append argv [|"--noninteractive"|]
25+
26+
let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
27+
let fsiSession = FsiEvaluationSession(fsiConfig, allArgs, inStream, outStream, errStream)
28+
fsiSession
29+
30+
let eval (fsiSession:FsiEvaluationSession) text =
31+
match fsiSession.EvalExpression(text) with
32+
| Some value -> Some (sprintf "%A" value.ReflectionValue)
33+
| None -> Some (sprintf "Got no result!")
34+
35+
let evalInteraction (fsiSession:FsiEvaluationSession) text =
36+
fsiSession.EvalInteraction(text)
37+
38+
let tryEval (fsiSession:FsiEvaluationSession) text =
39+
try
40+
eval fsiSession text
41+
with e ->
42+
evalInteraction fsiSession text
43+
Some ""
44+
45+
[<TestFixture>]
46+
type ``evaluation`` () =
47+
48+
[<Test>]
49+
member x.``eval values`` () =
50+
let fsi = init fsiPath
51+
eval fsi "1+3" |> should equal "4"
52+
53+
[<Test>]
54+
member x.``eval binding`` () =
55+
let fsi = init fsiPath
56+
fsi.EvalInteraction("let add x y = x + y")
57+
eval fsi "add" |> should equal "val add : x:int -> y:int -> int"
58+
59+
[<Test>]
60+
member x.``eval binding ok`` () =
61+
let fsi = init fsiPath
62+
tryEval fsi "let add x y = x + y" |> should equal ""
63+
64+
[<Test>]
65+
member x.``eval binding info`` () =
66+
let fsi = init fsiPath
67+
fsi.EvalInteraction("let add x y = x + y")
68+
printfn "%A" (eval fsi "add")
69+
printfn "%A" (eval fsi "add 5 6")

fs-src/ltfsclient.Tests/app.config

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<dependentAssembly>
6+
<assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
7+
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
8+
</dependentAssembly>
9+
<dependentAssembly>
10+
<assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
11+
<bindingRedirect oldVersion="0.0.0.0-2.6.3.13283" newVersion="2.6.3.13283" />
12+
</dependentAssembly>
13+
</assemblyBinding>
14+
</runtime>
15+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>752d9571-36d2-43a7-8100-6bff2631bfe8</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<RootNamespace>ltfsclient.Tests</RootNamespace>
11+
<AssemblyName>ltfsclient.Tests</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<TargetFSharpCoreVersion>4.3.1.0</TargetFSharpCoreVersion>
14+
<Name>ltfsclient.Tests</Name>
15+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
16+
<RestorePackages>true</RestorePackages>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<Tailcalls>false</Tailcalls>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<WarningLevel>3</WarningLevel>
26+
<DocumentationFile>bin\Debug\ltfsclient.Tests.XML</DocumentationFile>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<Tailcalls>true</Tailcalls>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<WarningLevel>3</WarningLevel>
35+
<DocumentationFile>bin\Release\ltfsclient.Tests.XML</DocumentationFile>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="FSharp.Compiler.Service">
39+
<HintPath>..\packages\FSharp.Compiler.Service.0.0.7-alpha\lib\net40\FSharp.Compiler.Service.dll</HintPath>
40+
<Private>True</Private>
41+
</Reference>
42+
<Reference Include="FSharp.Data">
43+
<HintPath>..\packages\FSharp.Data.1.1.10\lib\net40\FSharp.Data.dll</HintPath>
44+
<Private>True</Private>
45+
</Reference>
46+
<Reference Include="FSharp.Data.DesignTime">
47+
<HintPath>..\packages\FSharp.Data.1.1.10\lib\net40\FSharp.Data.DesignTime.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="FsUnit.NUnit">
51+
<HintPath>..\packages\FsUnit.1.2.1.0\Lib\Net40\FsUnit.NUnit.dll</HintPath>
52+
<Private>True</Private>
53+
</Reference>
54+
<Reference Include="mscorlib" />
55+
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
56+
<Private>True</Private>
57+
</Reference>
58+
<Reference Include="nunit.framework">
59+
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
60+
<Private>True</Private>
61+
</Reference>
62+
<Reference Include="System" />
63+
<Reference Include="System.Core" />
64+
<Reference Include="System.Numerics" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<Compile Include="Library1.fs" />
68+
<None Include="packages.config" />
69+
<None Include="app.config" />
70+
</ItemGroup>
71+
<PropertyGroup>
72+
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
73+
</PropertyGroup>
74+
<Choose>
75+
<When Condition="'$(VisualStudioVersion)' == '11.0'">
76+
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
77+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
78+
</PropertyGroup>
79+
</When>
80+
<Otherwise>
81+
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
82+
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
83+
</PropertyGroup>
84+
</Otherwise>
85+
</Choose>
86+
<Import Project="$(FSharpTargetsPath)" />
87+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
88+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
89+
Other similar extension points exist, see Microsoft.Common.targets.
90+
<Target Name="BeforeBuild">
91+
</Target>
92+
<Target Name="AfterBuild">
93+
</Target>
94+
-->
95+
</Project>
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="FSharp.Compiler.Service" version="0.0.11-alpha" targetFramework="net40" />
4+
<package id="FSharp.Data" version="1.1.10" targetFramework="net40" />
5+
<package id="FsUnit" version="1.2.1.0" targetFramework="net40" />
6+
<package id="NUnit" version="2.6.3" targetFramework="net40" />
7+
</packages>

fs-src/ltfsclient.sln

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{8AF6B2
1212
EndProject
1313
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ltfsclient", "ltfsclient\ltfsclient.fsproj", "{70870340-FCFE-472F-8063-770082AF204C}"
1414
EndProject
15+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ltfsclient.Tests", "ltfsclient.Tests\ltfsclient.Tests.fsproj", "{752D9571-36D2-43A7-8100-6BFF2631BFE8}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -22,6 +24,10 @@ Global
2224
{70870340-FCFE-472F-8063-770082AF204C}.Debug|Any CPU.Build.0 = Debug|Any CPU
2325
{70870340-FCFE-472F-8063-770082AF204C}.Release|Any CPU.ActiveCfg = Release|Any CPU
2426
{70870340-FCFE-472F-8063-770082AF204C}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{752D9571-36D2-43A7-8100-6BFF2631BFE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{752D9571-36D2-43A7-8100-6BFF2631BFE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{752D9571-36D2-43A7-8100-6BFF2631BFE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{752D9571-36D2-43A7-8100-6BFF2631BFE8}.Release|Any CPU.Build.0 = Release|Any CPU
2531
EndGlobalSection
2632
GlobalSection(SolutionProperties) = preSolution
2733
HideSolutionNode = FALSE

fs-src/ltfsclient/ltfsclient.fsx

+47-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#I "../packages/FSharp.Compiler.Service.0.0.7-alpha/lib/net40"
2+
#I "../packages/FSharp.Compiler.Service.0.0.11-alpha/lib/net40"
33
#I "../packages/FSharp.Data.1.1.10/lib/net40"
44
#r "FSharp.Compiler.Service.dll"
55
#r "FSharp.Data.dll"
@@ -65,11 +65,32 @@ let fsi = lazy (
6565
| None -> failwith "fsi path not set"
6666
)
6767

68-
let eval text =
69-
let fsiSession = fsi.Value
68+
let evalExpr (fsiSession:FsiEvaluationSession) text =
7069
match fsiSession.EvalExpression(text) with
71-
| Some value -> Some (sprintf "%A" value.ReflectionValue)
72-
| None -> Some (sprintf "Got no result!")
70+
| Some value -> (sprintf "%A" value.ReflectionValue)
71+
| None -> (sprintf "Got no result!")
72+
73+
let evalInteraction (fsiSession:FsiEvaluationSession) text =
74+
fsiSession.EvalInteraction(text)
75+
76+
type Evaluation =
77+
| Success
78+
| Exception of string
79+
| Result of string
80+
81+
let eval text =
82+
let fsiSession = fsi.Value
83+
try
84+
let v = evalExpr fsiSession text
85+
Evaluation.Result(v)
86+
with e ->
87+
try
88+
evalInteraction fsiSession text
89+
Evaluation.Success
90+
with e ->
91+
let exInfo = [e.Message; e.StackTrace.Replace("\r\n", "\n") ] |> List.fold (fun s x -> x + "\n" + s) ""
92+
Evaluation.Exception(exInfo)
93+
7394

7495
// LightTable
7596

@@ -160,6 +181,13 @@ let rec handle stream request =
160181
log "handle request:"
161182
logf "%A" request
162183

184+
let responseAndLoop response =
185+
log "Sending eval response"
186+
logf "%A" response
187+
send_response stream response
188+
log "Sent Eval response"
189+
read_request stream handle
190+
163191
match request with
164192
| Malformed s ->
165193
log "Received malformed request, aborting"
@@ -171,42 +199,26 @@ let rec handle stream request =
171199
| EvalFSharpSelectedCmd (clientId, args) ->
172200
log "Eval cmd selected"
173201
let cmd, res =
174-
try
175-
match eval (args.Code) with
176-
| None ->
177-
("editor.eval.fsharp.success", JsonValue.Object( Map.ofList [("meta", args.Meta.JsonValue)] ))
178-
| Some result ->
179-
("editor.eval.fsharp.result", JsonValue.Object( Map.ofList [("result", JsonValue.String(result)); ("meta", args.Meta.JsonValue)] ))
180-
with e ->
181-
let exInfo = [e.Message; e.StackTrace.Replace("\r\n", "\n") ] |> List.fold (fun s x -> x + "\n" + s) ""
202+
match eval (args.Code) with
203+
| Evaluation.Success ->
204+
("editor.eval.fsharp.success", JsonValue.Object( Map.ofList [("meta", args.Meta.JsonValue)] ))
205+
| Evaluation.Result(result) ->
206+
("editor.eval.fsharp.result", JsonValue.Object( Map.ofList [("result", JsonValue.String(result)); ("meta", args.Meta.JsonValue)] ))
207+
| Evaluation.Exception(exInfo) ->
182208
("editor.eval.fsharp.exception", JsonValue.Object( Map.ofList [("ex", JsonValue.String(exInfo)); ("meta", args.Meta.JsonValue)] ))
183-
184-
let response = {ClientId = clientId; Cmd = cmd; Args = res}
185-
log "Sending eval response"
186-
logf "%A" response
187-
send_response stream response
188-
log "Sent Eval response"
189-
read_request stream handle
209+
responseAndLoop {ClientId = clientId; Cmd = cmd; Args = res}
190210

191211
| EvalFSharpNoSelectionCmd (clientId, args) ->
192212
log "Eval cmd not selected"
193213
let cmd, res =
194-
try
195-
match eval (args.Code) with
196-
| None ->
197-
("editor.eval.fsharp.success", JsonValue.Object( Map.ofList [("meta", JsonValue.Null)] ))
198-
| Some result ->
199-
("editor.eval.fsharp.result", JsonValue.Object( Map.ofList [("result", JsonValue.String(result)); ("meta", JsonValue.Null)] ))
200-
with e ->
201-
let exInfo = [e.Message; e.StackTrace.Replace("\r\n", "\n") ] |> List.fold (fun s x -> x + "\n" + s) ""
214+
match eval (args.Code) with
215+
| Evaluation.Success ->
216+
("editor.eval.fsharp.success", JsonValue.Object( Map.ofList [("meta", JsonValue.Null)] ))
217+
| Evaluation.Result(result) ->
218+
("editor.eval.fsharp.result", JsonValue.Object( Map.ofList [("result", JsonValue.String(result)); ("meta", JsonValue.Null)] ))
219+
| Evaluation.Exception(exInfo) ->
202220
("editor.eval.fsharp.exception", JsonValue.Object( Map.ofList [("ex", JsonValue.String(exInfo)); ("meta", JsonValue.Null)] ))
203-
204-
let response = {ClientId = clientId; Cmd = cmd; Args = res}
205-
log "Sending eval response"
206-
logf "%A" response
207-
send_response stream response
208-
log "Sent Eval response"
209-
read_request stream handle
221+
responseAndLoop {ClientId = clientId; Cmd = cmd; Args = res}
210222

211223
| Valid (clientId, cmd, args) ->
212224
logf "Cmd '%s' unsupported (args '%s') " cmd (args.ToString()) |> ignore

fs-src/ltfsclient/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="FSharp.Compiler.Service" version="0.0.7-alpha" targetFramework="net40" />
3+
<package id="FSharp.Compiler.Service" version="0.0.11-alpha" targetFramework="net40" />
44
<package id="FSharp.Data" version="1.1.10" targetFramework="net40" />
55
</packages>

fs-src/packages/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
*.nupkg
3+
*.nuspec
34
*.xml
45

56
portable-net40+sl5+wp8+win8
67
sl4-windowsphone71
78
sl5
9+
10+
FsUnit*
11+
NUnit*

fs-src/packages/repositories.config

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<repositories>
33
<repository path="..\ConsoleApplication2-cli\packages.config" />
4+
<repository path="..\ltfsclient.Tests\packages.config" />
5+
<repository path="..\ltfsclient\packages.config" />
46
</repositories>

fs-src/test/test.fs

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ System.DateTime.Now
55
Array.create 12 (byte 0)
66

77
[0..2..10]
8+
9+
let add x y = x + y + 10
10+
11+
add 1 3

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(defproject lt-fsharp "0.0.1"
1+
(defproject lt-fsharp "0.0.2"
22
:dependencies [[org.clojure/clojure "1.5.1"]])

0 commit comments

Comments
 (0)