Skip to content

Commit

Permalink
Update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Emik03 committed Dec 22, 2022
1 parent 3866d5a commit 45492d1
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 136 deletions.
247 changes: 125 additions & 122 deletions CSharp/Source/AST.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#region Emik.MPL

// <copyright file="AST.cs" company="Emik">
// Copyright (c) Emik. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>

#endregion

// ReSharper disable AssignNullToNotNullAttribute
namespace Emik.Rhainterop;

#pragma warning disable SA1300
#pragma warning disable MA0055
#pragma warning disable MA0055, SA1300
/// <summary>Represents a key for a previously computed abstract syntax tree.</summary>
/// <remarks><para>
/// This type implements <see cref="IDisposable"/> to dispose the syntax tree when it is out of use.
Expand All @@ -26,95 +29,13 @@ public sealed class AST : ICloneable,
/// <param name="id">The <see cref="Id"/> to store.</param>
internal AST(ulong id) => Id = id;

/// <summary>Finalizes an instance of the <see cref="AST"/> class.</summary>
~AST() => Dispose();

/// <summary>Gets the key value used to get the <see cref="AST"/> that represents this in Rust.</summary>
[CLSCompliant(false), Pure]
public ulong Id { get; }

/// <summary>Implicitly calls <see cref="Id"/>.</summary>
/// <param name="ast">The <see cref="AST"/> to grab <see cref="Id"/>.</param>
/// <returns>The value <see cref="Id"/>, or 0 if <see langword="null"/>.</returns>
[CLSCompliant(false), Pure]
public static implicit operator ulong(AST? ast) => ast?.Id ?? 0;

/// <summary>Determines if two <see cref="AST"/> instances are equal.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>They are both <see langword="null"/>, or share the same <see cref="Id"/>.</returns>
[Pure]
public static bool operator ==(AST? x, AST? y) => x is null ? y is null : y is not null && x.Id == y.Id;

/// <summary>Determines if two <see cref="AST"/> instances are unequal.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>One of them is <see langword="null"/>, or have different values for <see cref="Id"/>.</returns>
[Pure]
public static bool operator !=(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is greater than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>The parameter <paramref name="x"/> has a greater <see cref="Id"/> than <paramref name="y"/>.</returns>
[Pure]
public static bool operator >(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is greater or equal than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>
/// The parameter <paramref name="x"/> has a greater or equal <see cref="Id"/> than <paramref name="y"/>.
/// </returns>
[Pure]
public static bool operator >=(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is less than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>The parameter <paramref name="x"/> has a lesser <see cref="Id"/> than <paramref name="y"/>.</returns>
[Pure]
public static bool operator <(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is less or equal than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>
/// The parameter <paramref name="x"/> has a lesser or equal <see cref="Id"/> than <paramref name="y"/>.
/// </returns>
[Pure]
public static bool operator <=(AST? x, AST? y) => !(x == y);

/// <inheritdoc />
public void Dispose()
{
GC.SuppressFinalize(this);
Trace.WriteLineIf(Id is 0 || !drop(Id), "Failed to drop AST.");
}

/// <inheritdoc />
[Pure]
public override bool Equals(object? obj) => Equals(obj as AST);

/// <inheritdoc />
[Pure]
public new bool Equals(object? x, object? y) => Equals(x as AST, y as AST);

/// <inheritdoc />
[Pure]
public bool Equals(AST? other) => other is not null && Id == other.Id;

/// <inheritdoc />
[Pure]
public bool Equals(AST? x, AST? y) => x == y;

/// <inheritdoc />
[Pure]
public int Compare(object? x, object? y) => Compare(x as AST, y as AST);

/// <inheritdoc />
[Pure]
public int Compare(AST? x, AST? y) => x?.CompareTo(y) ?? int.MinValue;
public object Clone() => new AST(Id);

/// <inheritdoc />
[Pure]
Expand All @@ -126,45 +47,11 @@ public void Dispose()

/// <inheritdoc />
[Pure]
public override int GetHashCode() => Id.GetHashCode();

/// <inheritdoc />
[Pure]
public int GetHashCode(object? obj) => GetHashCode(obj as AST);

/// <inheritdoc />
[Pure]
public int GetHashCode(AST? obj) => obj?.GetHashCode() ?? 0;

/// <inheritdoc />
[Pure]
public override string ToString() => $"{nameof(AST)} {nameof(Id)} = {Id}";
public int Compare(object? x, object? y) => Compare(x as AST, y as AST);

/// <inheritdoc />
[Pure]
public object Clone() => new AST(Id);

/// <summary><c>eval</c> Function.</summary>
/// <remarks><para>Or "How to Shoot Yourself in the Foot even Easier".</para></remarks>
/// <typeparam name="T">
/// The type of the resulting type, <see cref="object"/> can be used much like <c>rhai::Dynamic</c> in Rust.
/// </typeparam>
/// <param name="length">The size of the internally allocated buffer.</param>
/// <returns>The resulting type from the expression given, or a runtime error from Rhai.</returns>
[MustUseReturnValue]

// ReSharper disable once LambdaShouldNotCaptureContext
public Result<T, Exception> Eval<T>(int length = Span.Stackalloc) => Span.Allocate(length, Eval<T>);

/// <summary><c>eval</c> Function.</summary>
/// <remarks><para>Or "How to Shoot Yourself in the Foot even Easier".</para></remarks>
/// <typeparam name="T">
/// The type of the resulting type, <see cref="object"/> can be used much like <c>rhai::Dynamic</c> in Rust.
/// </typeparam>
/// <param name="buffer">The buffer to mutate.</param>
/// <returns>The resulting type from the expression given, or a runtime error from Rhai.</returns>
[MustUseReturnValue]
public Result<T, Exception> Eval<T>(Span<byte> buffer) => EvalInner<T>(buffer);
public int Compare(AST? x, AST? y) => x?.CompareTo(y) ?? int.MinValue;

/// <inheritdoc />
[Pure]
Expand Down Expand Up @@ -235,6 +122,122 @@ object IConvertible.ToType(Type? conversionType, IFormatProvider? provider) =>
[Pure]
TypeCode IConvertible.GetTypeCode() => Id.GetTypeCode();

/// <inheritdoc />
public void Dispose()
{
GC.SuppressFinalize(this);
Trace.WriteLineIf(Id is 0 || !drop(Id), "Failed to drop AST.");
}

/// <inheritdoc />
[Pure]
public new bool Equals(object? x, object? y) => Equals(x as AST, y as AST);

/// <inheritdoc />
[Pure]
public int GetHashCode(object? obj) => GetHashCode(obj as AST);

/// <inheritdoc />
[Pure]
public bool Equals(AST? x, AST? y) => x == y;

/// <inheritdoc />
[Pure]
public int GetHashCode(AST? obj) => obj?.GetHashCode() ?? 0;

/// <inheritdoc />
[Pure]
public bool Equals(AST? other) => other is not null && Id == other.Id;

/// <summary>Finalizes an instance of the <see cref="AST"/> class.</summary>
~AST() => Dispose();

/// <summary>Implicitly calls <see cref="Id"/>.</summary>
/// <param name="ast">The <see cref="AST"/> to grab <see cref="Id"/>.</param>
/// <returns>The value <see cref="Id"/>, or 0 if <see langword="null"/>.</returns>
[CLSCompliant(false), Pure]
public static implicit operator ulong(AST? ast) => ast?.Id ?? 0;

/// <summary>Determines if two <see cref="AST"/> instances are equal.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>They are both <see langword="null"/>, or share the same <see cref="Id"/>.</returns>
[Pure]
public static bool operator ==(AST? x, AST? y) => x is null ? y is null : y is not null && x.Id == y.Id;

/// <summary>Determines if two <see cref="AST"/> instances are unequal.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>One of them is <see langword="null"/>, or have different values for <see cref="Id"/>.</returns>
[Pure]
public static bool operator !=(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is greater than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>The parameter <paramref name="x"/> has a greater <see cref="Id"/> than <paramref name="y"/>.</returns>
[Pure]
public static bool operator >(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is greater or equal than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>
/// The parameter <paramref name="x"/> has a greater or equal <see cref="Id"/> than <paramref name="y"/>.
/// </returns>
[Pure]
public static bool operator >=(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is less than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>The parameter <paramref name="x"/> has a lesser <see cref="Id"/> than <paramref name="y"/>.</returns>
[Pure]
public static bool operator <(AST? x, AST? y) => !(x == y);

/// <summary>Determines if one <see cref="AST"/> is less or equal than another.</summary>
/// <param name="x">The left-hand side.</param>
/// <param name="y">The right-hand side.</param>
/// <returns>
/// The parameter <paramref name="x"/> has a lesser or equal <see cref="Id"/> than <paramref name="y"/>.
/// </returns>
[Pure]
public static bool operator <=(AST? x, AST? y) => !(x == y);

/// <inheritdoc />
[Pure]
public override bool Equals(object? obj) => Equals(obj as AST);

/// <inheritdoc />
[Pure]
public override int GetHashCode() => Id.GetHashCode();

/// <inheritdoc />
[Pure]
public override string ToString() => $"{nameof(AST)} {nameof(Id)} = {Id}";

/// <summary><c>eval</c> Function.</summary>
/// <remarks><para>Or "How to Shoot Yourself in the Foot even Easier".</para></remarks>
/// <typeparam name="T">
/// The type of the resulting type, <see cref="object"/> can be used much like <c>rhai::Dynamic</c> in Rust.
/// </typeparam>
/// <param name="length">The size of the internally allocated buffer.</param>
/// <returns>The resulting type from the expression given, or a runtime error from Rhai.</returns>
[MustUseReturnValue]

// ReSharper disable once LambdaShouldNotCaptureContext
public Result<T, Exception> Eval<T>(int length = Span.Stackalloc) => Span.Allocate(length, Eval<T>);

/// <summary><c>eval</c> Function.</summary>
/// <remarks><para>Or "How to Shoot Yourself in the Foot even Easier".</para></remarks>
/// <typeparam name="T">
/// The type of the resulting type, <see cref="object"/> can be used much like <c>rhai::Dynamic</c> in Rust.
/// </typeparam>
/// <param name="buffer">The buffer to mutate.</param>
/// <returns>The resulting type from the expression given, or a runtime error from Rhai.</returns>
[MustUseReturnValue]
public Result<T, Exception> Eval<T>(Span<byte> buffer) => EvalInner<T>(buffer);

/// <summary>Calls the constructor.</summary>
/// <param name="id">The <see cref="Id"/> to store.</param>
/// <returns>A new instance of <see cref="AST"/> with the inner <see cref="Id"/> set.</returns>
Expand Down
5 changes: 5 additions & 0 deletions CSharp/Source/Raw.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#region Emik.MPL

// <copyright file="Raw.cs" company="Emik">
// Copyright (c) Emik. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>

#endregion

namespace Emik.Rhainterop;

#pragma warning disable SA1300
Expand Down
7 changes: 6 additions & 1 deletion CSharp/Source/Rhai.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// <copyright file="Rhai.cs" company="Emik">
#region Emik.MPL

// <copyright file="Rhai.cs" company="Emik">
// Copyright (c) Emik. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>

#endregion

// ReSharper disable LambdaShouldNotCaptureContext
namespace Emik.Rhainterop;

Expand Down
5 changes: 5 additions & 0 deletions CSharp/Source/RhaiException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#region Emik.MPL

// <copyright file="RhaiException.cs" company="Emik">
// Copyright (c) Emik. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>

#endregion

namespace Emik.Rhainterop;

/// <summary>An <see cref="Exception"/> for Rhai compiler or runtime errors.</summary>
Expand Down
9 changes: 7 additions & 2 deletions CSharp/Source/SpanManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#region Emik.MPL

// <copyright file="SpanManager.cs" company="Emik">
// Copyright (c) Emik. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// </copyright>

#endregion

namespace Emik.Rhainterop;

/// <summary>Allows a span into managed memory.</summary>
sealed class SpanManager : MemoryManager<byte>
{
[NonNegativeValue]
readonly unsafe byte* _pointer;
readonly int _length;

[NonNegativeValue]
readonly int _length;
readonly unsafe byte* _pointer;

/// <summary>Initializes a new instance of the <see cref="SpanManager"/> class.</summary>
/// <remarks><para>
Expand Down
10 changes: 6 additions & 4 deletions Rust/rhainterop/src/engines.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::{Id, IdMutex, Map, MapMutex};
use std::collections::HashMap;
use std::num::Wrapping;
use std::ops::DerefMut;

use rhai::packages::Package;
use rhai::Engine;
use rhai::OptimizationLevel;
use rhai_fs::FilesystemPackage;
use rhai_rand::RandomPackage;
use rhai_sci::SciPackage;
use std::collections::HashMap;
use std::num::Wrapping;
use std::ops::DerefMut;

use crate::{Id, IdMutex, Map, MapMutex};

thread_local! {
static ASTS: MapMutex = HashMap::new().into();
Expand Down
16 changes: 9 additions & 7 deletions Rust/rhainterop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
mod engines;
mod pointers;
use std::collections::HashMap;
use std::num::Wrapping;
use std::sync::Mutex;

use crate::engines::{ast, engine, with};
use crate::pointers::Raw;
use rhai::plugin::RhaiResult;
use rhai::{Engine, AST};
use rmp_serde::Serializer;
use serde::Serialize;
use std::collections::HashMap;
use std::num::Wrapping;
use std::sync::Mutex;

use crate::engines::{ast, engine, with};
use crate::pointers::Raw;

mod engines;
mod pointers;

pub type ASTResult<E> = Result<AST, E>;
pub type Id = Wrapping<u64>;
Expand Down

0 comments on commit 45492d1

Please sign in to comment.