From 5db31d087df9d0b74e3aa503f5d72836c7e9cd95 Mon Sep 17 00:00:00 2001 From: Devin Bidwell Date: Sun, 30 Nov 2025 16:35:14 -0700 Subject: [PATCH] fixed tests. --- csharp_mod/FfiGlue.cs | 193 +++++++++++----------- rust_compiler/libs/parser/src/test/mod.rs | 4 +- 2 files changed, 101 insertions(+), 96 deletions(-) diff --git a/csharp_mod/FfiGlue.cs b/csharp_mod/FfiGlue.cs index 400805d..551d531 100644 --- a/csharp_mod/FfiGlue.cs +++ b/csharp_mod/FfiGlue.cs @@ -15,122 +15,127 @@ #pragma warning disable SA1500, SA1505, SA1507, #pragma warning disable SA1600, SA1601, SA1604, SA1605, SA1611, SA1615, SA1649, -namespace Slang { -using System; -using System.Runtime.InteropServices; +namespace Slang +{ + using System; + using System.Runtime.InteropServices; -public unsafe partial class Ffi { + public unsafe partial class Ffi + { #if IOS - private const string RustLib = "slang.framework/slang"; + private const string RustLib = "slang.framework/slang"; #else - public const string RustLib = "slang_compiler.dll"; + public const string RustLib = "slang_compiler.dll"; #endif -} - -/// -/// &'lt [T] but with a guaranteed #[repr(C)] layout. -/// -/// # C layout (for some given type T) -/// -/// ```c -/// typedef struct { -/// // Cannot be NULL -/// T * ptr; -/// size_t len; -/// } slice_T; -/// ``` -/// -/// # Nullable pointer? -/// -/// If you want to support the above typedef, but where the ptr field is -/// allowed to be NULL (with the contents of len then being undefined) -/// use the Option< slice_ptr<_> > type. -/// -[StructLayout(LayoutKind.Sequential, Size = 16)] -public unsafe struct slice_ref_uint16_t { - /// - /// Pointer to the first element (if any). - /// - public UInt16 /*const*/ * ptr; + } /// - /// Element count + /// &'lt [T] but with a guaranteed #[repr(C)] layout. + /// + /// # C layout (for some given type T) + /// + /// ```c + /// typedef struct { + /// // Cannot be NULL + /// T * ptr; + /// size_t len; + /// } slice_T; + /// ``` + /// + /// # Nullable pointer? + /// + /// If you want to support the above typedef, but where the ptr field is + /// allowed to be NULL (with the contents of len then being undefined) + /// use the Option< slice_ptr<_> > type. /// - public UIntPtr len; -} + [StructLayout(LayoutKind.Sequential, Size = 16)] + public unsafe struct slice_ref_uint16_t + { + /// + /// Pointer to the first element (if any). + /// + public UInt16 /*const*/ + * ptr; -/// -/// Same as [Vec][rust::Vec], but with guaranteed #[repr(C)] layout -/// -[StructLayout(LayoutKind.Sequential, Size = 24)] -public unsafe struct Vec_uint8_t { - public byte * ptr; + /// + /// Element count + /// + public UIntPtr len; + } - public UIntPtr len; - - public UIntPtr cap; -} - -public unsafe partial class Ffi { /// - /// 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 GetBytes() call on a string in C#. + /// Same as [Vec][rust::Vec], but with guaranteed #[repr(C)] layout /// - [DllImport(RustLib, ExactSpelling = true)] public static unsafe extern - Vec_uint8_t compile_from_string ( - slice_ref_uint16_t input); -} + [StructLayout(LayoutKind.Sequential, Size = 24)] + public unsafe struct Vec_uint8_t + { + public byte* ptr; -[StructLayout(LayoutKind.Sequential, Size = 64)] -public unsafe struct FfiToken_t { - public Vec_uint8_t tooltip; + public UIntPtr len; - public Vec_uint8_t error; + public UIntPtr cap; + } - public Int32 column; + public unsafe partial class Ffi + { + /// + /// 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 GetBytes() call on a string in C#. + /// + [DllImport(RustLib, ExactSpelling = true)] + public static extern unsafe Vec_uint8_t compile_from_string(slice_ref_uint16_t input); + } - public Int32 length; + [StructLayout(LayoutKind.Sequential, Size = 64)] + public unsafe struct FfiToken_t + { + public Vec_uint8_t tooltip; - public UInt32 token_kind; -} + public Vec_uint8_t error; -/// -/// Same as [Vec][rust::Vec], but with guaranteed #[repr(C)] layout -/// -[StructLayout(LayoutKind.Sequential, Size = 24)] -public unsafe struct Vec_FfiToken_t { - public FfiToken_t * ptr; + public Int32 column; - public UIntPtr len; + public Int32 length; - public UIntPtr cap; -} + public UInt32 token_kind; + } -public unsafe partial class Ffi { - [DllImport(RustLib, ExactSpelling = true)] public static unsafe extern - void free_ffi_token_vec ( - Vec_FfiToken_t v); -} - -public unsafe partial class Ffi { - [DllImport(RustLib, ExactSpelling = true)] public static unsafe extern - void free_string ( - Vec_uint8_t s); -} - -public unsafe partial class Ffi { /// - /// 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 tokenize many times without triggering frame drops - /// from the GC from a GetBytes() call on a string in C#. + /// Same as [Vec][rust::Vec], but with guaranteed #[repr(C)] layout /// - [DllImport(RustLib, ExactSpelling = true)] public static unsafe extern - Vec_FfiToken_t tokenize_line ( - slice_ref_uint16_t input); -} + [StructLayout(LayoutKind.Sequential, Size = 24)] + public unsafe struct Vec_FfiToken_t + { + public FfiToken_t* ptr; + public UIntPtr len; + public UIntPtr cap; + } + + public unsafe partial class Ffi + { + [DllImport(RustLib, ExactSpelling = true)] + public static extern unsafe void free_ffi_token_vec(Vec_FfiToken_t v); + } + + public unsafe partial class Ffi + { + [DllImport(RustLib, ExactSpelling = true)] + public static extern unsafe void free_string(Vec_uint8_t s); + } + + public unsafe partial class Ffi + { + /// + /// 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 tokenize many times without triggering frame drops + /// from the GC from a GetBytes() call on a string in C#. + /// + [DllImport(RustLib, ExactSpelling = true)] + public static extern unsafe Vec_FfiToken_t tokenize_line(slice_ref_uint16_t input); + } } /* Slang */ diff --git a/rust_compiler/libs/parser/src/test/mod.rs b/rust_compiler/libs/parser/src/test/mod.rs index 5822da9..c23869a 100644 --- a/rust_compiler/libs/parser/src/test/mod.rs +++ b/rust_compiler/libs/parser/src/test/mod.rs @@ -92,7 +92,7 @@ fn test_priority_expression() -> Result<()> { let expression = parser.parse()?.unwrap(); - assert_eq!("(let x = (4))", expression.to_string()); + assert_eq!("(let x = 4)", expression.to_string()); Ok(()) } @@ -109,7 +109,7 @@ fn test_binary_expression() -> Result<()> { assert_eq!("(((45 * 2) - (15 / 5)) + (5 ** 2))", expr.to_string()); let expr = parser!("(5 - 2) * 10").parse()?.unwrap(); - assert_eq!("(((5 - 2)) * 10)", expr.to_string()); + assert_eq!("((5 - 2) * 10)", expr.to_string()); Ok(()) }