Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ node_modules/
.fake

# Wasm example files
examples/*.wasm
examples/*.wasm


# Clojure ignore due to VS Code extensions
.clj-kondo/
.lsp/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "test/tinyfs_testprojects"]
path = test/tinyfs_testprojects
url = git@github.com:morgankenyon/tinyfs_testprojects.git
26 changes: 17 additions & 9 deletions cli/Arguments.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,30 @@ let runPrint print =
printfn "%s" print
Ok()

let parseFsProjectFile (fileInfo: FileInfo) =

[]

let compileFile (filename: string) =
printfn "Compiling: %s" filename

let fileText = System.IO.File.ReadAllText filename

let name = System.IO.Path.GetFileNameWithoutExtension filename
let fileInfo = new System.IO.FileInfo(filename)
let directory = fileInfo.DirectoryName
let wasmFilename = $"{directory}{Path.DirectorySeparatorChar}{name}.wasm"
if fileInfo.Extension = "fs" then
let fileText = System.IO.File.ReadAllText filename

let name = System.IO.Path.GetFileNameWithoutExtension filename
let directory = fileInfo.DirectoryName
let wasmFilename = $"{directory}{Path.DirectorySeparatorChar}{name}.wasm"

let wasmBytes = EndToEnd.compile fileText |> List.toArray
let wasmBytes = EndToEnd.compile fileText |> List.toArray

System.IO.File.WriteAllBytes(wasmFilename, wasmBytes)
printfn "Compiled to: %s" wasmFilename
System.IO.File.WriteAllBytes(wasmFilename, wasmBytes)
printfn "Compiled to: %s" wasmFilename

wasmFilename
wasmFilename
else
printfn "%s" "FS Proj baby"
"test"

let runFile (filename: string) =
printfn "Running: %s" filename
Expand Down
53 changes: 52 additions & 1 deletion src/FSharpToAst.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module TinyFS.Core.FSharpToAst

open FSharp.Compiler.CodeAnalysis
open System.IO
open FSharp.Compiler.Text
open Ionide.ProjInfo

open System.IO

let parseAndCheckSingleFile (checker: FSharpChecker) (input: string) =
let file = Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".fsx")
Expand All @@ -15,6 +17,39 @@ let parseAndCheckSingleFile (checker: FSharpChecker) (input: string) =
checker.ParseAndCheckProject(projOptions)
|> Async.RunSynchronously

let sysLib nm =
let sysDir =
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
let (++) a b = Path.Combine(a, b)
sysDir ++ nm + ".dll"

let fsCorePath () =
"C:\Program Files\dotnet\sdk\9.0.200\FSharp\FSharp.Core.dll"

let parseAndCheckProject (checker: FSharpChecker) (projectPath: string) (projectFilePath: string) =
// let file = new System.IO.FileInfo(projectPath);
// file.Directory.Create();
let projectDirectory: DirectoryInfo = Directory.CreateDirectory(projectPath);
let toolsPath = Init.init projectDirectory None
let defaultLoader: IWorkspaceLoader = WorkspaceLoader.Create(toolsPath, [])
let projectOptions = defaultLoader.LoadProjects([ projectPath ]) |> Seq.toArray
let fcsProjectOptions =
FCS.mapManyOptions projectOptions
|> Seq.toList
let args: string array =
[|
yield $"{projectPath}/Library.fs"
|]
let projectOptions =
checker.GetProjectOptionsFromCommandLineArgs(projectFilePath, args) // projectFilePath args // GetProjectOptionsFromProjectFile(projectFilePath)

// Parse and check the entire project
let projectResults =
checker.ParseAndCheckProject(projectOptions)
|> Async.RunSynchronously

projectResults

let getDeclarations checker (input: string) =
let checkProjectResults = parseAndCheckSingleFile checker input
let checkedFile = checkProjectResults.AssemblyContents.ImplementationFiles.[0]
Expand All @@ -31,3 +66,19 @@ let getDeclarations checker (input: string) =
failwith msg
else
checkedFile.Declarations

let getDeclarationsFromProject checker (projectPath: string) (projectFilePath: string) =
let checkProjectResults = parseAndCheckProject checker projectPath projectFilePath

// Now you can work with the full project results
// Example: get all the declarations in the project
let declarations =
checkProjectResults.AssemblyContents.ImplementationFiles
|> List.map (fun f -> f.Declarations)
|> List.collect id

declarations
//for file in checkProjectResults.AssemblyContents.ImplementationFiles do
// for decl in file.Declarations do
// // Process declarations
// printfn " Declaration: %A" decl.
4 changes: 4 additions & 0 deletions src/TinyFS.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

<ItemGroup>
<PackageReference Include="FSharp.Compiler.Service" Version="43.9.201" />
<PackageReference Include="Ionide.ProjInfo" Version="0.70.2" />
<PackageReference Include="Ionide.ProjInfo.FCS" Version="0.70.2" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.2.0" ExcludeAssets="runtime" PrivateAssets="all" />
<PackageReference Include="NuGet.Frameworks" Version="6.2.1" ExcludeAssets="runtime" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 22 additions & 12 deletions test/FSharpToAstTests.fs
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
module TinyFS.Test.FSharpToAstTests

open Faqt
open FSharp.Compiler.CodeAnalysis
open System.Reflection
open System
open System.IO
open TinyFS.Core.FSharpToAst
open TinyFS.Test.Helpers
open Xunit
open FSharp.Compiler.CodeAnalysis

//let checker: FSharpChecker = FSharpChecker.Create(keepAssemblyContents = true)
//[<Fact>]
//let ``Can convert simple let statement to ast`` () =
// let input =
// $"""
//module Test
let checker: FSharpChecker = FSharpChecker.Create(keepAssemblyContents = true)
let GetDirectoryPath(assembly: Assembly) =
let filePath = (new Uri(assembly.Location)).LocalPath;
Path.GetDirectoryName(filePath);

//let x = 1
//"""
let joinWithOpSeparator (parts: string list) =
List.reduce (fun a b -> $"{a}{Path.DirectorySeparatorChar}{b}") parts
[<Fact>]
let ``Can convert simple let statement to ast`` () =

let localDir = GetDirectoryPath(Assembly.GetExecutingAssembly())
let testProjectPath =
[ localDir; "tinyfs_testprojects"; "HelloWorld" ]
|> joinWithOpSeparator
let testProjectDir =
[ localDir; "tinyfs_testprojects"; "HelloWorld"; "HelloWorld.fsproj" ]
|> joinWithOpSeparator

// let declarations = getDeclarations checker input
// declarations.Should().HaveLength(1)
let declarations = getDeclarationsFromProject checker testProjectPath testProjectDir
declarations.Should().HaveLength(1)

//[<Fact>]
//let ``Can convert several let statements to ast`` () =
Expand Down
11 changes: 11 additions & 0 deletions test/TinyFS.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="tinyfs_testprojects\HelloWorld\Library.fs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<None Include="tinyfs_testprojects\.gitignore" />
<None Include="tinyfs_testprojects\HelloWorld\HelloWorld.fsproj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="tinyfs_testprojects\LICENSE" />
<None Include="tinyfs_testprojects\README.md" />
<Compile Include="Helpers.fs" />
<Compile Include="Leb128Tests.fs" />
<Compile Include="FSharpToAstTests.fs" />
Expand All @@ -16,6 +25,8 @@
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup />

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Faqt" Version="4.5.0" />
Expand Down
1 change: 1 addition & 0 deletions test/tinyfs_testprojects
Submodule tinyfs_testprojects added at e79167
Loading