Skip to content

Commit

Permalink
Add Stage1 Compiler
Browse files Browse the repository at this point in the history
This cut-down compiler can be used to compile lthe standard library.
  • Loading branch information
iwillspeak committed Nov 8, 2021
1 parent 2e3c646 commit 296415a
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Feersum.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{B4FD8357-953D-49DE-A05E-DDBFD6E9489C}") = "Feersum.Core", "src\Feersum
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Feersum.CompilerServices", "src\Feersum.CompilerServices\Feersum.CompilerServices.fsproj", "{BFEAD4F4-D3B5-4118-84C1-D9618BF16AE1}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Feersum.Stage1", "src\Feersum.Stage1\Feersum.Stage1.fsproj", "{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -162,6 +164,18 @@ Global
{BFEAD4F4-D3B5-4118-84C1-D9618BF16AE1}.Release|x64.Build.0 = Release|Any CPU
{BFEAD4F4-D3B5-4118-84C1-D9618BF16AE1}.Release|x86.ActiveCfg = Release|Any CPU
{BFEAD4F4-D3B5-4118-84C1-D9618BF16AE1}.Release|x86.Build.0 = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|x64.ActiveCfg = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|x64.Build.0 = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|x86.ActiveCfg = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Debug|x86.Build.0 = Debug|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|Any CPU.Build.0 = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|x64.ActiveCfg = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|x64.Build.0 = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|x86.ActiveCfg = Release|Any CPU
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2285F372-B4BF-4AB6-9E3F-B9CD045D09AD} = {312A2802-9517-47DE-9F5D-74B36DA2EA56}
Expand All @@ -174,5 +188,6 @@ Global
{60F97CC7-7DE6-49BC-AD8B-11A3C3A8531C} = {312A2802-9517-47DE-9F5D-74B36DA2EA56}
{9F359509-5A2A-4B16-A68B-CFE1984AE0EE} = {312A2802-9517-47DE-9F5D-74B36DA2EA56}
{BFEAD4F4-D3B5-4118-84C1-D9618BF16AE1} = {312A2802-9517-47DE-9F5D-74B36DA2EA56}
{789F28C0-2C4A-4DCE-8A31-E69DB1CF56DE} = {312A2802-9517-47DE-9F5D-74B36DA2EA56}
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion eng/UseLocalCompiler.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
core library so `dotnet` copies it to the output directory. -->
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Serehfa\Serehfa.csproj" />
<!-- FIXME: Cross targetting of the core library.
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Feersum.Core\Feersum.Core.scmproj" /> -->
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Feersum\Feersum.fsproj">
<Private>False</Private>
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
<!-- <SetConfiguration>Configuration=Release</SetConfiguration> -->
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<CopyToOutputDirectory>false</CopyToOutputDirectory>
<CopyToPublishDirectory>false</CopyToPublishDirectory>
Expand Down
29 changes: 29 additions & 0 deletions eng/UseStage1Compiler.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project>

<PropertyGroup>
<LanguageTargets Condition="'$(MSBuildProjectExtension)' == '.scmproj'">$(MSBuildThisFileDirectory)../src/Feersum.Sdk/targets/Feersum.Sdk.targets</LanguageTargets>
</PropertyGroup>

<!-- Config for our language targets. We don't want to pull in a compiler from
NuGet but instead want to dogfood the current build outputs. -->
<PropertyGroup>
<NoImplicitFeersumCoreReference>true</NoImplicitFeersumCoreReference>
<CompilerConfiguration>$(Configuration)</CompilerConfiguration>
<CompilerConfiguration Condition=" '$(CompilerConfiguration)' == '' ">Debug</CompilerConfiguration>
<FeersumCompilerPath>$(MSBuildThisFileDirectory)..\src\Feersum.Stage1\bin\$(CompilerConfiguration)\net5.0\Feersum.Stage1.dll</FeersumCompilerPath>
</PropertyGroup>

<!-- Reference the compiler so we have it built and can run it. Refernece our
core library so `dotnet` copies it to the output directory. -->
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Serehfa\Serehfa.csproj" />
<ProjectReference Include="$(MSBuildThisFileDirectory)..\src\Feersum.Stage1\Feersum.Stage1.fsproj">
<Private>False</Private>
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<CopyToOutputDirectory>false</CopyToOutputDirectory>
<CopyToPublishDirectory>false</CopyToPublishDirectory>
</ProjectReference>
</ItemGroup>

</Project>
8 changes: 6 additions & 2 deletions src/Feersum.CompilerServices/Eval.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ open Serehfa
open Feersum.CompilerServices.Compile
open Feersum.CompilerServices.Options

/// The default set of otpions for scripting
let defaultScriptOptions = CompilationOptions.Create Debug Script

/// Raw External Representation
///
/// Returns the external representation for a CIL type.
Expand All @@ -19,9 +22,8 @@ let cilExternalRepr (object: Object) = Write.GetExternalRepresentation(object)
///
/// This first compiles the tree to an in-memory assembly and then calls the
/// main method on that.
let eval ast =
let evalWith options ast =
let memStream = new MemoryStream()
let options = CompilationOptions.Create Debug Script

let result =
Compilation.compile options memStream "evalCtx" None ast
Expand All @@ -44,3 +46,5 @@ let eval ast =
.Throw()

Error([])

let eval = evalWith defaultScriptOptions
4 changes: 4 additions & 0 deletions src/Feersum.CompilerServices/Feersum.CompilerServices.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Title>Feersum Scheme Compiler Services Library</Title>
<Description>
This package contains the compiler tooling used by the main Feersum compiler.
</Description>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Feersum.Core/Feersum.Core.scmproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateGitVersionInformation>false</GenerateGitVersionInformation>
</PropertyGroup>

<Import Project="../../eng/UseLocalCompiler.props" />
<Import Project="../../eng/UseStage1Compiler.props" />

<ItemGroup>
<Compile Include="cxr.scm" />
Expand Down
22 changes: 22 additions & 0 deletions src/Feersum.Stage1/Feersum.Stage1.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Argu" Version="6.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Feersum.CompilerServices\Feersum.CompilerServices.fsproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions src/Feersum.Stage1/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

open Argu.ArguAttributes
open Argu

open Feersum.CompilerServices.Diagnostics
open Feersum.CompilerServices.Options
open Feersum.CompilerServices.Compile

/// Command line arguments type. Cut-down stage1 compiler options
type CliArguments =
| Configuration of BuildConfiguration
| OutputType of OutputType
| CoreLibPath of string
| GenerateDeps of bool
| [<AltCommandLine("-r")>] Reference of string
| [<AltCommandLine("-o")>] Output of string
| [<MainCommand>] Sources of source_file: string list

interface IArgParserTemplate with
member s.Usage =
match s with
| Configuration _ -> "The build configuration (Debug / Release)."
| OutputType _ -> "The output type (Lib / Exe / Script)."
| CoreLibPath _ -> "Location of mscorelib.dll, or System.Runtime.dll."
| GenerateDeps _ -> "Generate .deps.json and .runtimeconfig.json stubs."
| Reference _ -> "Compiled Scheme assembly to reference."
| Output _ -> "The output path to write compilation results to."
| Sources _ -> "Scheme source files for compilation."


[<EntryPoint>]
let main argv =
let parser =
ArgumentParser.Create<CliArguments>(programName = "feersum-stage1")

let args = parser.Parse(argv)

let buildConfig =
args.TryGetResult Configuration
|> Option.defaultValue Release

let outputType =
args.TryGetResult OutputType
|> Option.defaultValue Exe

let options =
{ CompilationOptions.Create buildConfig outputType with
References = args.GetResults Reference
GenerateDepsFiles =
(args.TryGetResult GenerateDeps)
|> Option.defaultValue true
MsCorePaths = args.GetResults CoreLibPath }

let diags = Compilation.compileFiles options (args.GetResult(Output)) (args.GetResult(Sources))
if hasErrors diags then
dumpDiagnostics diags
diags |> List.length
else
0
1 change: 1 addition & 0 deletions src/Feersum/Feersum.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Feersum.CompilerServices\Feersum.CompilerServices.fsproj" />
<ProjectReference Include="..\Feersum.Core\Feersum.Core.scmproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="Version.fs" />
Expand Down
6 changes: 5 additions & 1 deletion src/Feersum/Repl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Feersum.Version
open Feersum.CompilerServices.Syntax
open Feersum.CompilerServices.Eval
open Feersum.CompilerServices.Diagnostics
open Feersum.Core

/// Read a single line of user input and parse it into a
/// syntax tree. If the input can't be parsed then read
Expand Down Expand Up @@ -42,4 +43,7 @@ let rec private repl evaluator =
let runRepl () =
ReadLine.HistoryEnabled <- true
printVersion ()
eval >> Result.map print |> repl
let options =
{ defaultScriptOptions
with References = [ typeof<LispProgram>.Assembly.Location ]}
evalWith options >> Result.map print |> repl

0 comments on commit 296415a

Please sign in to comment.