Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d3974ad590
|
|||
|
098d689750
|
|||
|
3edf0324c7
|
|||
|
92f0d22805
|
|||
|
811f4f4959
|
|||
| c041518c9b |
@@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
[0.2.4]
|
||||||
|
|
||||||
|
- Groundwork laid to collect and track source maps
|
||||||
|
- IC Housing will now display the `Slang` source error line (if available)
|
||||||
|
instead of the `IC10` source error line
|
||||||
|
|
||||||
[0.2.3]
|
[0.2.3]
|
||||||
|
|
||||||
- Fixed stack underflow with function invocations
|
- Fixed stack underflow with function invocations
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
<Name>Slang</Name>
|
<Name>Slang</Name>
|
||||||
<Author>JoeDiertay</Author>
|
<Author>JoeDiertay</Author>
|
||||||
<Version>0.2.3</Version>
|
<Version>0.2.4</Version>
|
||||||
<Description>
|
<Description>
|
||||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||||
|
|
||||||
|
|||||||
@@ -113,6 +113,34 @@ public static unsafe class SlangExtensions
|
|||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static unsafe List<SourceMapEntry> ToList(this Vec_FfiSourceMapEntry_t vec)
|
||||||
|
{
|
||||||
|
var toReturn = new List<SourceMapEntry>((int)vec.len);
|
||||||
|
|
||||||
|
var currentPtr = vec.ptr;
|
||||||
|
|
||||||
|
for (int i = 0; i < (int)vec.len; i++)
|
||||||
|
{
|
||||||
|
var item = currentPtr[i];
|
||||||
|
|
||||||
|
toReturn.Add(
|
||||||
|
new SourceMapEntry
|
||||||
|
{
|
||||||
|
Ic10Line = item.line_number,
|
||||||
|
SlangSource = new Range
|
||||||
|
{
|
||||||
|
EndCol = item.span.end_col,
|
||||||
|
EndLine = item.span.end_line,
|
||||||
|
StartCol = item.span.start_col,
|
||||||
|
StartLine = item.span.start_line,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
private static uint GetColorForKind(uint kind)
|
private static uint GetColorForKind(uint kind)
|
||||||
{
|
{
|
||||||
switch (kind)
|
switch (kind)
|
||||||
|
|||||||
@@ -71,18 +71,6 @@ public unsafe struct Vec_uint8_t {
|
|||||||
public UIntPtr cap;
|
public UIntPtr cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe partial class Ffi {
|
|
||||||
/// <summary>
|
|
||||||
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
|
||||||
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
|
||||||
/// This should result in the ability to compile many times without triggering frame drops
|
|
||||||
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
|
||||||
/// </summary>
|
|
||||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
|
||||||
Vec_uint8_t compile_from_string (
|
|
||||||
slice_ref_uint16_t input);
|
|
||||||
}
|
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
||||||
public unsafe struct FfiRange_t {
|
public unsafe struct FfiRange_t {
|
||||||
public UInt32 start_col;
|
public UInt32 start_col;
|
||||||
@@ -94,6 +82,44 @@ public unsafe struct FfiRange_t {
|
|||||||
public UInt32 end_line;
|
public UInt32 end_line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential, Size = 20)]
|
||||||
|
public unsafe struct FfiSourceMapEntry_t {
|
||||||
|
public UInt32 line_number;
|
||||||
|
|
||||||
|
public FfiRange_t span;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
|
||||||
|
/// </summary>
|
||||||
|
[StructLayout(LayoutKind.Sequential, Size = 24)]
|
||||||
|
public unsafe struct Vec_FfiSourceMapEntry_t {
|
||||||
|
public FfiSourceMapEntry_t * ptr;
|
||||||
|
|
||||||
|
public UIntPtr len;
|
||||||
|
|
||||||
|
public UIntPtr cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential, Size = 48)]
|
||||||
|
public unsafe struct FfiCompilationResult_t {
|
||||||
|
public Vec_uint8_t output_code;
|
||||||
|
|
||||||
|
public Vec_FfiSourceMapEntry_t source_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public unsafe partial class Ffi {
|
||||||
|
/// <summary>
|
||||||
|
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
||||||
|
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
||||||
|
/// This should result in the ability to compile many times without triggering frame drops
|
||||||
|
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
||||||
|
/// </summary>
|
||||||
|
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||||
|
FfiCompilationResult_t compile_from_string (
|
||||||
|
slice_ref_uint16_t input);
|
||||||
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential, Size = 48)]
|
[StructLayout(LayoutKind.Sequential, Size = 48)]
|
||||||
public unsafe struct FfiDiagnostic_t {
|
public unsafe struct FfiDiagnostic_t {
|
||||||
public Vec_uint8_t message;
|
public Vec_uint8_t message;
|
||||||
@@ -146,6 +172,12 @@ public unsafe partial class Ffi {
|
|||||||
Vec_FfiDocumentedItem_t v);
|
Vec_FfiDocumentedItem_t v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public unsafe partial class Ffi {
|
||||||
|
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||||
|
void free_ffi_compilation_result (
|
||||||
|
FfiCompilationResult_t input);
|
||||||
|
}
|
||||||
|
|
||||||
public unsafe partial class Ffi {
|
public unsafe partial class Ffi {
|
||||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||||
void free_ffi_diagnostic_vec (
|
void free_ffi_diagnostic_vec (
|
||||||
|
|||||||
@@ -15,11 +15,59 @@ public static class GlobalCode
|
|||||||
// so that save file data is smaller
|
// so that save file data is smaller
|
||||||
private static Dictionary<Guid, string> codeDict = new();
|
private static Dictionary<Guid, string> codeDict = new();
|
||||||
|
|
||||||
|
// This Dictionary stores the source maps for the given SLANG_REF, where
|
||||||
|
// the key is the IC10 line, and the value is a List of Slang ranges where that
|
||||||
|
// line would have come from
|
||||||
|
private static Dictionary<Guid, Dictionary<uint, List<Range>>> sourceMaps = new();
|
||||||
|
|
||||||
public static void ClearCache()
|
public static void ClearCache()
|
||||||
{
|
{
|
||||||
codeDict.Clear();
|
codeDict.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void SetSourceMap(Guid reference, List<SourceMapEntry> sourceMapEntries)
|
||||||
|
{
|
||||||
|
var builtDictionary = new Dictionary<uint, List<Range>>();
|
||||||
|
|
||||||
|
foreach (var entry in sourceMapEntries)
|
||||||
|
{
|
||||||
|
if (!builtDictionary.ContainsKey(entry.Ic10Line))
|
||||||
|
{
|
||||||
|
builtDictionary[entry.Ic10Line] = new();
|
||||||
|
}
|
||||||
|
builtDictionary[entry.Ic10Line].Add(entry.SlangSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceMaps[reference] = builtDictionary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool GetSlangErrorLineFromICError(
|
||||||
|
Guid reference,
|
||||||
|
uint icErrorLine,
|
||||||
|
out uint slangSrc,
|
||||||
|
out Range slangSpan
|
||||||
|
)
|
||||||
|
{
|
||||||
|
slangSrc = icErrorLine;
|
||||||
|
slangSpan = new Range { };
|
||||||
|
|
||||||
|
if (!sourceMaps.ContainsKey(reference))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var foundRange = sourceMaps[reference][icErrorLine];
|
||||||
|
|
||||||
|
if (foundRange is null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
slangSrc = foundRange[0].StartLine;
|
||||||
|
slangSpan = foundRange[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static string GetSource(Guid reference)
|
public static string GetSource(Guid reference)
|
||||||
{
|
{
|
||||||
if (!codeDict.ContainsKey(reference))
|
if (!codeDict.ContainsKey(reference))
|
||||||
|
|||||||
@@ -10,10 +10,23 @@ using StationeersIC10Editor;
|
|||||||
|
|
||||||
public struct Range
|
public struct Range
|
||||||
{
|
{
|
||||||
public uint StartCol;
|
public uint StartCol = 0;
|
||||||
public uint EndCol;
|
public uint EndCol = 0;
|
||||||
public uint StartLine;
|
public uint StartLine = 0;
|
||||||
public uint EndLine;
|
public uint EndLine = 0;
|
||||||
|
|
||||||
|
public Range(uint startLine, uint startCol, uint endLine, uint endCol)
|
||||||
|
{
|
||||||
|
StartLine = startLine;
|
||||||
|
StartCol = startCol;
|
||||||
|
EndLine = endLine;
|
||||||
|
EndCol = endCol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"L{StartLine}C{StartCol} - L{EndLine}C{EndCol}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Diagnostic
|
public struct Diagnostic
|
||||||
@@ -23,6 +36,17 @@ public struct Diagnostic
|
|||||||
public Range Range;
|
public Range Range;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct SourceMapEntry
|
||||||
|
{
|
||||||
|
public Range SlangSource;
|
||||||
|
public uint Ic10Line;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"IC10: {Ic10Line} Slang: `{SlangSource}`";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class Marshal
|
public static class Marshal
|
||||||
{
|
{
|
||||||
private static IntPtr _libraryHandle = IntPtr.Zero;
|
private static IntPtr _libraryHandle = IntPtr.Zero;
|
||||||
@@ -78,11 +102,16 @@ public static class Marshal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static unsafe bool CompileFromString(string inputString, out string compiledString)
|
public static unsafe bool CompileFromString(
|
||||||
|
string inputString,
|
||||||
|
out string compiledString,
|
||||||
|
out List<SourceMapEntry> sourceMapEntries
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrEmpty(inputString) || !EnsureLibLoaded())
|
if (String.IsNullOrEmpty(inputString) || !EnsureLibLoaded())
|
||||||
{
|
{
|
||||||
compiledString = String.Empty;
|
compiledString = String.Empty;
|
||||||
|
sourceMapEntries = new();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,19 +124,16 @@ public static class Marshal
|
|||||||
};
|
};
|
||||||
|
|
||||||
var result = Ffi.compile_from_string(input);
|
var result = Ffi.compile_from_string(input);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if ((ulong)result.len < 1)
|
sourceMapEntries = result.source_map.ToList();
|
||||||
{
|
compiledString = result.output_code.AsString();
|
||||||
compiledString = String.Empty;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
compiledString = result.AsString();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
result.Drop();
|
Ffi.free_ffi_compilation_result(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,34 @@
|
|||||||
namespace Slang;
|
namespace Slang;
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using Assets.Scripts.Objects;
|
using Assets.Scripts.Objects;
|
||||||
using Assets.Scripts.Objects.Electrical;
|
using Assets.Scripts.Objects.Electrical;
|
||||||
using Assets.Scripts.Objects.Motherboards;
|
using Assets.Scripts.Objects.Motherboards;
|
||||||
using Assets.Scripts.UI;
|
using Assets.Scripts.UI;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
|
|
||||||
|
class LineErrorData
|
||||||
|
{
|
||||||
|
public AsciiString SourceRef;
|
||||||
|
public uint IC10ErrorSource;
|
||||||
|
public string SlangErrorReference;
|
||||||
|
public Range SlangErrorSpan;
|
||||||
|
|
||||||
|
public LineErrorData(
|
||||||
|
AsciiString sourceRef,
|
||||||
|
uint ic10ErrorSource,
|
||||||
|
string slangErrorRef,
|
||||||
|
Range slangErrorSpan
|
||||||
|
)
|
||||||
|
{
|
||||||
|
this.SourceRef = sourceRef;
|
||||||
|
this.IC10ErrorSource = ic10ErrorSource;
|
||||||
|
this.SlangErrorReference = slangErrorRef;
|
||||||
|
this.SlangErrorSpan = slangErrorSpan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPatch]
|
[HarmonyPatch]
|
||||||
public static class SlangPatches
|
public static class SlangPatches
|
||||||
{
|
{
|
||||||
@@ -14,6 +36,9 @@ public static class SlangPatches
|
|||||||
private static AsciiString? _motherboardCachedCode;
|
private static AsciiString? _motherboardCachedCode;
|
||||||
private static Guid? _currentlyEditingGuid;
|
private static Guid? _currentlyEditingGuid;
|
||||||
|
|
||||||
|
private static ConditionalWeakTable<ProgrammableChip, LineErrorData> _errorReferenceTable =
|
||||||
|
new();
|
||||||
|
|
||||||
[HarmonyPatch(
|
[HarmonyPatch(
|
||||||
typeof(ProgrammableChipMotherboard),
|
typeof(ProgrammableChipMotherboard),
|
||||||
nameof(ProgrammableChipMotherboard.InputFinished)
|
nameof(ProgrammableChipMotherboard.InputFinished)
|
||||||
@@ -26,7 +51,7 @@ public static class SlangPatches
|
|||||||
// guard to ensure we have valid IC10 before continuing
|
// guard to ensure we have valid IC10 before continuing
|
||||||
if (
|
if (
|
||||||
!SlangPlugin.IsSlangSource(ref result)
|
!SlangPlugin.IsSlangSource(ref result)
|
||||||
|| !Marshal.CompileFromString(result, out string compiled)
|
|| !Marshal.CompileFromString(result, out var compiled, out var sourceMap)
|
||||||
|| string.IsNullOrEmpty(compiled)
|
|| string.IsNullOrEmpty(compiled)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -37,6 +62,7 @@ public static class SlangPatches
|
|||||||
|
|
||||||
// Ensure we cache this compiled code for later retreival.
|
// Ensure we cache this compiled code for later retreival.
|
||||||
GlobalCode.SetSource(thisRef, result);
|
GlobalCode.SetSource(thisRef, result);
|
||||||
|
GlobalCode.SetSourceMap(thisRef, sourceMap);
|
||||||
|
|
||||||
_currentlyEditingGuid = null;
|
_currentlyEditingGuid = null;
|
||||||
|
|
||||||
@@ -140,6 +166,100 @@ public static class SlangPatches
|
|||||||
chipData.SourceCode = code;
|
chipData.SourceCode = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(
|
||||||
|
typeof(ProgrammableChip),
|
||||||
|
nameof(ProgrammableChip.ErrorLineNumberString),
|
||||||
|
MethodType.Getter
|
||||||
|
)]
|
||||||
|
[HarmonyPostfix]
|
||||||
|
public static void pgc_ErrorLineNumberString(ProgrammableChip __instance, ref string __result)
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
String.IsNullOrEmpty(__result)
|
||||||
|
|| !uint.TryParse(__result.Trim(), out var ic10ErrorLineNumber)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sourceAscii = __instance.GetSourceCode();
|
||||||
|
|
||||||
|
if (_errorReferenceTable.TryGetValue(__instance, out var cache))
|
||||||
|
{
|
||||||
|
if (cache.SourceRef.Equals(sourceAscii) && cache.IC10ErrorSource == ic10ErrorLineNumber)
|
||||||
|
{
|
||||||
|
__result = cache.SlangErrorReference;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = System.Text.Encoding.UTF8.GetString(
|
||||||
|
System.Text.Encoding.ASCII.GetBytes(__instance.GetSourceCode())
|
||||||
|
);
|
||||||
|
|
||||||
|
var slangIndex = source.LastIndexOf(GlobalCode.SLANG_REF);
|
||||||
|
|
||||||
|
if (
|
||||||
|
slangIndex < 0
|
||||||
|
|| !Guid.TryParse(
|
||||||
|
source
|
||||||
|
.Substring(
|
||||||
|
source.LastIndexOf(GlobalCode.SLANG_REF) + GlobalCode.SLANG_REF.Length
|
||||||
|
)
|
||||||
|
.Trim(),
|
||||||
|
out var slangGuid
|
||||||
|
)
|
||||||
|
|| !GlobalCode.GetSlangErrorLineFromICError(
|
||||||
|
slangGuid,
|
||||||
|
ic10ErrorLineNumber,
|
||||||
|
out var slangErrorLineNumber,
|
||||||
|
out var slangSpan
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
L.Warning($"IC error at: {__result} -- Slang source error line: {slangErrorLineNumber}");
|
||||||
|
__result = slangErrorLineNumber.ToString();
|
||||||
|
_errorReferenceTable.Remove(__instance);
|
||||||
|
_errorReferenceTable.Add(
|
||||||
|
__instance,
|
||||||
|
new LineErrorData(
|
||||||
|
sourceAscii,
|
||||||
|
ic10ErrorLineNumber,
|
||||||
|
slangErrorLineNumber.ToString(),
|
||||||
|
slangSpan
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(
|
||||||
|
typeof(ProgrammableChip),
|
||||||
|
nameof(ProgrammableChip.SetSourceCode),
|
||||||
|
new Type[] { typeof(string) }
|
||||||
|
)]
|
||||||
|
[HarmonyPostfix]
|
||||||
|
public static void pgc_SetSourceCode_string(ProgrammableChip __instance, string sourceCode)
|
||||||
|
{
|
||||||
|
_errorReferenceTable.Remove(__instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(
|
||||||
|
typeof(ProgrammableChip),
|
||||||
|
nameof(ProgrammableChip.SetSourceCode),
|
||||||
|
new Type[] { typeof(string), typeof(ICircuitHolder) }
|
||||||
|
)]
|
||||||
|
[HarmonyPostfix]
|
||||||
|
public static void pgc_SetSourceCode_string_parent(
|
||||||
|
ProgrammableChip __instance,
|
||||||
|
string sourceCode,
|
||||||
|
ICircuitHolder parent
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_errorReferenceTable.Remove(__instance);
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPatch(
|
[HarmonyPatch(
|
||||||
typeof(ProgrammableChipMotherboard),
|
typeof(ProgrammableChipMotherboard),
|
||||||
nameof(ProgrammableChipMotherboard.SerializeSave)
|
nameof(ProgrammableChipMotherboard.SerializeSave)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace Slang
|
|||||||
{
|
{
|
||||||
public const string PluginGuid = "com.biddydev.slang";
|
public const string PluginGuid = "com.biddydev.slang";
|
||||||
public const string PluginName = "Slang";
|
public const string PluginName = "Slang";
|
||||||
public const string PluginVersion = "0.1.1";
|
public const string PluginVersion = "0.2.4";
|
||||||
|
|
||||||
public static Mod MOD = new Mod(PluginName, PluginVersion);
|
public static Mod MOD = new Mod(PluginName, PluginVersion);
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyName>StationeersSlang</AssemblyName>
|
<AssemblyName>StationeersSlang</AssemblyName>
|
||||||
<Description>Slang Compiler Bridge</Description>
|
<Description>Slang Compiler Bridge</Description>
|
||||||
<Version>0.2.3</Version>
|
<Version>0.2.4</Version>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
3
rust_compiler/Cargo.lock
generated
3
rust_compiler/Cargo.lock
generated
@@ -571,6 +571,7 @@ dependencies = [
|
|||||||
"helpers",
|
"helpers",
|
||||||
"lsp-types",
|
"lsp-types",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
|
"safer-ffi",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokenizer",
|
"tokenizer",
|
||||||
]
|
]
|
||||||
@@ -909,7 +910,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.2.3"
|
version = "0.2.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.2.3"
|
version = "0.2.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|||||||
@@ -3,4 +3,4 @@ mod test;
|
|||||||
mod v1;
|
mod v1;
|
||||||
mod variable_manager;
|
mod variable_manager;
|
||||||
|
|
||||||
pub use v1::{Compiler, CompilerConfig, Error};
|
pub use v1::{CompilationResult, Compiler, CompilerConfig, Error};
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ macro_rules! compile {
|
|||||||
&mut writer,
|
&mut writer,
|
||||||
Some(crate::CompilerConfig { debug: true }),
|
Some(crate::CompilerConfig { debug: true }),
|
||||||
);
|
);
|
||||||
compiler.compile()
|
compiler.compile().errors
|
||||||
}};
|
}};
|
||||||
|
|
||||||
(debug $source:expr) => {{
|
(debug $source:expr) => {{
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@ edition = "2024"
|
|||||||
tokenizer = { path = "../tokenizer" }
|
tokenizer = { path = "../tokenizer" }
|
||||||
helpers = { path = "../helpers" }
|
helpers = { path = "../helpers" }
|
||||||
lsp-types = { workspace = true }
|
lsp-types = { workspace = true }
|
||||||
|
safer-ffi = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use super::sys_call::SysCall;
|
use super::sys_call::SysCall;
|
||||||
use crate::sys_call;
|
use crate::sys_call;
|
||||||
|
use safer_ffi::prelude::*;
|
||||||
use std::{borrow::Cow, ops::Deref};
|
use std::{borrow::Cow, ops::Deref};
|
||||||
use tokenizer::token::Number;
|
use tokenizer::token::Number;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use compiler::Compiler;
|
use compiler::{CompilationResult, Compiler};
|
||||||
use helpers::Documentation;
|
use helpers::Documentation;
|
||||||
use parser::{sys_call::SysCall, Parser};
|
use parser::{sys_call::SysCall, tree_node::Span, Parser};
|
||||||
use safer_ffi::prelude::*;
|
use safer_ffi::prelude::*;
|
||||||
use std::io::BufWriter;
|
use std::io::BufWriter;
|
||||||
use tokenizer::{
|
use tokenizer::{
|
||||||
@@ -8,6 +8,20 @@ use tokenizer::{
|
|||||||
Tokenizer,
|
Tokenizer,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive_ReprC]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct FfiSourceMapEntry {
|
||||||
|
pub line_number: u32,
|
||||||
|
pub span: FfiRange,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive_ReprC]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct FfiCompilationResult {
|
||||||
|
pub output_code: safer_ffi::String,
|
||||||
|
pub source_map: safer_ffi::Vec<FfiSourceMapEntry>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive_ReprC]
|
#[derive_ReprC]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct FfiToken {
|
pub struct FfiToken {
|
||||||
@@ -34,6 +48,17 @@ pub struct FfiDocumentedItem {
|
|||||||
docs: safer_ffi::String,
|
docs: safer_ffi::String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Span> for FfiRange {
|
||||||
|
fn from(value: Span) -> Self {
|
||||||
|
Self {
|
||||||
|
start_line: value.start_line as u32,
|
||||||
|
end_line: value.end_line as u32,
|
||||||
|
start_col: value.start_col as u32,
|
||||||
|
end_col: value.end_col as u32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<lsp_types::Range> for FfiRange {
|
impl From<lsp_types::Range> for FfiRange {
|
||||||
fn from(value: lsp_types::Range) -> Self {
|
fn from(value: lsp_types::Range) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -69,6 +94,11 @@ impl From<lsp_types::Diagnostic> for FfiDiagnostic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[ffi_export]
|
||||||
|
pub fn free_ffi_compilation_result(input: FfiCompilationResult) {
|
||||||
|
drop(input)
|
||||||
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
pub fn free_ffi_token_vec(v: safer_ffi::Vec<FfiToken>) {
|
pub fn free_ffi_token_vec(v: safer_ffi::Vec<FfiToken>) {
|
||||||
drop(v)
|
drop(v)
|
||||||
@@ -94,7 +124,7 @@ pub fn free_docs_vec(v: safer_ffi::Vec<FfiDocumentedItem>) {
|
|||||||
/// This should result in the ability to compile many times without triggering frame drops
|
/// This should result in the ability to compile many times without triggering frame drops
|
||||||
/// from the GC from a `GetBytes()` call on a string in C#.
|
/// from the GC from a `GetBytes()` call on a string in C#.
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::String {
|
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> FfiCompilationResult {
|
||||||
let res = std::panic::catch_unwind(|| {
|
let res = std::panic::catch_unwind(|| {
|
||||||
let input = String::from_utf16_lossy(input.as_slice());
|
let input = String::from_utf16_lossy(input.as_slice());
|
||||||
let mut writer = BufWriter::new(Vec::new());
|
let mut writer = BufWriter::new(Vec::new());
|
||||||
@@ -103,19 +133,45 @@ pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::
|
|||||||
let parser = Parser::new(tokenizer);
|
let parser = Parser::new(tokenizer);
|
||||||
let compiler = Compiler::new(parser, &mut writer, None);
|
let compiler = Compiler::new(parser, &mut writer, None);
|
||||||
|
|
||||||
if !compiler.compile().is_empty() {
|
let res = compiler.compile();
|
||||||
return safer_ffi::String::EMPTY;
|
|
||||||
|
if !res.errors.is_empty() {
|
||||||
|
return (safer_ffi::String::EMPTY, res.source_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
let Ok(compiled_vec) = writer.into_inner() else {
|
let Ok(compiled_vec) = writer.into_inner() else {
|
||||||
return safer_ffi::String::EMPTY;
|
return (safer_ffi::String::EMPTY, res.source_map);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Safety: I know the compiler only outputs valid utf8
|
// Safety: I know the compiler only outputs valid utf8
|
||||||
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) })
|
(
|
||||||
|
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) }),
|
||||||
|
res.source_map,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
res.unwrap_or("".into())
|
if let Ok((res_str, source_map)) = res {
|
||||||
|
FfiCompilationResult {
|
||||||
|
source_map: source_map
|
||||||
|
.into_iter()
|
||||||
|
.flat_map(|(k, v)| {
|
||||||
|
v.into_iter()
|
||||||
|
.map(|span| FfiSourceMapEntry {
|
||||||
|
span: span.into(),
|
||||||
|
line_number: k as u32,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.into(),
|
||||||
|
output_code: res_str,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FfiCompilationResult {
|
||||||
|
output_code: "".into(),
|
||||||
|
source_map: vec![].into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
@@ -184,7 +240,9 @@ pub fn diagnose_source(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::Vec<
|
|||||||
let tokenizer = Tokenizer::from(input.as_str());
|
let tokenizer = Tokenizer::from(input.as_str());
|
||||||
let compiler = Compiler::new(Parser::new(tokenizer), &mut writer, None);
|
let compiler = Compiler::new(Parser::new(tokenizer), &mut writer, None);
|
||||||
|
|
||||||
let diagnosis = compiler.compile();
|
let CompilationResult {
|
||||||
|
errors: diagnosis, ..
|
||||||
|
} = compiler.compile();
|
||||||
|
|
||||||
let mut result_vec: Vec<FfiDiagnostic> = Vec::with_capacity(diagnosis.len());
|
let mut result_vec: Vec<FfiDiagnostic> = Vec::with_capacity(diagnosis.len());
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#![allow(clippy::result_large_err)]
|
#![allow(clippy::result_large_err)]
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use compiler::Compiler;
|
use compiler::{CompilationResult, Compiler};
|
||||||
use parser::Parser as ASTParser;
|
use parser::Parser as ASTParser;
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
@@ -90,7 +90,7 @@ fn run_logic<'a>() -> Result<(), Error<'a>> {
|
|||||||
|
|
||||||
let compiler = Compiler::new(parser, &mut writer, None);
|
let compiler = Compiler::new(parser, &mut writer, None);
|
||||||
|
|
||||||
let errors = compiler.compile();
|
let CompilationResult { errors, .. } = compiler.compile();
|
||||||
|
|
||||||
if !errors.is_empty() {
|
if !errors.is_empty() {
|
||||||
let mut std_error = stderr();
|
let mut std_error = stderr();
|
||||||
|
|||||||
Reference in New Issue
Block a user