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