Skip to content

Commit

Permalink
Cosmetic refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbergen committed Oct 1, 2017
1 parent ee723f5 commit b4ec907
Show file tree
Hide file tree
Showing 84 changed files with 1,650 additions and 1,610 deletions.
8 changes: 5 additions & 3 deletions MixAssembler/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public static InstructionInstanceBase[] Assemble(string[] sourceLines, out PreIn

public static InstructionInstanceBase Assemble(string instructionLine, int locationCounter, out ParsedSourceLine parsedLine, SymbolCollection symbols, out AssemblyFindingCollection findings)
{
var status = new ParsingStatus(symbols);
status.LocationCounter = locationCounter;
status.LineNumber = 0;
var status = new ParsingStatus(symbols)
{
LocationCounter = locationCounter,
LineNumber = 0
};

parsedLine = Parser.ParseInstructionLine(instructionLine, status);
findings = status.Findings;
Expand Down
10 changes: 5 additions & 5 deletions MixAssembler/Finding/AssemblyFindingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace MixAssembler.Finding
{
public class AssemblyFindingCollection : Collection<AssemblyFinding>
{
public bool ContainsDebugs => contains(Severity.Debug);
public bool ContainsDebugs => Contains(Severity.Debug);

public bool ContainsErrors => contains(Severity.Error);
public bool ContainsErrors => Contains(Severity.Error);

public bool ContainsInfos => contains(Severity.Info);
public bool ContainsInfos => Contains(Severity.Info);

public bool ContainsWarnings => contains(Severity.Warning);
public bool ContainsWarnings => Contains(Severity.Warning);

bool contains(Severity severity)
bool Contains(Severity severity)
{
foreach (AssemblyFinding finding in Items)
{
Expand Down
20 changes: 11 additions & 9 deletions MixAssembler/Instruction/MixInstructionParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class MixInstructionParameters : IInstructionParameters
mTextLength = textLength;
}

bool areValuesDefined(AssemblingStatus status)
bool AreValuesDefined(AssemblingStatus status)
{
if (!mAddress.IsValueDefined(status.LocationCounter))
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public InstructionInstanceBase CreateInstance(InstructionBase instruction, Assem

var mixInstruction = (MixInstruction)instruction;

if (!areValuesDefined(status)) return null;
if (!AreValuesDefined(status)) return null;

var addressMagnitude = mAddress.GetMagnitude(status.LocationCounter);
var word = new Word(MixInstruction.AddressByteCount);
Expand All @@ -79,12 +79,14 @@ public InstructionInstanceBase CreateInstance(InstructionBase instruction, Assem
}

word.MagnitudeLongValue = addressMagnitude;
var fieldSpecValue = getFieldSpecValue(status, mixInstruction);
var fieldSpecValue = GetFieldSpecValue(status, mixInstruction);
if (fieldSpecValue == null) return null;

var instructionWord = new FullWord();
instructionWord.Sign = mAddress.GetSign(status.LocationCounter);
for (int i = 0; i < word.ByteCount; i++)
var instructionWord = new FullWord
{
Sign = mAddress.GetSign(status.LocationCounter)
};
for (int i = 0; i < word.ByteCount; i++)
{
instructionWord[i] = word[i];
}
Expand All @@ -94,12 +96,12 @@ public InstructionInstanceBase CreateInstance(InstructionBase instruction, Assem
instructionWord[MixInstruction.OpcodeByte] = mixInstruction.Opcode;

var instance = mixInstruction.CreateInstance(instructionWord);
reportInstanceErrors(status, instance);
ReportInstanceErrors(status, instance);

return instance;
}

MixByte getFieldSpecValue(AssemblingStatus status, MixInstruction mixInstruction)
MixByte GetFieldSpecValue(AssemblingStatus status, MixInstruction mixInstruction)
{
var fieldValue = mField.GetValue(status.LocationCounter);

Expand Down Expand Up @@ -174,7 +176,7 @@ public static IInstructionParameters ParseAddressField(InstructionBase instructi
return new MixInstructionParameters(address, indexCharIndex, index, sectionCharIndex, field, addressField.Length);
}

void reportInstanceErrors(AssemblingStatus status, MixInstruction.Instance instance)
void ReportInstanceErrors(AssemblingStatus status, MixInstruction.Instance instance)
{
var errorArray = instance.Validate();
if (errorArray != null)
Expand Down
46 changes: 22 additions & 24 deletions MixAssembler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public static class Parser
static InstructionSet mInstructionSet = new InstructionSet();
static LoaderInstructions mLoaderInstructions = new LoaderInstructions();

static bool isCommentLine(string sourceLine) => sourceLine.Trim().Length == 0 || sourceLine[0] == '*';
static bool IsCommentLine(string sourceLine) => sourceLine.Trim().Length == 0 || sourceLine[0] == '*';

/// <summary>
/// This method parses an "in-memory" instruction. That is: an instruction without a location field.
/// </summary>
public static ParsedSourceLine ParseInstructionLine(string instructionLine, ParsingStatus status) =>
// add an empty location field, then parse as usual.
parseLine(" " + instructionLine, status);
ParseLine(" " + instructionLine, status);

static int findFirstNonWhiteSpace(string sourceLine, int searchBeyondIndex)
static int FindFirstNonWhiteSpace(string sourceLine, int searchBeyondIndex)
{
for (int i = searchBeyondIndex + 1; i < sourceLine.Length; i++)
{
Expand All @@ -46,7 +46,7 @@ static int findFirstNonWhiteSpace(string sourceLine, int searchBeyondIndex)
return -1;
}

static int findFirstWhiteSpace(string sourceLine, int searchBeyondIndex)
static int FindFirstWhiteSpace(string sourceLine, int searchBeyondIndex)
{
for (int i = searchBeyondIndex + 1; i < sourceLine.Length; i++)
{
Expand All @@ -59,7 +59,7 @@ static int findFirstWhiteSpace(string sourceLine, int searchBeyondIndex)
/// <summary>
/// This method is used to handle instructions targeted at the assembler itself.
/// </summary>
static bool handleAssemblyInstruction(string opField, string addressField, SymbolBase symbol, ParsingStatus status)
static bool HandleAssemblyInstruction(string opField, string addressField, SymbolBase symbol, ParsingStatus status)
{
IValue expression;
status.LineSection = LineSection.AddressField;
Expand Down Expand Up @@ -108,7 +108,7 @@ static bool handleAssemblyInstruction(string opField, string addressField, Symbo
return false;
}

static void getMixOrLoaderInstructionAndParameters(string opField, string addressField, ParsingStatus status, out InstructionBase instruction, out IInstructionParameters instructionParameters)
static void GetMixOrLoaderInstructionAndParameters(string opField, string addressField, ParsingStatus status, out InstructionBase instruction, out IInstructionParameters instructionParameters)
{
status.LineSection = LineSection.AddressField;
instructionParameters = null;
Expand All @@ -131,14 +131,12 @@ static void getMixOrLoaderInstructionAndParameters(string opField, string addres
}
}

static ParsedSourceLine parseLine(string sourceLine, ParsingStatus status)
static ParsedSourceLine ParseLine(string sourceLine, ParsingStatus status)
{
IInstructionParameters instructionParameters;
InstructionBase instruction;

if (isCommentLine(sourceLine)) return new ParsedSourceLine(status.LineNumber, sourceLine);
if (IsCommentLine(sourceLine)) return new ParsedSourceLine(status.LineNumber, sourceLine);

var lineFields = splitLine(sourceLine);
var lineFields = SplitLine(sourceLine);
lineFields[locFieldIndex] = lineFields[locFieldIndex].ToUpper();
lineFields[opFieldIndex] = lineFields[opFieldIndex].ToUpper();
lineFields[addressFieldIndex] = lineFields[addressFieldIndex].ToUpper();
Expand All @@ -150,14 +148,14 @@ static ParsedSourceLine parseLine(string sourceLine, ParsingStatus status)
return new ParsedSourceLine(status.LineNumber, lineFields[0], "", "", "", null, null);
}

var symbol = parseLocField(lineFields[locFieldIndex], status);
var symbol = ParseLocField(lineFields[locFieldIndex], status);
// if the location field contains a symbol name, set its value to the location counter
symbol?.SetValue(status.LocationCounter);

getMixOrLoaderInstructionAndParameters(lineFields[opFieldIndex], lineFields[addressFieldIndex], status, out instruction, out instructionParameters);
GetMixOrLoaderInstructionAndParameters(lineFields[opFieldIndex], lineFields[addressFieldIndex], status, out InstructionBase instruction, out IInstructionParameters instructionParameters);

// the following call must be made even if a MIX or loader instruction was found, as some loader instructions require the assembler to act, as well
var assemblyInstructionHandled = handleAssemblyInstruction(lineFields[opFieldIndex], lineFields[addressFieldIndex], symbol, status);
var assemblyInstructionHandled = HandleAssemblyInstruction(lineFields[opFieldIndex], lineFields[addressFieldIndex], symbol, status);

// if the line isn't a comment or a MIX or loader instruction, it must be an assembler instruction. If not, we don't know the mnemonic
if (instruction == null && !assemblyInstructionHandled)
Expand All @@ -168,7 +166,7 @@ static ParsedSourceLine parseLine(string sourceLine, ParsingStatus status)
return new ParsedSourceLine(status.LineNumber, lineFields[locFieldIndex], lineFields[opFieldIndex], lineFields[addressFieldIndex], lineFields[commentFieldIndex], instruction, instructionParameters);
}

static SymbolBase parseLocField(string locField, ParsingStatus status)
static SymbolBase ParseLocField(string locField, ParsingStatus status)
{
if (locField == "") return null;

Expand Down Expand Up @@ -208,14 +206,14 @@ public static PreInstruction[] ParseSource(string[] sourceLines, ParsingStatus s
{
status.LineNumber = lineNumber;

if (endLineNumber != -1 && !isCommentLine(sourceLines[lineNumber]))
if (endLineNumber != -1 && !IsCommentLine(sourceLines[lineNumber]))
{
status.ReportParsingWarning(LineSection.CommentField, 0, sourceLines[lineNumber].Length, "END has been parsed; line will be treated as comment");
preInstructions.Add(new ParsedSourceLine(lineNumber, sourceLines[lineNumber]));
}
else
{
var parsedLine = parseLine(sourceLines[lineNumber], status);
var parsedLine = ParseLine(sourceLines[lineNumber], status);
preInstructions.Add(parsedLine);
if (parsedLine.OpField == "END")
{
Expand Down Expand Up @@ -250,23 +248,23 @@ public static PreInstruction[] ParseSource(string[] sourceLines, ParsingStatus s
return preInstructions.ToArray();
}

static string[] splitLine(string sourceLine)
static string[] SplitLine(string sourceLine)
{
int addressFieldEnd;
var searchBeyondIndex = findFirstWhiteSpace(sourceLine, -1);
var searchBeyondIndex = FindFirstWhiteSpace(sourceLine, -1);
if (searchBeyondIndex == -1) return new string[] { sourceLine, "", "", "" };

var opFieldStart = findFirstNonWhiteSpace(sourceLine, searchBeyondIndex);
var opFieldStart = FindFirstNonWhiteSpace(sourceLine, searchBeyondIndex);
if (opFieldStart == -1) return new string[] { sourceLine.Substring(0, searchBeyondIndex), "", "", "" };

var opFieldEnd = findFirstWhiteSpace(sourceLine, opFieldStart);
var opFieldEnd = FindFirstWhiteSpace(sourceLine, opFieldStart);
if (opFieldEnd == -1)
{
return new string[] { sourceLine.Substring(0, searchBeyondIndex), sourceLine.Substring(opFieldStart), "", "" };
}

int opFieldLength = opFieldEnd - opFieldStart;
var addressFieldStart = findFirstNonWhiteSpace(sourceLine, opFieldEnd);
var addressFieldStart = FindFirstNonWhiteSpace(sourceLine, opFieldEnd);
if (addressFieldStart == -1)
{
return new string[] { sourceLine.Substring(0, searchBeyondIndex), sourceLine.Substring(opFieldStart, opFieldLength), "", "" };
Expand All @@ -286,7 +284,7 @@ static string[] splitLine(string sourceLine)
}
else
{
addressFieldEnd = findFirstWhiteSpace(sourceLine, addressFieldStart);
addressFieldEnd = FindFirstWhiteSpace(sourceLine, addressFieldStart);
}

if (addressFieldEnd == -1)
Expand All @@ -295,7 +293,7 @@ static string[] splitLine(string sourceLine)
}

int addressFieldLength = addressFieldEnd - addressFieldStart;
var commentFieldStart = findFirstNonWhiteSpace(sourceLine, addressFieldEnd);
var commentFieldStart = FindFirstNonWhiteSpace(sourceLine, addressFieldEnd);
if (commentFieldStart == -1)
{
return new string[] { sourceLine.Substring(0, searchBeyondIndex), sourceLine.Substring(opFieldStart, opFieldLength), sourceLine.Substring(addressFieldStart, addressFieldLength), "" };
Expand Down
6 changes: 3 additions & 3 deletions MixAssembler/Symbol/LiteralConstantSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class LiteralConstantSymbol : SymbolBase

public override bool IsValueDefined(int currentAddress) => mValueDefined;

static string getName(Word.Signs literalSign, long literalMagnitude, int count) =>
static string GetName(Word.Signs literalSign, long literalMagnitude, int count) =>
string.Concat("=", (literalSign.IsNegative() ? "-" : ""), literalMagnitude, '=', count);

public static IValue ParseValue(string text, int sectionCharIndex, ParsingStatus status)
Expand All @@ -50,12 +50,12 @@ public static IValue ParseValue(string text, int sectionCharIndex, ParsingStatus
var literalSign = expressionValue.GetSign(status.LocationCounter);

int count = 0;
var name = getName(literalSign, literalMagnitude, count);
var name = GetName(literalSign, literalMagnitude, count);

while (status.Symbols[name] != null)
{
count++;
name = getName(literalSign, literalMagnitude, count);
name = GetName(literalSign, literalMagnitude, count);
}

SymbolBase literalConstantSymbol = new LiteralConstantSymbol(literalSign, literalMagnitude, name);
Expand Down
24 changes: 12 additions & 12 deletions MixAssembler/Symbol/LocalSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public LocalSymbol(int index) : base(index.ToString())

public override void SetValue(long value) => mAddresses.Add(value);

static bool isBackwardReferenceChar(char c) => c == 'B';
static bool IsBackwardReferenceChar(char c) => c == 'B';

static bool isDefinitionChar(char c) => c == 'H';
static bool IsDefinitionChar(char c) => c == 'H';

static bool isForwardReferenceChar(char c) => c == 'F';
static bool IsForwardReferenceChar(char c) => c == 'F';

static bool isLocalSymbol(string text) => (text.Length == 2 && char.IsNumber(text[0])) && isLocalSymbolChar(text[1]);
static bool IsLocalSymbol(string text) => (text.Length == 2 && char.IsNumber(text[0])) && IsLocalSymbolChar(text[1]);

static bool isLocalSymbolChar(char c) => "BHF".IndexOf(c) >= 0;
static bool IsLocalSymbolChar(char c) => "BHF".IndexOf(c) >= 0;

public override bool IsValueDefined(int currentAddress) => false;

Expand All @@ -46,9 +46,9 @@ public override long GetValue(int currentAddress)

public static SymbolBase ParseDefinition(string text, int sectionCharIndex, ParsingStatus status)
{
if (!isLocalSymbol(text)) return null;
if (!IsLocalSymbol(text)) return null;

if (!isDefinitionChar(text[1]))
if (!IsDefinitionChar(text[1]))
{
status.ReportParsingError(sectionCharIndex + 1, 1, "local symbol reference found where definition was expected");
}
Expand All @@ -67,7 +67,7 @@ public static SymbolBase ParseDefinition(string text, int sectionCharIndex, Pars

public static IValue ParseValue(string text, int sectionCharIndex, ParsingStatus status)
{
if (!isLocalSymbol(text)) return null;
if (!IsLocalSymbol(text)) return null;

char localSymbolChar = text[0];
SymbolBase symbol = status.Symbols[new string(localSymbolChar, 1)];
Expand All @@ -84,13 +84,13 @@ public static IValue ParseValue(string text, int sectionCharIndex, ParsingStatus
return symbol;
}

if (isDefinitionChar(text[1]))
if (IsDefinitionChar(text[1]))
{
status.ReportParsingError(sectionCharIndex + 1, 1, "local symbol definition found where reference was expected");
return symbol;
}

return new reference((LocalSymbol)symbol, isForwardReferenceChar(text[1]) ? reference.Directions.Forwards : reference.Directions.Backwards);
return new Reference((LocalSymbol)symbol, IsForwardReferenceChar(text[1]) ? Reference.Directions.Forwards : Reference.Directions.Backwards);
}

public ICollection<long> Addresses
Expand All @@ -112,12 +112,12 @@ public override Word.Signs GetSign(int currentAddress)
throw new NotImplementedException();
}

class reference : IValue
class Reference : IValue
{
Directions mDirection;
LocalSymbol mReferee;

public reference(LocalSymbol referee, Directions direction)
public Reference(LocalSymbol referee, Directions direction)
{
mReferee = referee;
mDirection = direction;
Expand Down
Loading

0 comments on commit b4ec907

Please sign in to comment.