mirror of
https://github.com/Smaug123/WoofWare.PawPrint
synced 2025-10-08 15:38:41 +00:00
Tidy up return flow (#12)
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -7,3 +7,6 @@ riderModule.iml
|
|||||||
.direnv/
|
.direnv/
|
||||||
*.DotSettings.user
|
*.DotSettings.user
|
||||||
result
|
result
|
||||||
|
|
||||||
|
.fake
|
||||||
|
.vscode/
|
||||||
|
@@ -13,7 +13,7 @@ module Roslyn =
|
|||||||
let compile (sources : string list) : byte[] =
|
let compile (sources : string list) : byte[] =
|
||||||
// Create a syntax tree per source snippet.
|
// Create a syntax tree per source snippet.
|
||||||
let parseOptions =
|
let parseOptions =
|
||||||
CSharpParseOptions.Default.WithLanguageVersion (LanguageVersion.Preview)
|
CSharpParseOptions.Default.WithLanguageVersion LanguageVersion.Preview
|
||||||
|
|
||||||
let syntaxTrees : SyntaxTree[] =
|
let syntaxTrees : SyntaxTree[] =
|
||||||
sources
|
sources
|
||||||
@@ -25,14 +25,13 @@ module Roslyn =
|
|||||||
|
|
||||||
// Reference every assembly found in the runtime directory – crude but
|
// Reference every assembly found in the runtime directory – crude but
|
||||||
// guarantees we can resolve System.* et al.
|
// guarantees we can resolve System.* et al.
|
||||||
let runtimeDir =
|
let runtimeDir = Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory ()
|
||||||
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory ()
|
|
||||||
|
|
||||||
let metadataReferences : MetadataReference[] =
|
let metadataReferences : MetadataReference[] =
|
||||||
Directory.GetFiles (runtimeDir, "*.dll")
|
Directory.GetFiles (runtimeDir, "*.dll")
|
||||||
|> Array.map (fun path -> MetadataReference.CreateFromFile path :> MetadataReference)
|
|> Array.map (fun path -> MetadataReference.CreateFromFile path :> MetadataReference)
|
||||||
|
|
||||||
let compilationOptions = CSharpCompilationOptions (OutputKind.ConsoleApplication)
|
let compilationOptions = CSharpCompilationOptions OutputKind.ConsoleApplication
|
||||||
|
|
||||||
let compilation =
|
let compilation =
|
||||||
CSharpCompilation.Create (
|
CSharpCompilation.Create (
|
||||||
@@ -44,7 +43,7 @@ module Roslyn =
|
|||||||
|
|
||||||
use peStream = new MemoryStream ()
|
use peStream = new MemoryStream ()
|
||||||
|
|
||||||
let emitResult = compilation.Emit (peStream)
|
let emitResult = compilation.Emit peStream
|
||||||
|
|
||||||
if emitResult.Success then
|
if emitResult.Success then
|
||||||
peStream.ToArray ()
|
peStream.ToArray ()
|
||||||
|
@@ -12,6 +12,22 @@ open WoofWare.PawPrint.Test
|
|||||||
module TestCases =
|
module TestCases =
|
||||||
let assy = typeof<RunResult>.Assembly
|
let assy = typeof<RunResult>.Assembly
|
||||||
|
|
||||||
|
let unimplemented =
|
||||||
|
[
|
||||||
|
{
|
||||||
|
FileName = "BasicException.cs"
|
||||||
|
ExpectedReturnCode = 10
|
||||||
|
}
|
||||||
|
{
|
||||||
|
FileName = "WriteLine.cs"
|
||||||
|
ExpectedReturnCode = 10
|
||||||
|
}
|
||||||
|
{
|
||||||
|
FileName = "BasicLock.cs"
|
||||||
|
ExpectedReturnCode = 10
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
let cases : TestCase list =
|
let cases : TestCase list =
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@@ -25,7 +41,7 @@ module TestCases =
|
|||||||
]
|
]
|
||||||
|
|
||||||
[<TestCaseSource(nameof cases)>]
|
[<TestCaseSource(nameof cases)>]
|
||||||
let ``Can run a no-op`` (case : TestCase) : unit =
|
let ``Can evaluate C# files`` (case : TestCase) : unit =
|
||||||
let source = Assembly.getEmbeddedResourceAsString case.FileName assy
|
let source = Assembly.getEmbeddedResourceAsString case.FileName assy
|
||||||
let image = Roslyn.compile [ source ]
|
let image = Roslyn.compile [ source ]
|
||||||
let messages, loggerFactory = LoggerFactory.makeTest ()
|
let messages, loggerFactory = LoggerFactory.makeTest ()
|
||||||
@@ -35,15 +51,54 @@ module TestCases =
|
|||||||
|
|
||||||
use peImage = new MemoryStream (image)
|
use peImage = new MemoryStream (image)
|
||||||
|
|
||||||
let terminalState, terminatingThread =
|
try
|
||||||
Program.run loggerFactory peImage dotnetRuntimes []
|
let terminalState, terminatingThread =
|
||||||
|
Program.run loggerFactory peImage dotnetRuntimes []
|
||||||
|
|
||||||
let exitCode =
|
let exitCode =
|
||||||
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
|
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
|
||||||
| [] -> failwith "expected program to return a value, but it returned void"
|
| [] -> failwith "expected program to return a value, but it returned void"
|
||||||
| head :: _ ->
|
| head :: _ ->
|
||||||
match head with
|
match head with
|
||||||
| EvalStackValue.Int32 i -> i
|
| EvalStackValue.Int32 i -> i
|
||||||
| ret -> failwith "expected program to return an int, but it returned %O{ret}"
|
| ret -> failwith $"expected program to return an int, but it returned %O{ret}"
|
||||||
|
|
||||||
exitCode |> shouldEqual case.ExpectedReturnCode
|
exitCode |> shouldEqual case.ExpectedReturnCode
|
||||||
|
|
||||||
|
with _ ->
|
||||||
|
for message in messages () do
|
||||||
|
System.Console.Error.WriteLine $"{message}"
|
||||||
|
|
||||||
|
reraise ()
|
||||||
|
|
||||||
|
[<TestCaseSource(nameof unimplemented)>]
|
||||||
|
[<Explicit "not yet implemented">]
|
||||||
|
let ``Can evaluate C# files (unimplemented)`` (case : TestCase) : unit =
|
||||||
|
let source = Assembly.getEmbeddedResourceAsString case.FileName assy
|
||||||
|
let image = Roslyn.compile [ source ]
|
||||||
|
let messages, loggerFactory = LoggerFactory.makeTest ()
|
||||||
|
|
||||||
|
let dotnetRuntimes =
|
||||||
|
DotnetRuntime.SelectForDll assy.Location |> ImmutableArray.CreateRange
|
||||||
|
|
||||||
|
use peImage = new MemoryStream (image)
|
||||||
|
|
||||||
|
try
|
||||||
|
let terminalState, terminatingThread =
|
||||||
|
Program.run loggerFactory peImage dotnetRuntimes []
|
||||||
|
|
||||||
|
let exitCode =
|
||||||
|
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
|
||||||
|
| [] -> failwith "expected program to return a value, but it returned void"
|
||||||
|
| head :: _ ->
|
||||||
|
match head with
|
||||||
|
| EvalStackValue.Int32 i -> i
|
||||||
|
| ret -> failwith $"expected program to return an int, but it returned %O{ret}"
|
||||||
|
|
||||||
|
exitCode |> shouldEqual case.ExpectedReturnCode
|
||||||
|
|
||||||
|
with _ ->
|
||||||
|
for message in messages () do
|
||||||
|
System.Console.Error.WriteLine $"{message}"
|
||||||
|
|
||||||
|
reraise ()
|
||||||
|
@@ -19,8 +19,9 @@
|
|||||||
<Compile Include="TestBasicLock.fs" />
|
<Compile Include="TestBasicLock.fs" />
|
||||||
<EmbeddedResource Include="sources\BasicLock.cs" />
|
<EmbeddedResource Include="sources\BasicLock.cs" />
|
||||||
<EmbeddedResource Include="sources\NoOp.cs" />
|
<EmbeddedResource Include="sources\NoOp.cs" />
|
||||||
<EmbeddedResource Include="sources\HelloWorld.cs" />
|
<EmbeddedResource Include="sources\BasicException.cs" />
|
||||||
<EmbeddedResource Include="sources\TriangleNumber.cs" />
|
<EmbeddedResource Include="sources\TriangleNumber.cs" />
|
||||||
|
<EmbeddedResource Include="sources\WriteLine.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@@ -6,7 +6,6 @@ namespace HelloWorldApp
|
|||||||
{
|
{
|
||||||
static int ReallyMain(string[] args)
|
static int ReallyMain(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello, world!");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
13
WoofWare.PawPrint.Test/sources/WriteLine.cs
Normal file
13
WoofWare.PawPrint.Test/sources/WriteLine.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace HelloWorldApp
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static int Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, world!");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -781,25 +781,6 @@ module IlMachineState =
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: which stack do we actually want to push to?
|
|
||||||
let pushToStackCoerced (o : EvalStackValue) (targetType : TypeDefn) (thread : ThreadId) (state : IlMachineState) =
|
|
||||||
{ state with
|
|
||||||
ThreadState =
|
|
||||||
state.ThreadState
|
|
||||||
|> Map.change
|
|
||||||
thread
|
|
||||||
(fun threadState ->
|
|
||||||
match threadState with
|
|
||||||
| None -> failwith "Logic error: tried to push to stack of a nonexistent thread"
|
|
||||||
| Some threadState ->
|
|
||||||
{ threadState with
|
|
||||||
ThreadState.MethodState =
|
|
||||||
threadState.MethodState |> MethodState.pushToEvalStack (failwith "TODO")
|
|
||||||
}
|
|
||||||
|> Some
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let popFromStackToLocalVariable
|
let popFromStackToLocalVariable
|
||||||
(thread : ThreadId)
|
(thread : ThreadId)
|
||||||
(localVariableIndex : int)
|
(localVariableIndex : int)
|
||||||
@@ -1018,26 +999,27 @@ module AbstractMachine =
|
|||||||
|
|
||||||
let state =
|
let state =
|
||||||
match returnState.WasConstructingObj with
|
match returnState.WasConstructingObj with
|
||||||
| None -> state
|
|
||||||
| Some constructing ->
|
| Some constructing ->
|
||||||
|
// Assumption: a constructor can't also return a value.
|
||||||
state
|
state
|
||||||
|> IlMachineState.pushToEvalStack (CliType.OfManagedObject constructing) currentThread
|
|> IlMachineState.pushToEvalStack (CliType.OfManagedObject constructing) currentThread
|
||||||
|
| None ->
|
||||||
|
match threadStateAtEndOfMethod.MethodState.EvaluationStack.Values with
|
||||||
|
| [] ->
|
||||||
|
// no return value
|
||||||
|
state
|
||||||
|
| [ retVal ] ->
|
||||||
|
let retType =
|
||||||
|
threadStateAtEndOfMethod.MethodState.ExecutingMethod.Signature.ReturnType
|
||||||
|
|
||||||
match threadStateAtEndOfMethod.MethodState.EvaluationStack.Values with
|
let toPush = EvalStackValue.toCliTypeCoerced (CliType.zeroOf retType) retVal
|
||||||
| [] ->
|
|
||||||
// no return value
|
|
||||||
(state, WhatWeDid.Executed) |> ExecutionResult.Stepped
|
|
||||||
| [ retVal ] ->
|
|
||||||
let retType =
|
|
||||||
threadStateAtEndOfMethod.MethodState.ExecutingMethod.Signature.ReturnType
|
|
||||||
|
|
||||||
state
|
state |> IlMachineState.pushToEvalStack toPush currentThread
|
||||||
|> IlMachineState.pushToStackCoerced retVal retType currentThread
|
| _ ->
|
||||||
|> Tuple.withRight WhatWeDid.Executed
|
failwith
|
||||||
|> ExecutionResult.Stepped
|
"Unexpected interpretation result has a local evaluation stack with more than one element on RET"
|
||||||
| _ ->
|
|
||||||
failwith
|
state |> Tuple.withRight WhatWeDid.Executed |> ExecutionResult.Stepped
|
||||||
"Unexpected interpretation result has a local evaluation stack with more than one element on RET"
|
|
||||||
|
|
||||||
| LdcI4_0 ->
|
| LdcI4_0 ->
|
||||||
state
|
state
|
||||||
|
Reference in New Issue
Block a user