Skip to content

Commit

Permalink
added comment support and whitespace support
Browse files Browse the repository at this point in the history
  • Loading branch information
rbaker26 committed Jun 2, 2020
1 parent 595e821 commit 7853caf
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 36 deletions.
90 changes: 73 additions & 17 deletions SAP1EMU.Assembler/Assemble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,64 @@ namespace SAP1EMU.Assembler
public static class Assemble
{

public static List<string> ParseFileContents(List<string> unchecked_assembly)
public static List<string> Parse(List<string> unchecked_assembly)
{
_ = IsValid(unchecked_assembly); //if is not valid, will throw execptions for CLI to catch and display to user

// *********************************************************************
// Sanitize *
// *********************************************************************

// Remove Blank Lines
unchecked_assembly.RemoveAll(s => s == null);
unchecked_assembly.RemoveAll(s => Regex.IsMatch(s, "^\\s*$"));


// Remove Newline Comments
unchecked_assembly.RemoveAll(s => s[0] == '#');


for (int i = 0; i < unchecked_assembly.Count; i++)
{
// *******************************
// Trim Whitespace *
// *******************************

// Outter Whitespace
unchecked_assembly[i] = unchecked_assembly[i].Trim();

// Inner Whitspace
unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s{2,}", " ");
// *******************************

// *******************************
// Remove Inline Comments
// *******************************
unchecked_assembly[i] = Regex.Replace(unchecked_assembly[i], "\\s*#.*$", "");
// *******************************


}
// *********************************************************************





// *********************************************************************
// Validate
// *********************************************************************


//if is not valid, will throw execptions for CLI to catch and display to user
_ = IsValid(unchecked_assembly);
// *********************************************************************




// *********************************************************************
// Assemble
// *********************************************************************
List<string> binary = new List<string>();

int lines_of_asm = unchecked_assembly.Count;
Expand All @@ -28,7 +80,7 @@ public static List<string> ParseFileContents(List<string> unchecked_assembly)

if (line == "...")
{
int nop_count = 16 - lines_of_asm+1;
int nop_count = 16 - lines_of_asm + 1;
for (int i = 0; i < nop_count; i++)
{
binary.Add("00000000");
Expand Down Expand Up @@ -67,24 +119,16 @@ public static List<string> ParseFileContents(List<string> unchecked_assembly)

current_line_number++;
}
// *********************************************************************


return binary;
}


public static List<string> ParseAssembly(string filename)
{
// Loads the file into the list
List<string> unchecked_assembly = new List<string>(System.IO.File.ReadAllLines(filename));

return ParseFileContents(unchecked_assembly);

}


private static bool IsValid(List<string> unchecked_assembly)
{
bool dot_macro_used = false;

int line_number = 1;
foreach (string line in unchecked_assembly)
{
Expand All @@ -94,14 +138,14 @@ private static bool IsValid(List<string> unchecked_assembly)

if(nibbles.Length == 0)
{
throw new ParseException($"SAP1ASM: Line cannot be blank {line_number}", new ParseException("Use \"NOP 0x0\" for a no-operation command"));
throw new ParseException($"SAP1ASM: Line cannot be blank (line: {line_number})", new ParseException("Use \"NOP 0x0\" for a no-operation command"));

}

string instruction = nibbles[0];
if (nibbles.Length < 2)
{
throw new ParseException($"SAP1ASM: No lower nibble detected {line_number}", new ParseException($"{instruction} must be paired with a valid address in the range of 0x0 - 0xF"));
throw new ParseException($"SAP1ASM: No lower nibble detected (line: {line_number})", new ParseException($"{instruction} must be paired with a valid address in the range of 0x0 - 0xF"));
}
string addr = nibbles[1];

Expand Down Expand Up @@ -139,7 +183,19 @@ private static bool IsValid(List<string> unchecked_assembly)

if(line.Contains("..."))
{
throw new ParseException($"SAP1ASM: invalid use of \"...\" {line_number}", new ParseException($"{line} must only contain \"...\" with no extra charecters or spaces"));
throw new ParseException($"SAP1ASM: invalid use of \"...\" on line {line_number}", new ParseException($"{line} must only contain \"...\" with no extra charecters or spaces"));

}
}
else
{
if (!dot_macro_used)
{
dot_macro_used = true;
}
else
{
throw new ParseException($"SAP1ASM: invalid use of \"...\" {line_number}", new ParseException($"{line} must only contain once instance of \"...\" in the program"));

}
}
Expand Down
2 changes: 1 addition & 1 deletion SAP1EMU.Engine-CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static void Main(string[] args)

if (fileType == FileType.S)
{
compiled_binary = Assemble.ParseFileContents(source_file_contents);
compiled_binary = Assemble.Parse(source_file_contents);
}
else
{
Expand Down
20 changes: 10 additions & 10 deletions SAP1EMU.Lib.Test/AssemblerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void TestParseList_Valid_Code_1()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public void TestParseList_Valid_Code_2()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public void TestParseList_Valid_Code_3()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -183,7 +183,7 @@ public void TestParseList_Valid_Code_4()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -239,7 +239,7 @@ public void TestParseList_Valid_Code_5()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -279,7 +279,7 @@ public void TestParseList_Valid_Code_6()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand Down Expand Up @@ -332,7 +332,7 @@ public void TestParseList_Valid_Code_7()

};

List<string> compiled_bin = Assemble.ParseFileContents(asm);
List<string> compiled_bin = Assemble.Parse(asm);

for (int i = 0; i <= 15; i++)
{
Expand All @@ -359,7 +359,7 @@ public void TestParseList_Invalid_Code_1()
try
{
// This should fail and throw an execption, if it doesn't it will fail
_ = Assemble.ParseFileContents(asm);
_ = Assemble.Parse(asm);
Assert.Fail();
}
catch (ParseException)
Expand All @@ -380,7 +380,7 @@ public void TestParseList_Invalid_Code_2()
try
{
// This should fail and throw an execption, if it doesn't it will fail
_ = Assemble.ParseFileContents(asm);
_ = Assemble.Parse(asm);
Assert.Fail();
}
catch (ParseException)
Expand All @@ -406,7 +406,7 @@ public void TestParseList_Invalid_Code_3()
try
{
// This should fail and throw an execption, if it doesn't it will fail
_ = Assemble.ParseFileContents(asm);
_ = Assemble.Parse(asm);
Assert.Fail();
}
catch (ParseException)
Expand Down
2 changes: 1 addition & 1 deletion SAP1EMU.WebApp/Controllers/AssemblerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ActionResult Post([FromBody] List<string> codeList)
{
try
{
return Ok(SAP1EMU.Assembler.Assemble.ParseFileContents(codeList));
return Ok(SAP1EMU.Assembler.Assemble.Parse(codeList));

}
catch (Exception e)
Expand Down
14 changes: 7 additions & 7 deletions SAP1EMU.WebApp/wwwroot/js/CodeMirror/mode/gas/gas_sap1.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
}
maybeEnd = (ch === "*");
}
return "comment";
return "variable";
}

return {
Expand All @@ -124,12 +124,12 @@

var style, cur, ch = stream.next();

if (ch === "/") {
if (stream.eat("*")) {
state.tokenize = clikeComment;
return clikeComment(stream, state);
}
}
//if (ch === "/") {
// if (stream.eat("*")) {
// state.tokenize = clikeComment;
// return clikeComment(stream, state);
// }
//}

if (ch === lineCommentStartSymbol) {
stream.skipToEnd();
Expand Down

0 comments on commit 7853caf

Please sign in to comment.