Add "at" operator.

This commit is contained in:
2022-11-05 17:41:54 +08:00
parent 8e3bd87667
commit a422f06221
3 changed files with 35 additions and 13 deletions

View File

@@ -90,6 +90,7 @@ namespace Cryville.Common.Pdt {
}
public partial class PdtInterpreter<T> {
readonly static Dictionary<char, int> OP_PRIORITY = new Dictionary<char, int> {
{ '@', 7 },
{ '*', 6 }, { '/', 6 }, { '%', 6 },
{ '+', 5 }, { '-', 5 },
{ '=', 4 }, { '<', 4 }, { '>', 4 },
@@ -100,6 +101,7 @@ namespace Cryville.Common.Pdt {
{ '$', -1 },
};
readonly static Dictionary<char, int> OP_TYPE = new Dictionary<char, int> {
{ '@', 0 },
{ '*', 0 }, { '/', 0 }, { '%', 0 },
{ '+', 0 }, { '-', 0 },
{ '=', 0 }, { '<', 0 }, { '>', 0 },

View File

@@ -42,28 +42,33 @@ namespace Cryville.Common.Pdt {
/// <summary>
/// Gets the memory of the span as a number.
/// </summary>
/// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>A number.</returns>
/// <exception cref="InvalidCastException">The span does not represent a number.</exception>
public float AsNumber() {
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
public float AsNumber(int offset = 0) {
if (Type != PdtInternalType.Number)
throw new InvalidCastException("Not a number");
float value;
byte* ptr = (byte*)&value;
for (int i = 0; i < sizeof(float); i++)
ptr[i] = _ptr[i];
ptr[i] = _ptr[i + offset];
return value;
}
/// <summary>
/// Sets the memory of the span to a number.
/// </summary>
/// <param name="value">The number.</param>
/// <exception cref="InvalidCastException">The span does not represent a number.</exception>
public void SetNumber(float value) {
/// <param name="offset">The offset from the start of the span.</param>
/// <exception cref="InvalidCastException">The span at the offset does not represent a number.</exception>
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
public void SetNumber(float value, int offset = 0) {
if (Type != PdtInternalType.Number)
throw new InvalidCastException("Not a number");
if (Length < sizeof(float) + offset)
throw new InvalidOperationException("Frame length not sufficient");
byte* ptr = (byte*)&value;
for (int i = 0; i < sizeof(float); i++)
_ptr[i] = ptr[i];
_ptr[i + offset] = ptr[i];
}
/// <summary>
/// Gets the memory of the span as a string.