Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d3974ad590
|
|||
|
098d689750
|
|||
|
3edf0324c7
|
|||
|
92f0d22805
|
|||
|
811f4f4959
|
|||
| c041518c9b | |||
|
2b26d0d278
|
|||
|
236b50c813
|
|||
|
342b1ab107
|
|||
| 0732f68bcf | |||
|
0ac010ef8f
|
|||
|
c2208fbb15
|
|||
| 295f062797 |
24
Changelog.md
24
Changelog.md
@@ -1,5 +1,29 @@
|
|||||||
# 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]
|
||||||
|
|
||||||
|
- Fixed stack underflow with function invocations
|
||||||
|
- They are still "heavy", but they should work as expected now
|
||||||
|
- Fixed issue where syscall functions were not allowed as infix operators
|
||||||
|
|
||||||
|
[0.2.2]
|
||||||
|
|
||||||
|
- Fixed some formatting issues when converting Markdown to Text Mesh Pro for
|
||||||
|
Stationpedia
|
||||||
|
- Added support for ternary expressions
|
||||||
|
- `let i = someValue ? 4 : 5;`
|
||||||
|
- `i = someValue ? 4 : 5;`
|
||||||
|
- This greedily evaluates both sides, so side effects like calling functions
|
||||||
|
is not recommended i.e.
|
||||||
|
- `i = someValue : doSomething() : doSomethingElse();`
|
||||||
|
- Both sides will be evaluated before calling the `select` instruction
|
||||||
|
|
||||||
[0.2.1]
|
[0.2.1]
|
||||||
|
|
||||||
- Added support for `loadSlot` and `setSlot`
|
- Added support for `loadSlot` and `setSlot`
|
||||||
|
|||||||
@@ -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.1</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,17 +1,43 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
private static ProgrammableChipMotherboard? _currentlyEditingMotherboard;
|
private static ProgrammableChipMotherboard? _currentlyEditingMotherboard;
|
||||||
private static AsciiString? _motherboardCachedCode;
|
private static AsciiString? _motherboardCachedCode;
|
||||||
|
private static Guid? _currentlyEditingGuid;
|
||||||
|
|
||||||
|
private static ConditionalWeakTable<ProgrammableChip, LineErrorData> _errorReferenceTable =
|
||||||
|
new();
|
||||||
|
|
||||||
[HarmonyPatch(
|
[HarmonyPatch(
|
||||||
typeof(ProgrammableChipMotherboard),
|
typeof(ProgrammableChipMotherboard),
|
||||||
@@ -25,17 +51,20 @@ 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)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var thisRef = Guid.NewGuid();
|
var thisRef = _currentlyEditingGuid ?? Guid.NewGuid();
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
// Append REF to the bottom
|
// Append REF to the bottom
|
||||||
compiled += $"\n{GlobalCode.SLANG_REF}{thisRef}";
|
compiled += $"\n{GlobalCode.SLANG_REF}{thisRef}";
|
||||||
@@ -77,6 +106,7 @@ public static class SlangPatches
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_currentlyEditingGuid = sourceRef;
|
||||||
var slangSource = GlobalCode.GetSource(sourceRef);
|
var slangSource = GlobalCode.GetSource(sourceRef);
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(slangSource))
|
if (string.IsNullOrEmpty(slangSource))
|
||||||
@@ -136,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)
|
||||||
@@ -223,6 +347,7 @@ public static class SlangPatches
|
|||||||
|
|
||||||
_currentlyEditingMotherboard = null;
|
_currentlyEditingMotherboard = null;
|
||||||
_motherboardCachedCode = null;
|
_motherboardCachedCode = null;
|
||||||
|
_currentlyEditingGuid = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,18 @@ public static class TextMeshProFormatter
|
|||||||
RegexOptions.Singleline
|
RegexOptions.Singleline
|
||||||
);
|
);
|
||||||
|
|
||||||
// 3. Handle Headers (## Header)
|
|
||||||
// Convert ## Header to large bold text
|
|
||||||
text = Regex.Replace(
|
text = Regex.Replace(
|
||||||
text,
|
text,
|
||||||
@"^##(\s+)?(.+)$",
|
@"^\s*##\s+(.+)$",
|
||||||
"<size=120%><b>$1</b></size>",
|
"<size=110%><color=#ffffff><b>$1</b></color></size>",
|
||||||
|
RegexOptions.Multiline
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Handle # Headers SECOND (General)
|
||||||
|
text = Regex.Replace(
|
||||||
|
text,
|
||||||
|
@"^\s*#\s+(.+)$",
|
||||||
|
"<size=120%><color=#ffffff><b>$1</b></color></size>",
|
||||||
RegexOptions.Multiline
|
RegexOptions.Multiline
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -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.1</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.1"
|
version = "0.2.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "slang"
|
name = "slang"
|
||||||
version = "0.2.1"
|
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};
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use crate::compile;
|
use crate::compile;
|
||||||
|
use anyhow::Result;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_binary_expression() -> anyhow::Result<()> {
|
fn simple_binary_expression() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -26,7 +27,7 @@ fn simple_binary_expression() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_binary_expressions() -> anyhow::Result<()> {
|
fn nested_binary_expressions() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -51,6 +52,8 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
|||||||
add r1 r10 r9
|
add r1 r10 r9
|
||||||
mul r2 r1 r8
|
mul r2 r1 r8
|
||||||
move r15 r2
|
move r15 r2
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -71,7 +74,7 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn stress_test_constant_folding() -> anyhow::Result<()> {
|
fn stress_test_constant_folding() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
@@ -94,7 +97,7 @@ fn stress_test_constant_folding() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
r#"
|
r#"
|
||||||
@@ -120,3 +123,55 @@ fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression() -> Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let i = 1 > 2 ? 15 : 20;
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
sgt r1 1 2
|
||||||
|
select r2 r1 15 20
|
||||||
|
move r8 r2 #i
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression_assignment() -> Result<()> {
|
||||||
|
let compiled = compile! {
|
||||||
|
debug
|
||||||
|
r#"
|
||||||
|
let i = 0;
|
||||||
|
i = 1 > 2 ? 15 : 20;
|
||||||
|
"#
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
main:
|
||||||
|
move r8 0 #i
|
||||||
|
sgt r1 1 2
|
||||||
|
select r2 r1 15 20
|
||||||
|
move r8 r2 #i
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ fn no_arguments() -> anyhow::Result<()> {
|
|||||||
j main
|
j main
|
||||||
doSomething:
|
doSomething:
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -34,14 +35,17 @@ fn no_arguments() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn let_var_args() -> anyhow::Result<()> {
|
fn let_var_args() -> anyhow::Result<()> {
|
||||||
// !IMPORTANT this needs to be stabilized as it currently incorrectly calculates sp offset at
|
|
||||||
// both ends of the cleanup lifecycle
|
|
||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
fn doSomething(arg1) {};
|
fn mul2(arg1) {
|
||||||
|
return arg1 * 2;
|
||||||
|
};
|
||||||
|
loop {
|
||||||
let arg1 = 123;
|
let arg1 = 123;
|
||||||
let i = doSomething(arg1);
|
let i = mul2(arg1);
|
||||||
|
i = i ** 2;
|
||||||
|
}
|
||||||
"
|
"
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,23 +54,31 @@ fn let_var_args() -> anyhow::Result<()> {
|
|||||||
indoc! {
|
indoc! {
|
||||||
"
|
"
|
||||||
j main
|
j main
|
||||||
doSomething:
|
mul2:
|
||||||
pop r8 #arg1
|
pop r8 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
mul r1 r8 2
|
||||||
|
move r15 r1
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
j ra
|
j ra
|
||||||
main:
|
main:
|
||||||
|
L2:
|
||||||
move r8 123 #arg1
|
move r8 123 #arg1
|
||||||
push r8
|
push r8
|
||||||
push r8
|
push r8
|
||||||
jal doSomething
|
jal mul2
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #i
|
move r9 r15 #i
|
||||||
sub sp sp 1
|
pow r1 r9 2
|
||||||
|
move r9 r1 #i
|
||||||
|
j L2
|
||||||
|
L3:
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -97,7 +109,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
let compiled = compile! {
|
let compiled = compile! {
|
||||||
debug
|
debug
|
||||||
"
|
"
|
||||||
fn doSomething(arg1, arg2) {};
|
fn doSomething(arg1, arg2) {
|
||||||
|
return 5;
|
||||||
|
};
|
||||||
let thisVariableShouldStayInPlace = 123;
|
let thisVariableShouldStayInPlace = 123;
|
||||||
let returnedValue = doSomething(12, 34);
|
let returnedValue = doSomething(12, 34);
|
||||||
"
|
"
|
||||||
@@ -112,6 +126,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
move r15 5 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -126,7 +143,6 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
|||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #returnedValue
|
move r9 r15 #returnedValue
|
||||||
sub sp sp 1
|
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -154,6 +170,7 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -168,7 +185,6 @@ fn mixed_args() -> anyhow::Result<()> {
|
|||||||
get r8 db r0
|
get r8 db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
move r9 r15 #returnValue
|
move r9 r15 #returnValue
|
||||||
sub sp sp 1
|
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -198,6 +214,8 @@ fn with_return_statement() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg1
|
pop r8 #arg1
|
||||||
push ra
|
push ra
|
||||||
move r15 456 #returnValue
|
move r15 456 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
@@ -233,6 +251,7 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
|
|||||||
doSomething:
|
doSomething:
|
||||||
push ra
|
push ra
|
||||||
move r15 -1 #returnValue
|
move r15 -1 #returnValue
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -133,6 +133,8 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
|||||||
getTrue:
|
getTrue:
|
||||||
push ra
|
push ra
|
||||||
move r15 1 #returnValue
|
move r15 1 #returnValue
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
pop r13 #arg4
|
pop r13 #arg4
|
||||||
pop r14 #arg3
|
pop r14 #arg3
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 3
|
sub sp sp 3
|
||||||
@@ -31,6 +32,48 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_early_return() -> anyhow::Result<()> {
|
||||||
|
let compiled = compile!(debug r#"
|
||||||
|
// This is a test function declaration with no body
|
||||||
|
fn doSomething() {
|
||||||
|
if (1 == 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i = 1 + 2;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
doSomething();
|
||||||
|
"#);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
compiled,
|
||||||
|
indoc! {
|
||||||
|
"
|
||||||
|
j main
|
||||||
|
doSomething:
|
||||||
|
push ra
|
||||||
|
seq r1 1 1
|
||||||
|
beq r1 0 L2
|
||||||
|
j L1
|
||||||
|
L2:
|
||||||
|
move r8 3 #i
|
||||||
|
j L1
|
||||||
|
L1:
|
||||||
|
sub r0 sp 1
|
||||||
|
get ra db r0
|
||||||
|
sub sp sp 1
|
||||||
|
j ra
|
||||||
|
main:
|
||||||
|
jal doSomething
|
||||||
|
move r1 r15 #__binary_temp_2
|
||||||
|
"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
||||||
let compiled = compile!(debug r#"
|
let compiled = compile!(debug r#"
|
||||||
@@ -47,6 +90,7 @@ fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
|||||||
pop r8 #arg2
|
pop r8 #arg2
|
||||||
pop r9 #arg1
|
pop r9 #arg1
|
||||||
push ra
|
push ra
|
||||||
|
L1:
|
||||||
sub r0 sp 1
|
sub r0 sp 1
|
||||||
get ra db r0
|
get ra db r0
|
||||||
sub sp sp 1
|
sub sp sp 1
|
||||||
|
|||||||
@@ -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
@@ -94,19 +94,21 @@ impl<'a, 'b> VariableScope<'a, 'b> {
|
|||||||
pub const RETURN_REGISTER: u8 = 15;
|
pub const RETURN_REGISTER: u8 = 15;
|
||||||
pub const TEMP_STACK_REGISTER: u8 = 0;
|
pub const TEMP_STACK_REGISTER: u8 = 0;
|
||||||
|
|
||||||
pub fn registers(&self) -> impl Iterator<Item = &u8> {
|
pub fn registers(&self) -> Vec<u8> {
|
||||||
self.var_lookup_table
|
let mut used = Vec::new();
|
||||||
.values()
|
|
||||||
.filter(|val| {
|
for r in TEMP {
|
||||||
matches!(
|
if !self.temporary_vars.contains(&r) {
|
||||||
val,
|
used.push(r);
|
||||||
VariableLocation::Temporary(_) | VariableLocation::Persistant(_)
|
}
|
||||||
)
|
}
|
||||||
})
|
|
||||||
.map(|loc| match loc {
|
for r in PERSIST {
|
||||||
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => reg,
|
if !self.persistant_vars.contains(&r) {
|
||||||
_ => unreachable!(),
|
used.push(r);
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
used
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {
|
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {
|
||||||
|
|||||||
@@ -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 }
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -293,12 +293,12 @@ impl<'a> Parser<'a> {
|
|||||||
// Handle Infix operators (Binary, Logical, Assignment)
|
// Handle Infix operators (Binary, Logical, Assignment)
|
||||||
if self_matches_peek!(
|
if self_matches_peek!(
|
||||||
self,
|
self,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||||
) {
|
) {
|
||||||
return Ok(Some(self.infix(lhs)?));
|
return Ok(Some(self.infix(lhs)?));
|
||||||
} else if self_matches_current!(
|
} else if self_matches_current!(
|
||||||
self,
|
self,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||||
) {
|
) {
|
||||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||||
return Ok(Some(self.infix(lhs)?));
|
return Ok(Some(self.infix(lhs)?));
|
||||||
@@ -766,9 +766,11 @@ impl<'a> Parser<'a> {
|
|||||||
Expression::Binary(_)
|
Expression::Binary(_)
|
||||||
| Expression::Logical(_)
|
| Expression::Logical(_)
|
||||||
| Expression::Invocation(_)
|
| Expression::Invocation(_)
|
||||||
|
| Expression::Syscall(_)
|
||||||
| Expression::Priority(_)
|
| Expression::Priority(_)
|
||||||
| Expression::Literal(_)
|
| Expression::Literal(_)
|
||||||
| Expression::Variable(_)
|
| Expression::Variable(_)
|
||||||
|
| Expression::Ternary(_)
|
||||||
| Expression::Negation(_)
|
| Expression::Negation(_)
|
||||||
| Expression::MemberAccess(_)
|
| Expression::MemberAccess(_)
|
||||||
| Expression::MethodCall(_) => {}
|
| Expression::MethodCall(_) => {}
|
||||||
@@ -788,7 +790,7 @@ impl<'a> Parser<'a> {
|
|||||||
// Include Assign in the operator loop
|
// Include Assign in the operator loop
|
||||||
while token_matches!(
|
while token_matches!(
|
||||||
temp_token,
|
temp_token,
|
||||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question | Symbol::Colon)
|
||||||
) {
|
) {
|
||||||
let operator = match temp_token.token_type {
|
let operator = match temp_token.token_type {
|
||||||
TokenType::Symbol(s) => s,
|
TokenType::Symbol(s) => s,
|
||||||
@@ -1019,7 +1021,52 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
||||||
|
|
||||||
// --- PRECEDENCE LEVEL 8: Assignment (=) ---
|
// -- PRECEDENCE LEVEL 8: Ternary (x ? 1 : 2)
|
||||||
|
for i in (0..operators.len()).rev() {
|
||||||
|
if matches!(operators[i], Symbol::Question) {
|
||||||
|
// Ensure next operator is a colon
|
||||||
|
if i + 1 >= operators.len() || !matches!(operators[i + 1], Symbol::Colon) {
|
||||||
|
return Err(Error::InvalidSyntax(
|
||||||
|
self.current_span(),
|
||||||
|
"Ternary operator '?' missing matching ':'".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let false_branch = expressions.remove(i + 2);
|
||||||
|
let true_branch = expressions.remove(i + 1);
|
||||||
|
let condition = expressions.remove(i);
|
||||||
|
|
||||||
|
let span = Span {
|
||||||
|
start_line: condition.span.start_line,
|
||||||
|
end_line: false_branch.span.end_line,
|
||||||
|
start_col: condition.span.start_col,
|
||||||
|
end_col: false_branch.span.end_col,
|
||||||
|
};
|
||||||
|
|
||||||
|
let ternary_node = Spanned {
|
||||||
|
span,
|
||||||
|
node: TernaryExpression {
|
||||||
|
condition: Box::new(condition),
|
||||||
|
true_value: Box::new(true_branch),
|
||||||
|
false_value: Box::new(false_branch),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expressions.insert(
|
||||||
|
i,
|
||||||
|
Spanned {
|
||||||
|
node: Expression::Ternary(ternary_node),
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Remove the `?` and the `:` from the operators list
|
||||||
|
operators.remove(i);
|
||||||
|
operators.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- PRECEDENCE LEVEL 9: Assignment (=) ---
|
||||||
// Assignment is Right Associative: a = b = c => a = (b = c)
|
// Assignment is Right Associative: a = b = c => a = (b = c)
|
||||||
// We iterate Right to Left
|
// We iterate Right to Left
|
||||||
for (i, operator) in operators.iter().enumerate().rev() {
|
for (i, operator) in operators.iter().enumerate().rev() {
|
||||||
@@ -1179,18 +1226,34 @@ impl<'a> Parser<'a> {
|
|||||||
// Need to capture return span
|
// Need to capture return span
|
||||||
let ret_start_span = Self::token_to_span(¤t_token);
|
let ret_start_span = Self::token_to_span(¤t_token);
|
||||||
self.assign_next()?;
|
self.assign_next()?;
|
||||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
|
||||||
|
let expr = if token_matches!(
|
||||||
|
self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?,
|
||||||
|
TokenType::Symbol(Symbol::Semicolon)
|
||||||
|
) {
|
||||||
|
// rewind 1 token so we can check for the semicolon at the bottom of this function.
|
||||||
|
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self.expression()?.ok_or(Error::UnexpectedEOF)?)
|
||||||
|
};
|
||||||
|
|
||||||
let ret_span = Span {
|
let ret_span = Span {
|
||||||
start_line: ret_start_span.start_line,
|
start_line: ret_start_span.start_line,
|
||||||
start_col: ret_start_span.start_col,
|
start_col: ret_start_span.start_col,
|
||||||
end_line: expression.span.end_line,
|
end_line: expr
|
||||||
end_col: expression.span.end_col,
|
.as_ref()
|
||||||
|
.map(|e| e.span.end_line)
|
||||||
|
.unwrap_or(ret_start_span.end_line),
|
||||||
|
end_col: expr
|
||||||
|
.as_ref()
|
||||||
|
.map(|e| e.span.end_col)
|
||||||
|
.unwrap_or(ret_start_span.end_col),
|
||||||
};
|
};
|
||||||
|
|
||||||
let return_expr = Spanned {
|
let return_expr = Spanned {
|
||||||
span: ret_span,
|
span: ret_span,
|
||||||
node: Expression::Return(boxed!(expression)),
|
node: Expression::Return(expr.map(Box::new)),
|
||||||
};
|
};
|
||||||
expressions.push(return_expr);
|
expressions.push(return_expr);
|
||||||
|
|
||||||
|
|||||||
@@ -160,3 +160,37 @@ fn test_negative_literal_const() -> Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ternary_expression() -> Result<()> {
|
||||||
|
let expr = parser!(r#"let i = x ? 1 : 2;"#).parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = (x ? 1 : 2))", expr.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||||
|
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_operator_prescedence_with_ternary() -> Result<()> {
|
||||||
|
let expr = parser!("let x = x ? 1 : 3 * 2;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let x = (x ? 1 : (3 * 2)))", expr.to_string());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_nested_ternary_right_associativity() -> Result<()> {
|
||||||
|
let expr = parser!("let i = a ? b : c ? d : e;").parse()?.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
@@ -277,6 +278,23 @@ pub struct WhileExpression<'a> {
|
|||||||
pub body: BlockExpression<'a>,
|
pub body: BlockExpression<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct TernaryExpression<'a> {
|
||||||
|
pub condition: Box<Spanned<Expression<'a>>>,
|
||||||
|
pub true_value: Box<Spanned<Expression<'a>>>,
|
||||||
|
pub false_value: Box<Spanned<Expression<'a>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for TernaryExpression<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"({} ? {} : {})",
|
||||||
|
self.condition, self.true_value, self.false_value
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "(while {} {})", self.condition, self.body)
|
write!(f, "(while {} {})", self.condition, self.body)
|
||||||
@@ -364,8 +382,9 @@ pub enum Expression<'a> {
|
|||||||
MethodCall(Spanned<MethodCallExpression<'a>>),
|
MethodCall(Spanned<MethodCallExpression<'a>>),
|
||||||
Negation(Box<Spanned<Expression<'a>>>),
|
Negation(Box<Spanned<Expression<'a>>>),
|
||||||
Priority(Box<Spanned<Expression<'a>>>),
|
Priority(Box<Spanned<Expression<'a>>>),
|
||||||
Return(Box<Spanned<Expression<'a>>>),
|
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||||
Syscall(Spanned<SysCall<'a>>),
|
Syscall(Spanned<SysCall<'a>>),
|
||||||
|
Ternary(Spanned<TernaryExpression<'a>>),
|
||||||
Variable(Spanned<Cow<'a, str>>),
|
Variable(Spanned<Cow<'a, str>>),
|
||||||
While(Spanned<WhileExpression<'a>>),
|
While(Spanned<WhileExpression<'a>>),
|
||||||
}
|
}
|
||||||
@@ -391,8 +410,17 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
|||||||
Expression::MethodCall(e) => write!(f, "{}", e),
|
Expression::MethodCall(e) => write!(f, "{}", e),
|
||||||
Expression::Negation(e) => write!(f, "(-{})", e),
|
Expression::Negation(e) => write!(f, "(-{})", e),
|
||||||
Expression::Priority(e) => write!(f, "({})", e),
|
Expression::Priority(e) => write!(f, "({})", e),
|
||||||
Expression::Return(e) => write!(f, "(return {})", e),
|
Expression::Return(e) => write!(
|
||||||
|
f,
|
||||||
|
"(return {})",
|
||||||
|
if let Some(e) = e {
|
||||||
|
e.to_string()
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
Expression::Syscall(e) => write!(f, "{}", e),
|
Expression::Syscall(e) => write!(f, "{}", e),
|
||||||
|
Expression::Ternary(e) => write!(f, "{}", e),
|
||||||
Expression::Variable(id) => write!(f, "{}", id),
|
Expression::Variable(id) => write!(f, "{}", id),
|
||||||
Expression::While(e) => write!(f, "{}", e),
|
Expression::While(e) => write!(f, "{}", e),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,6 +225,7 @@ pub enum TokenType<'a> {
|
|||||||
#[token(".", symbol!(Dot))]
|
#[token(".", symbol!(Dot))]
|
||||||
#[token("^", symbol!(Caret))]
|
#[token("^", symbol!(Caret))]
|
||||||
#[token("%", symbol!(Percent))]
|
#[token("%", symbol!(Percent))]
|
||||||
|
#[token("?", symbol!(Question))]
|
||||||
#[token("==", symbol!(Equal))]
|
#[token("==", symbol!(Equal))]
|
||||||
#[token("!=", symbol!(NotEqual))]
|
#[token("!=", symbol!(NotEqual))]
|
||||||
#[token("&&", symbol!(LogicalAnd))]
|
#[token("&&", symbol!(LogicalAnd))]
|
||||||
@@ -535,6 +536,8 @@ pub enum Symbol {
|
|||||||
Caret,
|
Caret,
|
||||||
/// Represents the `%` symbol
|
/// Represents the `%` symbol
|
||||||
Percent,
|
Percent,
|
||||||
|
/// Represents the `?` symbol
|
||||||
|
Question,
|
||||||
|
|
||||||
// Double Character Symbols
|
// Double Character Symbols
|
||||||
/// Represents the `==` symbol
|
/// Represents the `==` symbol
|
||||||
@@ -601,6 +604,7 @@ impl std::fmt::Display for Symbol {
|
|||||||
Self::Asterisk => write!(f, "*"),
|
Self::Asterisk => write!(f, "*"),
|
||||||
Self::Slash => write!(f, "/"),
|
Self::Slash => write!(f, "/"),
|
||||||
Self::LessThan => write!(f, "<"),
|
Self::LessThan => write!(f, "<"),
|
||||||
|
Self::Question => write!(f, "?"),
|
||||||
Self::LessThanOrEqual => write!(f, "<="),
|
Self::LessThanOrEqual => write!(f, "<="),
|
||||||
Self::GreaterThan => write!(f, ">"),
|
Self::GreaterThan => write!(f, ">"),
|
||||||
Self::GreaterThanOrEqual => write!(f, ">="),
|
Self::GreaterThanOrEqual => write!(f, ">="),
|
||||||
|
|||||||
@@ -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