From c40d516587e5ddb83aacfb51b5f352b13132d6eb Mon Sep 17 00:00:00 2001 From: jackdelahunt Date: Sat, 28 Aug 2021 13:50:37 +0100 Subject: [PATCH] Feat: added dump command --- kasic/Commands/CommandRegister.cs | 3 ++- kasic/Commands/Dump.cs | 36 +++++++++++++++++++++++++++++++ kasic/Memory/Heap.cs | 14 +++++++----- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 kasic/Commands/Dump.cs diff --git a/kasic/Commands/CommandRegister.cs b/kasic/Commands/CommandRegister.cs index e092f28..04e7cfa 100644 --- a/kasic/Commands/CommandRegister.cs +++ b/kasic/Commands/CommandRegister.cs @@ -33,7 +33,8 @@ static CommandRegister() // string RegisterCommand(new Replace()); - + RegisterCommand(new Dump()); + // IO RegisterCommand(new Write()); diff --git a/kasic/Commands/Dump.cs b/kasic/Commands/Dump.cs new file mode 100644 index 0000000..3b3add1 --- /dev/null +++ b/kasic/Commands/Dump.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; +using kasic.Kasic; +using kasic.Logging; +using kasic.Memory; +using kasic.Utils; +using OperationResult; +using System.Web; + +namespace kasic.Commands +{ + public class Dump : Command + { + public Dump() : base("dump") + { + CommandSettings = new CommandSettings() + { + MinArgs = 0, + MaxArgs = 0, + ArgumentList = new ArgumentList(new List()), + ReturnType = KasicType.STRING, + }; + } + + public override Result Run(Context context, Arguments arguments, List flags) + { + var json =JsonSerializer.Serialize( + Heap.Dump, typeof(List), + new JsonSerializerOptions() {WriteIndented = flags.Contains("-i")} + ); + return new ReturnObject(this, json); + } + } +} \ No newline at end of file diff --git a/kasic/Memory/Heap.cs b/kasic/Memory/Heap.cs index a6d7ebd..279ecf0 100644 --- a/kasic/Memory/Heap.cs +++ b/kasic/Memory/Heap.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.Serialization; using kasic.Commands; using kasic.Kasic; using kasic.Logging; @@ -81,14 +82,17 @@ public static HeapObject GetByObjectId(int id) Debug.Assert(heap.Count > id && id != -1); return heap[id]; } + + public static List Dump => heap; } + [Serializable] public struct HeapObject { - public int ObjectId; - public string Name; - public KasicType Type; - public object Data; - public bool Const; + public int ObjectId { get; set; } + public string Name { get; set; } + public KasicType Type { get; set; } + public object Data { get; set; } + public bool Const { get; set; } } } \ No newline at end of file