mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-10 20:39:54 +00:00
ISADefinition parser is now done.
This commit is contained in:
@@ -12,6 +12,7 @@ namespace SVM.Assembler.Core
|
||||
[Serializable]
|
||||
public class ISADefinition
|
||||
{
|
||||
public Dictionary<string, Dictionary<string, string>> Enums = new Dictionary<string, Dictionary<string, string>>();
|
||||
public Dictionary<PrimaryInstruction, InstructionDefinition> InstructionDefinitions = new Dictionary<PrimaryInstruction, InstructionDefinition>();
|
||||
[NonSerialized]
|
||||
public Dictionary<string, InstructionDefinition> InstructionDefinitionAliases = new Dictionary<string, InstructionDefinition>();
|
||||
@@ -111,6 +112,13 @@ namespace SVM.Assembler.Core
|
||||
{
|
||||
Console.WriteLine($"ParseDefinition:{node.Name}");
|
||||
InstructionDefinition instDefinition = new InstructionDefinition();
|
||||
var PIAttr = node.Attributes.GetNamedItem("PrimaryInstruction");
|
||||
if (PIAttr == null) return false;
|
||||
if (!Enum.TryParse<PrimaryInstruction>(PIAttr.InnerText, out var pi))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
instDefinition.PrimaryInstruction = pi;
|
||||
foreach (XmlNode item in node.ChildNodes)
|
||||
{
|
||||
Console.WriteLine($"{item.Name}");
|
||||
@@ -152,6 +160,7 @@ namespace SVM.Assembler.Core
|
||||
break;
|
||||
}
|
||||
}
|
||||
definition.InstructionDefinitions.Add(pi, instDefinition);
|
||||
return true;
|
||||
}
|
||||
static bool ParseDefinitions(XmlNode node, ref ISADefinition definition)
|
||||
@@ -188,6 +197,48 @@ namespace SVM.Assembler.Core
|
||||
switch (item.Name)
|
||||
{
|
||||
case "Enums":
|
||||
{
|
||||
foreach (XmlNode enumNode in item.ChildNodes)
|
||||
{
|
||||
if (enumNode.Name == "Enum")
|
||||
{
|
||||
Dictionary<string, string> enumItem = new Dictionary<string, string>();
|
||||
var EnumNameAttr = enumNode.Attributes.GetNamedItem("Name");
|
||||
if (EnumNameAttr == null)
|
||||
{
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
foreach (XmlNode enumItemNode in enumNode.ChildNodes)
|
||||
{
|
||||
|
||||
if (enumItemNode.Name == "Item")
|
||||
{
|
||||
|
||||
var keyAttr = enumItemNode.Attributes.GetNamedItem("Key");
|
||||
var valueAttr = enumItemNode.Attributes.GetNamedItem("Value");
|
||||
if (keyAttr == null || valueAttr == null)
|
||||
{
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
enumItem.Add(keyAttr.InnerText, valueAttr.InnerText);
|
||||
}
|
||||
else
|
||||
{
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isaDefinition.Enums.Add(EnumNameAttr.InnerText, enumItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "Definitions":
|
||||
if (ParseDefinitions(item, ref isaDefinition) == false)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<ISARoot>
|
||||
<Enums>
|
||||
<Enum Name="Example">
|
||||
<Item Key="A" Value="0"/>
|
||||
</Enum>
|
||||
</Enums>
|
||||
<Definitions>
|
||||
<InstructionDefinition PrimaryInstruction="BMath">
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using SVM.Assembler.Core;
|
||||
using Newtonsoft.Json;
|
||||
using SVM.Assembler.Core;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SVM.Assembler
|
||||
{
|
||||
@@ -15,11 +14,12 @@ namespace SVM.Assembler
|
||||
Console.WriteLine("Cannot find ISA definition!");
|
||||
return;
|
||||
}
|
||||
if(!ISADefinition.TryParse(fs, out var def))
|
||||
if (!ISADefinition.TryParse(fs, out var def))
|
||||
{
|
||||
Console.WriteLine("Cannot load ISA definition!");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine(JsonConvert.SerializeObject(def, Formatting.Indented));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,8 @@
|
||||
<EmbeddedResource Include="ISA.xml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user