2019-02-04 23:52:21 +00:00
|
|
|
|
// <copyright file="Symbols.cs" company="Adrian Conlon">
|
|
|
|
|
// Copyright (c) Adrian Conlon. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace EightBit
|
2019-02-02 15:12:51 +00:00
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
public class Symbols
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<string, Dictionary<string, Dictionary<string, string>>> parsed;
|
|
|
|
|
|
2019-02-04 23:52:21 +00:00
|
|
|
|
public Symbols()
|
|
|
|
|
: this(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
public Symbols(string path)
|
|
|
|
|
{
|
2019-02-06 23:50:25 +00:00
|
|
|
|
this.Labels = new Dictionary<ushort, string>();
|
|
|
|
|
this.Constants = new Dictionary<ushort, string>();
|
|
|
|
|
this.Scopes = new Dictionary<string, ushort>();
|
|
|
|
|
this.Addresses = new Dictionary<string, ushort>();
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.parsed = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
if (path.Length > 0)
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.Parse(path);
|
|
|
|
|
this.AssignSymbols();
|
|
|
|
|
this.AssignScopes();
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 23:50:25 +00:00
|
|
|
|
public Dictionary<ushort, string> Labels { get; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-06 23:50:25 +00:00
|
|
|
|
public Dictionary<ushort, string> Constants { get; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-06 23:50:25 +00:00
|
|
|
|
public Dictionary<string, ushort> Scopes { get; }
|
2019-02-04 23:52:21 +00:00
|
|
|
|
|
2019-02-06 23:50:25 +00:00
|
|
|
|
public Dictionary<string, ushort> Addresses { get; }
|
2019-02-02 15:12:51 +00:00
|
|
|
|
|
|
|
|
|
private void AssignScopes()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
var parsedScopes = this.parsed["scope"];
|
2019-02-02 15:12:51 +00:00
|
|
|
|
foreach (var parsedScopeElement in parsedScopes)
|
|
|
|
|
{
|
|
|
|
|
var parsedScope = parsedScopeElement.Value;
|
|
|
|
|
var name = parsedScope["name"];
|
|
|
|
|
var trimmedName = name.Substring(1, name.Length - 2);
|
|
|
|
|
var size = parsedScope["size"];
|
2019-02-06 23:50:25 +00:00
|
|
|
|
this.Scopes[trimmedName] = ushort.Parse(size);
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AssignSymbols()
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
var symbols = this.parsed["sym"];
|
2019-02-02 15:12:51 +00:00
|
|
|
|
foreach (var symbolElement in symbols)
|
|
|
|
|
{
|
|
|
|
|
var symbol = symbolElement.Value;
|
|
|
|
|
var name = symbol["name"];
|
|
|
|
|
var trimmedName = name.Substring(1, name.Length - 2);
|
|
|
|
|
var value = symbol["val"].Substring(2);
|
|
|
|
|
var number = Convert.ToUInt16(value, 16);
|
|
|
|
|
var symbolType = symbol["type"];
|
|
|
|
|
if (symbolType == "lab")
|
|
|
|
|
{
|
2019-02-06 23:50:25 +00:00
|
|
|
|
this.Labels[number] = trimmedName;
|
|
|
|
|
this.Addresses[trimmedName] = number;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
else if (symbolType == "equ")
|
|
|
|
|
{
|
2019-02-06 23:50:25 +00:00
|
|
|
|
this.Constants[number] = trimmedName;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Parse(string path)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(path))
|
|
|
|
|
{
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
var line = reader.ReadLine();
|
|
|
|
|
var lineElements = line.Split(' ', '\t');
|
|
|
|
|
if (lineElements.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
var type = lineElements[0];
|
|
|
|
|
var dataElements = lineElements[1].Split(',');
|
|
|
|
|
var data = new Dictionary<string, string>();
|
|
|
|
|
foreach (var dataElement in dataElements)
|
|
|
|
|
{
|
|
|
|
|
var definition = dataElement.Split('=');
|
|
|
|
|
if (definition.Length == 2)
|
2019-02-04 23:52:21 +00:00
|
|
|
|
{
|
2019-02-02 15:12:51 +00:00
|
|
|
|
data[definition[0]] = definition[1];
|
2019-02-04 23:52:21 +00:00
|
|
|
|
}
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.ContainsKey("id"))
|
|
|
|
|
{
|
2019-02-04 23:52:21 +00:00
|
|
|
|
if (!this.parsed.ContainsKey(type))
|
|
|
|
|
{
|
|
|
|
|
this.parsed[type] = new Dictionary<string, Dictionary<string, string>>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:12:51 +00:00
|
|
|
|
var id = data["id"];
|
|
|
|
|
data.Remove("id");
|
2019-02-04 23:52:21 +00:00
|
|
|
|
this.parsed[type][id] = data;
|
2019-02-02 15:12:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|