9 Commits
0.2.2 ... 0.2.4

24 changed files with 1089 additions and 462 deletions

View File

@@ -1,5 +1,17 @@
# 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

View File

@@ -2,7 +2,7 @@
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Slang</Name>
<Author>JoeDiertay</Author>
<Version>0.2.2</Version>
<Version>0.2.4</Version>
<Description>
[h1]Slang: High-Level Programming for Stationeers[/h1]

View File

@@ -113,6 +113,34 @@ public static unsafe class SlangExtensions
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)
{
switch (kind)

View File

@@ -71,18 +71,6 @@ public unsafe struct Vec_uint8_t {
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)]
public unsafe struct FfiRange_t {
public UInt32 start_col;
@@ -94,6 +82,44 @@ public unsafe struct FfiRange_t {
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)]
public unsafe struct FfiDiagnostic_t {
public Vec_uint8_t message;
@@ -146,6 +172,12 @@ public unsafe partial class Ffi {
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 {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
void free_ffi_diagnostic_vec (

View File

@@ -15,11 +15,59 @@ public static class GlobalCode
// so that save file data is smaller
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()
{
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)
{
if (!codeDict.ContainsKey(reference))

View File

@@ -10,10 +10,23 @@ using StationeersIC10Editor;
public struct Range
{
public uint StartCol;
public uint EndCol;
public uint StartLine;
public uint EndLine;
public uint StartCol = 0;
public uint EndCol = 0;
public uint StartLine = 0;
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
@@ -23,6 +36,17 @@ public struct Diagnostic
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
{
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())
{
compiledString = String.Empty;
sourceMapEntries = new();
return false;
}
@@ -95,19 +124,16 @@ public static class Marshal
};
var result = Ffi.compile_from_string(input);
try
{
if ((ulong)result.len < 1)
{
compiledString = String.Empty;
return false;
}
compiledString = result.AsString();
sourceMapEntries = result.source_map.ToList();
compiledString = result.output_code.AsString();
return true;
}
finally
{
result.Drop();
Ffi.free_ffi_compilation_result(result);
}
}
}

View File

@@ -1,12 +1,34 @@
namespace Slang;
using System;
using System.Runtime.CompilerServices;
using Assets.Scripts.Objects;
using Assets.Scripts.Objects.Electrical;
using Assets.Scripts.Objects.Motherboards;
using Assets.Scripts.UI;
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]
public static class SlangPatches
{
@@ -14,6 +36,9 @@ public static class SlangPatches
private static AsciiString? _motherboardCachedCode;
private static Guid? _currentlyEditingGuid;
private static ConditionalWeakTable<ProgrammableChip, LineErrorData> _errorReferenceTable =
new();
[HarmonyPatch(
typeof(ProgrammableChipMotherboard),
nameof(ProgrammableChipMotherboard.InputFinished)
@@ -26,7 +51,7 @@ public static class SlangPatches
// guard to ensure we have valid IC10 before continuing
if (
!SlangPlugin.IsSlangSource(ref result)
|| !Marshal.CompileFromString(result, out string compiled)
|| !Marshal.CompileFromString(result, out var compiled, out var sourceMap)
|| string.IsNullOrEmpty(compiled)
)
{
@@ -37,6 +62,7 @@ public static class SlangPatches
// Ensure we cache this compiled code for later retreival.
GlobalCode.SetSource(thisRef, result);
GlobalCode.SetSourceMap(thisRef, sourceMap);
_currentlyEditingGuid = null;
@@ -140,6 +166,100 @@ public static class SlangPatches
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(
typeof(ProgrammableChipMotherboard),
nameof(ProgrammableChipMotherboard.SerializeSave)

View File

@@ -41,7 +41,7 @@ namespace Slang
{
public const string PluginGuid = "com.biddydev.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);

View File

@@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<AssemblyName>StationeersSlang</AssemblyName>
<Description>Slang Compiler Bridge</Description>
<Version>0.2.2</Version>
<Version>0.2.4</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

View File

@@ -571,6 +571,7 @@ dependencies = [
"helpers",
"lsp-types",
"pretty_assertions",
"safer-ffi",
"thiserror",
"tokenizer",
]
@@ -909,7 +910,7 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
[[package]]
name = "slang"
version = "0.2.2"
version = "0.2.4"
dependencies = [
"anyhow",
"clap",

View File

@@ -1,6 +1,6 @@
[package]
name = "slang"
version = "0.2.2"
version = "0.2.4"
edition = "2021"
[workspace]

View File

@@ -3,4 +3,4 @@ mod test;
mod v1;
mod variable_manager;
pub use v1::{Compiler, CompilerConfig, Error};
pub use v1::{CompilationResult, Compiler, CompilerConfig, Error};

View File

@@ -52,6 +52,8 @@ fn nested_binary_expressions() -> Result<()> {
add r1 r10 r9
mul r2 r1 r8
move r15 r2
j L1
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1

View File

@@ -17,6 +17,7 @@ fn no_arguments() -> anyhow::Result<()> {
j main
doSomething:
push ra
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1
@@ -34,14 +35,17 @@ fn no_arguments() -> anyhow::Result<()> {
#[test]
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! {
debug
"
fn doSomething(arg1) {};
let arg1 = 123;
let i = doSomething(arg1);
fn mul2(arg1) {
return arg1 * 2;
};
loop {
let arg1 = 123;
let i = mul2(arg1);
i = i ** 2;
}
"
};
@@ -50,23 +54,31 @@ fn let_var_args() -> anyhow::Result<()> {
indoc! {
"
j main
doSomething:
mul2:
pop r8 #arg1
push ra
mul r1 r8 2
move r15 r1
j L1
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1
j ra
main:
L2:
move r8 123 #arg1
push r8
push r8
jal doSomething
jal mul2
sub r0 sp 1
get r8 db r0
sub sp sp 1
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! {
debug
"
fn doSomething(arg1, arg2) {};
fn doSomething(arg1, arg2) {
return 5;
};
let thisVariableShouldStayInPlace = 123;
let returnedValue = doSomething(12, 34);
"
@@ -112,6 +126,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
pop r8 #arg2
pop r9 #arg1
push ra
move r15 5 #returnValue
j L1
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1
@@ -126,7 +143,6 @@ fn inline_literal_args() -> anyhow::Result<()> {
get r8 db r0
sub sp sp 1
move r9 r15 #returnedValue
sub sp sp 1
"
}
);
@@ -154,6 +170,7 @@ fn mixed_args() -> anyhow::Result<()> {
pop r8 #arg2
pop r9 #arg1
push ra
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1
@@ -168,7 +185,6 @@ fn mixed_args() -> anyhow::Result<()> {
get r8 db r0
sub sp sp 1
move r9 r15 #returnValue
sub sp sp 1
"
}
);
@@ -198,6 +214,8 @@ fn with_return_statement() -> anyhow::Result<()> {
pop r8 #arg1
push ra
move r15 456 #returnValue
j L1
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1
@@ -233,6 +251,7 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
doSomething:
push ra
move r15 -1 #returnValue
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1

View File

@@ -120,7 +120,7 @@ fn test_boolean_return() -> anyhow::Result<()> {
fn getTrue() {
return true;
};
let val = getTrue();
"
};
@@ -133,6 +133,8 @@ fn test_boolean_return() -> anyhow::Result<()> {
getTrue:
push ra
move r15 1 #returnValue
j L1
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1

View File

@@ -21,6 +21,7 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
pop r13 #arg4
pop r14 #arg3
push ra
L1:
sub r0 sp 1
get ra db r0
sub sp sp 3
@@ -31,6 +32,48 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
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]
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
let compiled = compile!(debug r#"
@@ -47,6 +90,7 @@ fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
pop r8 #arg2
pop r9 #arg1
push ra
L1:
sub r0 sp 1
get ra db r0
sub sp sp 1

View File

@@ -26,7 +26,7 @@ macro_rules! compile {
&mut writer,
Some(crate::CompilerConfig { debug: true }),
);
compiler.compile()
compiler.compile().errors
}};
(debug $source:expr) => {{

File diff suppressed because it is too large Load Diff

View File

@@ -94,19 +94,21 @@ impl<'a, 'b> VariableScope<'a, 'b> {
pub const RETURN_REGISTER: u8 = 15;
pub const TEMP_STACK_REGISTER: u8 = 0;
pub fn registers(&self) -> impl Iterator<Item = &u8> {
self.var_lookup_table
.values()
.filter(|val| {
matches!(
val,
VariableLocation::Temporary(_) | VariableLocation::Persistant(_)
)
})
.map(|loc| match loc {
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => reg,
_ => unreachable!(),
})
pub fn registers(&self) -> Vec<u8> {
let mut used = Vec::new();
for r in TEMP {
if !self.temporary_vars.contains(&r) {
used.push(r);
}
}
for r in PERSIST {
if !self.persistant_vars.contains(&r) {
used.push(r);
}
}
used
}
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {

View File

@@ -7,6 +7,7 @@ edition = "2024"
tokenizer = { path = "../tokenizer" }
helpers = { path = "../helpers" }
lsp-types = { workspace = true }
safer-ffi = { workspace = true }
thiserror = { workspace = true }

View File

@@ -766,6 +766,7 @@ impl<'a> Parser<'a> {
Expression::Binary(_)
| Expression::Logical(_)
| Expression::Invocation(_)
| Expression::Syscall(_)
| Expression::Priority(_)
| Expression::Literal(_)
| Expression::Variable(_)
@@ -1225,18 +1226,34 @@ impl<'a> Parser<'a> {
// Need to capture return span
let ret_start_span = Self::token_to_span(&current_token);
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 {
start_line: ret_start_span.start_line,
start_col: ret_start_span.start_col,
end_line: expression.span.end_line,
end_col: expression.span.end_col,
end_line: expr
.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 {
span: ret_span,
node: Expression::Return(boxed!(expression)),
node: Expression::Return(expr.map(Box::new)),
};
expressions.push(return_expr);

View File

@@ -1,5 +1,6 @@
use super::sys_call::SysCall;
use crate::sys_call;
use safer_ffi::prelude::*;
use std::{borrow::Cow, ops::Deref};
use tokenizer::token::Number;
@@ -381,7 +382,7 @@ pub enum Expression<'a> {
MethodCall(Spanned<MethodCallExpression<'a>>),
Negation(Box<Spanned<Expression<'a>>>),
Priority(Box<Spanned<Expression<'a>>>),
Return(Box<Spanned<Expression<'a>>>),
Return(Option<Box<Spanned<Expression<'a>>>>),
Syscall(Spanned<SysCall<'a>>),
Ternary(Spanned<TernaryExpression<'a>>),
Variable(Spanned<Cow<'a, str>>),
@@ -409,7 +410,15 @@ impl<'a> std::fmt::Display for Expression<'a> {
Expression::MethodCall(e) => write!(f, "{}", e),
Expression::Negation(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::Ternary(e) => write!(f, "{}", e),
Expression::Variable(id) => write!(f, "{}", id),

View File

@@ -1,6 +1,6 @@
use compiler::Compiler;
use compiler::{CompilationResult, Compiler};
use helpers::Documentation;
use parser::{sys_call::SysCall, Parser};
use parser::{sys_call::SysCall, tree_node::Span, Parser};
use safer_ffi::prelude::*;
use std::io::BufWriter;
use tokenizer::{
@@ -8,6 +8,20 @@ use 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]
#[repr(C)]
pub struct FfiToken {
@@ -34,6 +48,17 @@ pub struct FfiDocumentedItem {
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 {
fn from(value: lsp_types::Range) -> 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]
pub fn free_ffi_token_vec(v: safer_ffi::Vec<FfiToken>) {
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
/// from the GC from a `GetBytes()` call on a string in C#.
#[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 input = String::from_utf16_lossy(input.as_slice());
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 compiler = Compiler::new(parser, &mut writer, None);
if !compiler.compile().is_empty() {
return safer_ffi::String::EMPTY;
let res = compiler.compile();
if !res.errors.is_empty() {
return (safer_ffi::String::EMPTY, res.source_map);
}
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
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]
@@ -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 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());

View File

@@ -1,7 +1,7 @@
#![allow(clippy::result_large_err)]
use clap::Parser;
use compiler::Compiler;
use compiler::{CompilationResult, Compiler};
use parser::Parser as ASTParser;
use std::{
fs::File,
@@ -90,7 +90,7 @@ fn run_logic<'a>() -> Result<(), Error<'a>> {
let compiler = Compiler::new(parser, &mut writer, None);
let errors = compiler.compile();
let CompilationResult { errors, .. } = compiler.compile();
if !errors.is_empty() {
let mut std_error = stderr();