Skip to content

Commit

Permalink
Add doc generation support, core.fail
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed Nov 12, 2023
1 parent d3a4e76 commit 789304e
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 138 deletions.
2 changes: 1 addition & 1 deletion FrontEnd/EVIL.evil/EVIL.evil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<AssemblyName>evil</AssemblyName>
<Version>1.4.0</Version>
<Version>1.5.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
75 changes: 57 additions & 18 deletions FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public partial class EvmFrontEnd
{ "h|help", "display this message and quit.", (h) => _displayHelpAndQuit = h != null },
{ "v|version", "display compiler and VM version information.", (v) => _displayVersionAndQuit = v != null },
{ "I|include-dir=", "add include directory to the list of search paths.", (I) => _includeHandler.AddIncludeSearchPath(I) },
{ "g|gen-docs", "generate documentation for all detected native modules.", (g) => _generateModuleDocsAndQuit = g != null }
};

private static bool _displayHelpAndQuit;
private static bool _displayVersionAndQuit;
private static bool _generateModuleDocsAndQuit;

public async Task Run(string[] args)
{
Expand All @@ -49,6 +51,12 @@ public async Task Run(string[] args)
Terminate(BuildVersionBanner());
}

if (_generateModuleDocsAndQuit)
{
GenerateModuleDocs();
Terminate();
}

if (!extra.Any())
{
Terminate(
Expand Down Expand Up @@ -127,24 +135,7 @@ public async Task Run(string[] args)
Console.WriteLine(_compiler.Log.ToString());
}

_runtime.RegisterBuiltInModules();

var moduleStorePath = Path.Combine(AppContext.BaseDirectory, "modules");
if (Directory.Exists(moduleStorePath))
{
try
{
_runtimeModuleLoader.RegisterUserRuntimeModules(moduleStorePath);
}
catch (RuntimeModuleLoadException rmle)
{
var sb = new StringBuilder();
sb.AppendLine($"Failed to load user runtime module '{rmle.FilePath}'.");
sb.AppendLine(rmle.ToString());

Terminate(sb.ToString(), exitCode: ExitCode.ModuleLoaderFailed);
}
}
RegisterAllModules();

var initChunks = new List<Chunk>();
foreach (var chunk in script.Chunks)
Expand Down Expand Up @@ -209,6 +200,54 @@ private List<string> InitializeOptions(string[] args)
return fileNames;
}

private void GenerateModuleDocs()
{
var modules = RegisterAllModules();
var docsPath = Path.Combine(
AppContext.BaseDirectory,
"docs"
);

Directory.CreateDirectory(docsPath);

foreach (var module in modules)
{
var filePath = Path.Combine(docsPath, $"evrt_{module.FullyQualifiedName}.doc.md");
Console.WriteLine($"Generating '{filePath}'...");

using (var sw = new StreamWriter(filePath))
{
sw.WriteLine(module.Describe());
}
}
}

private List<RuntimeModule> RegisterAllModules()
{
var ret = new List<RuntimeModule>();

ret.AddRange(_runtime.RegisterBuiltInModules());

var moduleStorePath = Path.Combine(AppContext.BaseDirectory, "modules");
if (Directory.Exists(moduleStorePath))
{
try
{
ret.AddRange(_runtimeModuleLoader.RegisterUserRuntimeModules(moduleStorePath));
}
catch (RuntimeModuleLoadException rmle)
{
var sb = new StringBuilder();
sb.AppendLine($"Failed to load user runtime module '{rmle.FilePath}'.");
sb.AppendLine(rmle.ToString());

Terminate(sb.ToString(), exitCode: ExitCode.ModuleLoaderFailed);
}
}

return ret;
}

private bool IsValidInitChunk(Chunk chunk)
=> chunk.HasAttribute(AttributeNames.VmInit);

Expand Down
3 changes: 3 additions & 0 deletions Shared/Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<Content Include="$(MSBuildThisFileDirectory)..\README.md">
<Link>README.md</Link>
</Content>
<Content Include="$(MSBuildThisFileDirectory)..\.github\workflows\build-frontend-with-runtime.yml">
<Link>.github/workflows/build-frontend-with-runtime.yml</Link>
</Content>
<Content Include="$(MSBuildThisFileDirectory)highlighters\evil_katepart.xml" />
<Content Include="$(MSBuildThisFileDirectory)highlighters\evil_npp.xml" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion VirtualMachine/Ceres.Runtime/Ceres.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>

<Version>0.6.0</Version>
<Version>0.7.0</Version>
<AssemblyName>Ceres.Runtime</AssemblyName>
<AssemblyTitle>Ceres.Runtime</AssemblyTitle>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
16 changes: 16 additions & 0 deletions VirtualMachine/Ceres.Runtime/Modules/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,21 @@ private static DynamicValue StackTraceString(Fiber fiber, params DynamicValue[]

return fiber.StackTrace(skipNativeFrames);
}

[RuntimeModuleFunction("fail")]
[EvilDocFunction(
"Terminates VM execution with a user-requested runtime failure. This function does not return."
)]
[EvilDocArgument(
"message",
"The message that should be displayed for the failure.",
DynamicValueType.String,
DefaultValue = "no details provided"
)]
private static DynamicValue Fail(Fiber fiber, params DynamicValue[] args)
{
args.OptionalStringAt(0, "no details provided", out var message);
throw new UserFailException(message, fiber, args);
}
}
}
9 changes: 8 additions & 1 deletion VirtualMachine/Ceres.Runtime/Modules/StringModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static DynamicValue Split(Fiber _, params DynamicValue[] args)
ReturnType = DynamicValueType.String,
IsVariadic = true
)]
[EvilDocArgument("separator", "A separator to be inserted between each given value during concatenation.")]
[EvilDocArgument("separator", "A separator to be inserted between each given value during concatenation.", DynamicValueType.String)]
[EvilDocArgument("...", "An arbitrary amount of values to be concatenated into a String.")]
private static DynamicValue Join(Fiber _, params DynamicValue[] args)
{
Expand All @@ -140,6 +140,13 @@ private static DynamicValue Join(Fiber _, params DynamicValue[] args)
}

[RuntimeModuleFunction("rep")]
[EvilDocFunction(
"Repeats the provided String `count` times.",
Returns = "A String containing `str` repeated `count` times.",
ReturnType = DynamicValueType.String
)]
[EvilDocArgument("str", "A String to be repeated.", DynamicValueType.String)]
[EvilDocArgument("count", "An integer specifying the amount of times to repeat `str`.", DynamicValueType.Number)]
private static DynamicValue Repeat(Fiber _, params DynamicValue[] args)
{
args.ExpectExactly(2)
Expand Down
Loading

0 comments on commit 789304e

Please sign in to comment.