diff --git a/Qrakhen.Qamp.CLI/ConsoleRenderer.cs b/Qrakhen.Qamp.CLI/ConsoleRenderer.cs index 9cde85f..1c3c906 100644 --- a/Qrakhen.Qamp.CLI/ConsoleRenderer.cs +++ b/Qrakhen.Qamp.CLI/ConsoleRenderer.cs @@ -1,5 +1,4 @@ - -using Qrakhen.Qamp.Core; +using Qrakhen.Qamp.Core.Collections.Abstractions; using Qrakhen.Qamp.Core.Tokenization; using System.Text; diff --git a/Qrakhen.Qamp.CLI/Program.cs b/Qrakhen.Qamp.CLI/Program.cs index d6e85ad..8e10ef2 100644 --- a/Qrakhen.Qamp.CLI/Program.cs +++ b/Qrakhen.Qamp.CLI/Program.cs @@ -4,15 +4,18 @@ using Qrakhen.Qamp.Core.Execution; using Qrakhen.Qamp.Core.Logging; using System.Text; -char[] Ignored = [ '\0', '\b' ]; +// do args here too, keep console, etc. output with exit, all the spicy things +// if no file is given for example in args just keep cli live until quit + +List Ignored = [ '\0', '\b' ]; LoggerService.Default = LogLevel.Error; -Stack History = []; +List History = []; +int historyCursor = -1; ConsoleKeyInfo input; bool useSyntaxHighlighting = false; (int x, int y) cursor = (0, 0); ConsoleCode code = ConsoleCode.Error; -Runner runner = new Runner(new Options()); -ConsoleWriter cli = new ConsoleWriter(Encoding.Default); +Runner runner = new(new Options()); do { Console.ForegroundColor = ConsoleColor.White; MemoryStream stream = new MemoryStream(); @@ -53,6 +56,18 @@ do { continue; } + if (input.Key == ConsoleKey.UpArrow) { + historyCursor = Math.Min(History.Count - 1, historyCursor + 1); + byte[] bytes = History[historyCursor]; + continue; + } + + if (input.Key == ConsoleKey.DownArrow) { + historyCursor = Math.Max(0, historyCursor - 1); + byte[] bytes = History[historyCursor]; + continue; + } + if (input.Key == ConsoleKey.Backspace && stream.Position > 0) { Console.CursorLeft -= 1; Console.Write(' '); @@ -72,8 +87,10 @@ do { } } while (input.Key != ConsoleKey.Enter || input.Modifiers == ConsoleModifiers.Shift); try { + History.Insert(0, stream.GetBuffer()); Console.WriteLine(); runner.Run(stream); + stream.Dispose(); } catch (QampException e) { Console.ForegroundColor = ConsoleColor.Yellow; diff --git a/Qrakhen.Qamp.CLI/Qrakhen.Qamp.CLI.csproj b/Qrakhen.Qamp.CLI/Qrakhen.Qamp.CLI.csproj index 941e7a1..6382f4a 100644 --- a/Qrakhen.Qamp.CLI/Qrakhen.Qamp.CLI.csproj +++ b/Qrakhen.Qamp.CLI/Qrakhen.Qamp.CLI.csproj @@ -2,7 +2,7 @@ Exe - net10.0 + net8.0 enable enable ..\Build\ diff --git a/Qrakhen.Qamp.Core/IDebug.cs b/Qrakhen.Qamp.Core/Abstractions/IDebug.cs similarity index 93% rename from Qrakhen.Qamp.Core/IDebug.cs rename to Qrakhen.Qamp.Core/Abstractions/IDebug.cs index 836f395..7648ccf 100644 --- a/Qrakhen.Qamp.Core/IDebug.cs +++ b/Qrakhen.Qamp.Core/Abstractions/IDebug.cs @@ -1,6 +1,6 @@ using System.Runtime.CompilerServices; -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Abstractions; public interface IDebug { diff --git a/Qrakhen.Qamp.Core/ISerialize.cs b/Qrakhen.Qamp.Core/Abstractions/ISerialize.cs similarity index 71% rename from Qrakhen.Qamp.Core/ISerialize.cs rename to Qrakhen.Qamp.Core/Abstractions/ISerialize.cs index 27dfcad..0df2445 100644 --- a/Qrakhen.Qamp.Core/ISerialize.cs +++ b/Qrakhen.Qamp.Core/Abstractions/ISerialize.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Abstractions; public interface ISerialize { diff --git a/Qrakhen.Qamp.Core/IAdd.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IAdd.cs similarity index 64% rename from Qrakhen.Qamp.Core/IAdd.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IAdd.cs index 2e69c38..a822d13 100644 --- a/Qrakhen.Qamp.Core/IAdd.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IAdd.cs @@ -1,6 +1,6 @@ using System.Numerics; -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IAdd { diff --git a/Qrakhen.Qamp.Core/ICapturableBuffer.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/ICapturableBuffer.cs similarity index 73% rename from Qrakhen.Qamp.Core/ICapturableBuffer.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/ICapturableBuffer.cs index 101a92b..919e290 100644 --- a/Qrakhen.Qamp.Core/ICapturableBuffer.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/ICapturableBuffer.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface ICaptureBuffer { diff --git a/Qrakhen.Qamp.Core/IConsumable.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IConsumable.cs similarity index 61% rename from Qrakhen.Qamp.Core/IConsumable.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IConsumable.cs index a4bfdf6..d8e3acd 100644 --- a/Qrakhen.Qamp.Core/IConsumable.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IConsumable.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IConsumable { diff --git a/Qrakhen.Qamp.Core/IGet.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IGet.cs similarity index 57% rename from Qrakhen.Qamp.Core/IGet.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IGet.cs index 3e1ca56..5045217 100644 --- a/Qrakhen.Qamp.Core/IGet.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IGet.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IGet { diff --git a/Qrakhen.Qamp.Core/IGetSet.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IGetSet.cs similarity index 59% rename from Qrakhen.Qamp.Core/IGetSet.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IGetSet.cs index 04fbaf6..5d2ff48 100644 --- a/Qrakhen.Qamp.Core/IGetSet.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IGetSet.cs @@ -1,3 +1,3 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IGetSet : IGet, ISet; diff --git a/Qrakhen.Qamp.Core/IPeekable.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IPeekable.cs similarity index 52% rename from Qrakhen.Qamp.Core/IPeekable.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IPeekable.cs index 4a71660..9b36dab 100644 --- a/Qrakhen.Qamp.Core/IPeekable.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IPeekable.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IPeekable { diff --git a/Qrakhen.Qamp.Core/IReadable.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IReadable.cs similarity index 63% rename from Qrakhen.Qamp.Core/IReadable.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IReadable.cs index 90aee0f..1747bb1 100644 --- a/Qrakhen.Qamp.Core/IReadable.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IReadable.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IReadable { diff --git a/Qrakhen.Qamp.Core/IReader.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IReader.cs similarity index 79% rename from Qrakhen.Qamp.Core/IReader.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IReader.cs index c7b0d36..1e0c2d0 100644 --- a/Qrakhen.Qamp.Core/IReader.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IReader.cs @@ -1,6 +1,6 @@ using Qrakhen.Qamp.Core.Tokenization; -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IReader { diff --git a/Qrakhen.Qamp.Core/ISeekable.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/ISeekable.cs similarity index 64% rename from Qrakhen.Qamp.Core/ISeekable.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/ISeekable.cs index 7d2b681..ccebc5b 100644 --- a/Qrakhen.Qamp.Core/ISeekable.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/ISeekable.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface ISeekable { diff --git a/Qrakhen.Qamp.Core/ISet.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/ISet.cs similarity index 68% rename from Qrakhen.Qamp.Core/ISet.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/ISet.cs index 94c7978..7c2b244 100644 --- a/Qrakhen.Qamp.Core/ISet.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/ISet.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface ISet { diff --git a/Qrakhen.Qamp.Core/IStack.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IStack.cs similarity index 76% rename from Qrakhen.Qamp.Core/IStack.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/IStack.cs index cd37036..8463e8f 100644 --- a/Qrakhen.Qamp.Core/IStack.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IStack.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface IStack : IPush, IPop; diff --git a/Qrakhen.Qamp.Core/ISteppable.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/ISteppable.cs similarity index 56% rename from Qrakhen.Qamp.Core/ISteppable.cs rename to Qrakhen.Qamp.Core/Collections/Abstractions/ISteppable.cs index d4016cb..7f7fab9 100644 --- a/Qrakhen.Qamp.Core/ISteppable.cs +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/ISteppable.cs @@ -1,4 +1,4 @@ -namespace Qrakhen.Qamp.Core; +namespace Qrakhen.Qamp.Core.Collections.Abstractions; public interface ISteppable { diff --git a/Qrakhen.Qamp.Core/Collections/Abstractions/IToArray.cs b/Qrakhen.Qamp.Core/Collections/Abstractions/IToArray.cs new file mode 100644 index 0000000..dc032e8 --- /dev/null +++ b/Qrakhen.Qamp.Core/Collections/Abstractions/IToArray.cs @@ -0,0 +1,6 @@ +namespace Qrakhen.Qamp.Core.Collections.Abstractions; + +public interface IToArray +{ + T[] ToArray(); +} \ No newline at end of file diff --git a/Qrakhen.Qamp.Core/Collections/Expander.cs b/Qrakhen.Qamp.Core/Collections/Expander.cs index 95a8eac..9efa56f 100644 --- a/Qrakhen.Qamp.Core/Collections/Expander.cs +++ b/Qrakhen.Qamp.Core/Collections/Expander.cs @@ -1,4 +1,5 @@ -using System.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using System.Collections; namespace Qrakhen.Qamp.Core.Collections; diff --git a/Qrakhen.Qamp.Core/Collections/FixedArray.cs b/Qrakhen.Qamp.Core/Collections/FixedArray.cs index 95802a8..ceeea77 100644 --- a/Qrakhen.Qamp.Core/Collections/FixedArray.cs +++ b/Qrakhen.Qamp.Core/Collections/FixedArray.cs @@ -1,4 +1,5 @@ -using System.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using System.Collections; namespace Qrakhen.Qamp.Core.Collections; diff --git a/Qrakhen.Qamp.Core/Collections/Pointer.cs b/Qrakhen.Qamp.Core/Collections/Pointer.cs index 780c0e2..bedf011 100644 --- a/Qrakhen.Qamp.Core/Collections/Pointer.cs +++ b/Qrakhen.Qamp.Core/Collections/Pointer.cs @@ -1,4 +1,6 @@ -namespace Qrakhen.Qamp.Core.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; + +namespace Qrakhen.Qamp.Core.Collections; /// /// Dynamic Pointer able to point to any object implementing the interface. diff --git a/Qrakhen.Qamp.Core/Collections/PopStack.cs b/Qrakhen.Qamp.Core/Collections/PopStack.cs index e1bc3a6..0734330 100644 --- a/Qrakhen.Qamp.Core/Collections/PopStack.cs +++ b/Qrakhen.Qamp.Core/Collections/PopStack.cs @@ -1,4 +1,5 @@ -using System.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using System.Collections; namespace Qrakhen.Qamp.Core.Collections; diff --git a/Qrakhen.Qamp.Core/Collections/PushStack.cs b/Qrakhen.Qamp.Core/Collections/PushStack.cs index bcb2a35..2298f32 100644 --- a/Qrakhen.Qamp.Core/Collections/PushStack.cs +++ b/Qrakhen.Qamp.Core/Collections/PushStack.cs @@ -1,4 +1,6 @@ -namespace Qrakhen.Qamp.Core.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; + +namespace Qrakhen.Qamp.Core.Collections; /// /// Push-only Stack that is based on . diff --git a/Qrakhen.Qamp.Core/Collections/Register.cs b/Qrakhen.Qamp.Core/Collections/Register.cs index 546d273..3796c30 100644 --- a/Qrakhen.Qamp.Core/Collections/Register.cs +++ b/Qrakhen.Qamp.Core/Collections/Register.cs @@ -1,4 +1,5 @@ -using System.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using System.Collections; using System.Diagnostics.CodeAnalysis; namespace Qrakhen.Qamp.Core.Collections; diff --git a/Qrakhen.Qamp.Core/Collections/StackLike.cs b/Qrakhen.Qamp.Core/Collections/StackLike.cs index 5a0c682..4524a97 100644 --- a/Qrakhen.Qamp.Core/Collections/StackLike.cs +++ b/Qrakhen.Qamp.Core/Collections/StackLike.cs @@ -1,4 +1,6 @@ -namespace Qrakhen.Qamp.Core.Collections; +using Qrakhen.Qamp.Core.Collections.Abstractions; + +namespace Qrakhen.Qamp.Core.Collections; /// /// Stack-like implementation with a few added features like seeking and setting values. diff --git a/Qrakhen.Qamp.Core/Compilation/Builders/SegmentBuilder.cs b/Qrakhen.Qamp.Core/Compilation/Builders/SegmentBuilder.cs index d0642bb..51c9632 100644 --- a/Qrakhen.Qamp.Core/Compilation/Builders/SegmentBuilder.cs +++ b/Qrakhen.Qamp.Core/Compilation/Builders/SegmentBuilder.cs @@ -1,4 +1,5 @@ -using Qrakhen.Qamp.Core.Collections; +using Qrakhen.Qamp.Core.Abstractions; +using Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Execution; using Qrakhen.Qamp.Core.Values; diff --git a/Qrakhen.Qamp.Core/Compilation/Digester.cs b/Qrakhen.Qamp.Core/Compilation/Digester.cs index 411e469..63baab7 100644 --- a/Qrakhen.Qamp.Core/Compilation/Digester.cs +++ b/Qrakhen.Qamp.Core/Compilation/Digester.cs @@ -8,6 +8,7 @@ using Qrakhen.Qamp.Core.Compilation.Builders; using Qrakhen.Qamp.Core.Values.Objects; using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Logging; +using Qrakhen.Qamp.Core.Collections.Abstractions; namespace Qrakhen.Qamp.Core.Compilation; diff --git a/Qrakhen.Qamp.Core/Compilation/ExpressionParser.cs b/Qrakhen.Qamp.Core/Compilation/ExpressionParser.cs index 30a3cf8..8d05e65 100644 --- a/Qrakhen.Qamp.Core/Compilation/ExpressionParser.cs +++ b/Qrakhen.Qamp.Core/Compilation/ExpressionParser.cs @@ -107,17 +107,11 @@ public static class ExpressionParser else break; } - digester.Consume(TokenType.ArrayClose, "Expected ']' after array definition"); + digester.Consume(TokenType.ArrayClose, "Expected ']' after array declaration"); digester.EmitDynamic(OpCode.Array, length); // digester.MakeConstant(new Value((long)length))); } - static void ArrayAdd(Digester digester, bool canAssign) - { - digester.ParseExpression(); - digester.Emit(OpCode.ArrayAdd); - } - - static void Index(Digester digester, bool canAssign) + static void ArrayAccessor(Digester digester, bool canAssign) { digester.ParseExpression(); digester.Consume(TokenType.ArrayClose, "expected ] after array accessor"); @@ -252,7 +246,7 @@ public static class ExpressionParser _rules[TokenType.GroupClose] = new Rule(null, null, Weight.None); _rules[TokenType.ContextOpen] = new Rule(null, null, Weight.None); _rules[TokenType.ContextClose] = new Rule(null, null, Weight.None); - _rules[TokenType.ArrayOpen] = new Rule(Array, Index, Weight.Call); + _rules[TokenType.ArrayOpen] = new Rule(Array, ArrayAccessor, Weight.Call); _rules[TokenType.ArrayClose] = new Rule(null, null, Weight.None); _rules[TokenType.ArrayAdd] = new Rule(null, null, Weight.None); _rules[TokenType.ArrayRemove] = new Rule(null, null, Weight.None); diff --git a/Qrakhen.Qamp.Core/Exceptions.cs b/Qrakhen.Qamp.Core/Exceptions.cs index bd27ed8..7df2fa0 100644 --- a/Qrakhen.Qamp.Core/Exceptions.cs +++ b/Qrakhen.Qamp.Core/Exceptions.cs @@ -1,4 +1,5 @@ -using Qrakhen.Qamp.Core.Tokenization; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Values; namespace Qrakhen.Qamp.Core; diff --git a/Qrakhen.Qamp.Core/Execution/Instructions.cs b/Qrakhen.Qamp.Core/Execution/Instructions.cs index ae2eb22..f5071c6 100644 --- a/Qrakhen.Qamp.Core/Execution/Instructions.cs +++ b/Qrakhen.Qamp.Core/Execution/Instructions.cs @@ -1,4 +1,6 @@ -namespace Qrakhen.Qamp.Core.Execution; +using Qrakhen.Qamp.Core.Collections.Abstractions; + +namespace Qrakhen.Qamp.Core.Execution; public class Instructions(IEnumerable instructions) : ISteppable, diff --git a/Qrakhen.Qamp.Core/Execution/Runner.cs b/Qrakhen.Qamp.Core/Execution/Runner.cs index bad5bfb..2913e4d 100644 --- a/Qrakhen.Qamp.Core/Execution/Runner.cs +++ b/Qrakhen.Qamp.Core/Execution/Runner.cs @@ -192,6 +192,26 @@ public class Runner : IDisposable break; } + case Op.Array: { + // essentially collect all pushed values and create an array, no? + break; + } + + case Op.ArrayGet: { + // attempt to retrieve nth element of array on stack + break; + } + + case Op.ArraySet: { + // set nth item on array + break; + } + + case Op.ArrayAdd: { + // add to array + break; + } + case Op.Jump: { long delta = call.Instruction.NextLong(); call.Instruction.Cursor += delta; @@ -260,7 +280,7 @@ public class Runner : IDisposable break; } - case Op.Return: // todo: not done + case Op.Return: Value result = Pop(); CloseOuters(call.StackPtr.Branch()); Calls.Pop(); @@ -288,7 +308,7 @@ public class Runner : IDisposable if (!inheritingClass.Members.Has(member.Key)) inheritingClass.Members.Add(member.Key, member.Value); } - Pop(); // + Pop(); break; } @@ -321,14 +341,14 @@ public class Runner : IDisposable case Op.PrintStack: { for (int i = 0; i < Cursor; i++) { - Console.WriteLine($"{Stack[i].ToString(true)}"); + IO.Console.Write($"{Stack[i].ToString(true)}"); } break; } case Op.PrintGlobals: { foreach (var value in Globals) { - Console.WriteLine($"{value.Key}: {value.Value.ToString(true)}"); + IO.Console.Write($"{value.Key}: {value.Value.ToString(true)}"); } break; } diff --git a/Qrakhen.Qamp.Core/Execution/Segment.cs b/Qrakhen.Qamp.Core/Execution/Segment.cs index cd69c55..6922f5f 100644 --- a/Qrakhen.Qamp.Core/Execution/Segment.cs +++ b/Qrakhen.Qamp.Core/Execution/Segment.cs @@ -1,4 +1,5 @@ -using Qrakhen.Qamp.Core.Collections; +using Qrakhen.Qamp.Core.Abstractions; +using Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Values; using Qrakhen.Qamp.Core.Values.Objects; diff --git a/Qrakhen.Qamp.Core/Extensions.cs b/Qrakhen.Qamp.Core/Extensions.cs index c202ffe..f3b1422 100644 --- a/Qrakhen.Qamp.Core/Extensions.cs +++ b/Qrakhen.Qamp.Core/Extensions.cs @@ -9,6 +9,7 @@ public static class Extensions { if (string.IsNullOrEmpty(str)) return 0; + byte[] bytes = Encoding.ASCII.GetBytes(str); unchecked { uint hash = 216613661u; @@ -73,10 +74,8 @@ public static class Extensions public static int GetPrimitiveLength(this long value) { int length = 8; - for (int i = 1; i < 8; i++) - { - if (value <= (1L << (i * 8))) - { + for (int i = 1; i < 8; i++) { + if (value <= (1L << (i * 8))) { length = i; break; } @@ -84,20 +83,19 @@ public static class Extensions return length; } + public static byte[] GetBytes(this short value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this ushort value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this int value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this uint value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this long value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this ulong value) => BitConverter.GetBytes(value); + public static byte[] GetBytes(this double value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this short value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this ushort value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this int value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this uint value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this long value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this ulong value) => BitConverter.GetBytes(value); - public static byte[] GetBytes(this double value) => BitConverter.GetBytes(value); - - public static short ToInt16(this byte[] bytes) => BitConverter.ToInt16(bytes); - public static ushort ToUInt16(this byte[] bytes) => BitConverter.ToUInt16(bytes); - public static int ToInt32(this byte[] bytes) => BitConverter.ToInt32(bytes); - public static uint ToUInt32(this byte[] bytes) => BitConverter.ToUInt32(bytes); - public static long ToInt64(this byte[] bytes) => BitConverter.ToInt64(bytes); - public static ulong ToUInt64(this byte[] bytes) => BitConverter.ToUInt64(bytes); - public static double ToDouble(this byte[] bytes) => BitConverter.ToDouble(bytes); + public static short ToInt16(this byte[] bytes) => BitConverter.ToInt16(bytes); + public static ushort ToUInt16(this byte[] bytes) => BitConverter.ToUInt16(bytes); + public static int ToInt32(this byte[] bytes) => BitConverter.ToInt32(bytes); + public static uint ToUInt32(this byte[] bytes) => BitConverter.ToUInt32(bytes); + public static long ToInt64(this byte[] bytes) => BitConverter.ToInt64(bytes); + public static ulong ToUInt64(this byte[] bytes) => BitConverter.ToUInt64(bytes); + public static double ToDouble(this byte[] bytes) => BitConverter.ToDouble(bytes); } \ No newline at end of file diff --git a/Qrakhen.Qamp.Core/IToArray.cs b/Qrakhen.Qamp.Core/IToArray.cs deleted file mode 100644 index b458aee..0000000 --- a/Qrakhen.Qamp.Core/IToArray.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Qrakhen.Qamp.Core; - -public interface IToArray -{ - T[] ToArray(); -} \ No newline at end of file diff --git a/Qrakhen.Qamp.Core/Tokenization/Reader.cs b/Qrakhen.Qamp.Core/Tokenization/Reader.cs index 13ed3fd..c14b0a8 100644 --- a/Qrakhen.Qamp.Core/Tokenization/Reader.cs +++ b/Qrakhen.Qamp.Core/Tokenization/Reader.cs @@ -1,4 +1,5 @@ -using Qrakhen.Qamp.Core.Logging; +using Qrakhen.Qamp.Core.Collections.Abstractions; +using Qrakhen.Qamp.Core.Logging; using System.Text; using System.Text.RegularExpressions; using static Qrakhen.Qamp.Core.Tokenization.TokenType; diff --git a/Qrakhen.Qamp.Core/Tokenization/Tokens.cs b/Qrakhen.Qamp.Core/Tokenization/Tokens.cs index f57f372..4f45f5b 100644 --- a/Qrakhen.Qamp.Core/Tokenization/Tokens.cs +++ b/Qrakhen.Qamp.Core/Tokenization/Tokens.cs @@ -1,4 +1,6 @@ -namespace Qrakhen.Qamp.Core.Tokenization; +using Qrakhen.Qamp.Core.Abstractions; + +namespace Qrakhen.Qamp.Core.Tokenization; public class Tokens : IDebug { diff --git a/Qrakhen.Qamp.Core/Values/Objects/Array.cs b/Qrakhen.Qamp.Core/Values/Objects/Array.cs index 409ee0c..c0d61b4 100644 --- a/Qrakhen.Qamp.Core/Values/Objects/Array.cs +++ b/Qrakhen.Qamp.Core/Values/Objects/Array.cs @@ -4,5 +4,20 @@ public class Array(IEnumerable data) : Obj(ValueType.Array) { public List Data = [..data]; - public override string ToString() => $"Array [{Data.Count}]"; + public void Set(int index, Value value) + { + Data[index] = value; + } + + public Value Get(int index) + { + return Data[index]; + } + + public void Add(Value value) + { + Data.Add(value); + } + + public override string ToString() => $"[{string.Join(", ", Data)}]"; } \ No newline at end of file diff --git a/Qrakhen.Qamp.Core/Values/Objects/Dictionary.cs b/Qrakhen.Qamp.Core/Values/Objects/Dictionary.cs new file mode 100644 index 0000000..0244179 --- /dev/null +++ b/Qrakhen.Qamp.Core/Values/Objects/Dictionary.cs @@ -0,0 +1,20 @@ +namespace Qrakhen.Qamp.Core.Values.Objects; + +public class Dictionary(IEnumerable> data) : Obj(ValueType.Object) +{ + public Dictionary Data = new(data); + + public void Set(string key, Value value) + { + Data[key] = value; + } + + public Value Get(string key) + { + if (Data.TryGetValue(key, out Value value)) + return value; + return Value.Void; + } + + public override string ToString() => $"[{string.Join(", ", Data)}]"; +} \ No newline at end of file diff --git a/Qrakhen.Qamp.Core/Values/Value.cs b/Qrakhen.Qamp.Core/Values/Value.cs index e702a12..05d4f09 100644 --- a/Qrakhen.Qamp.Core/Values/Value.cs +++ b/Qrakhen.Qamp.Core/Values/Value.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; +using Qrakhen.Qamp.Core.Abstractions; using Qrakhen.Qamp.Core.Values.Objects; using String = Qrakhen.Qamp.Core.Values.Objects.String; using T = Qrakhen.Qamp.Core.Values.ValueType; diff --git a/Qrakhen.Qamp.Digest/Qrakhen.Qamp.Digest.csproj b/Qrakhen.Qamp.Digest/Qrakhen.Qamp.Digest.csproj index 359c961..6ed46a3 100644 --- a/Qrakhen.Qamp.Digest/Qrakhen.Qamp.Digest.csproj +++ b/Qrakhen.Qamp.Digest/Qrakhen.Qamp.Digest.csproj @@ -1,7 +1,7 @@  - net10.0 + net8.0 enable enable ..\Build\ diff --git a/Qrakhen.Qamp.Runtime/Qrakhen.Qamp.Runtime.csproj b/Qrakhen.Qamp.Runtime/Qrakhen.Qamp.Runtime.csproj index 5c59036..c190149 100644 --- a/Qrakhen.Qamp.Runtime/Qrakhen.Qamp.Runtime.csproj +++ b/Qrakhen.Qamp.Runtime/Qrakhen.Qamp.Runtime.csproj @@ -1,7 +1,7 @@  - net10.0 + net8.0 enable enable True diff --git a/README.md b/README.md index b87c8b3..abafb6c 100644 --- a/README.md +++ b/README.md @@ -38,12 +38,13 @@ All of the following examples are written in the classic dialect `--dialect=sqr` ### Syntax Most basic Usage: -``` -# This is a comment. -*~ q <~ 12; -*~ f <~ (n) <: n < 2 ? n : f(n-1) + f(n-2); -*~ a <~ [1, 2, 3]; -*~ x <~ a:0 + a:2; # = 4 +```cs + # this is a comment. +*~ q <~ 12; # q == 12 +*~ f <~ (n) <: n < 2 ? n : f(n-1) + f(n-2); # fibonacci +f(12); # 233 +*~ a <~ [1, 2, 3]; # a = [1, 2, 3] +*~ x <~ a:0 + a:2; # x == 4 ``` ### Assignments @@ -51,6 +52,7 @@ Most basic Usage: **Q&** has two distinct ways to assign a value to a variable: - By reference `<&`, or - By value `<=`. + Using the auto-assignment operator `<~`, **Q&** will choose the correct assignment based on what type the right-hand expression consists of. Primitive types will be assigned by value, any object values will be assigned by reference.