Got many functions initially working.

Code can be parsed via pipes or typed lines
This commit is contained in:
Brychan Dempsey 2021-03-11 19:22:49 +13:00
parent 2c9dff8e13
commit be780d54e2

View File

@ -31,10 +31,10 @@ namespace Assignment_1
}; };
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"); Console.WriteLine("┌──────────────────────────────────────────┐");
Console.WriteLine("┃ 159.341 2021 Semester 1, Assignment 1 ┃"); Console.WriteLine("│ 159.341 2021 Semester 1, Assignment 1 │");
Console.WriteLine("┃ Submitted by Brychan Dempsey, 14299890 ┃"); Console.WriteLine("│ Submitted by Brychan Dempsey, 14299890 │");
Console.WriteLine("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"); Console.WriteLine("└──────────────────────────────────────────┘");
MemoryStream sourceStream = new MemoryStream(1024); // Creates a memory stream to retain source while being interpreted. MemoryStream sourceStream = new MemoryStream(1024); // Creates a memory stream to retain source while being interpreted.
@ -50,11 +50,13 @@ namespace Assignment_1
} }
else else
{ {
sourceStream.Write(Encoding.UTF8.GetBytes("{\r\n")); sourceStream.Write(Encoding.UTF8.GetBytes("{ \r\n"));
sourceStream.Position = 0; sourceStream.Position = 0;
dynamicInput = true; dynamicInput = true;
} }
parser.StartParsing(sourceStream, dynamicInput); parser.StartParsing(sourceStream, dynamicInput);
Console.ReadLine();
} }
public class Parser public class Parser
{ {
@ -72,7 +74,63 @@ namespace Assignment_1
} }
public void StartParsing(Stream source, bool dynamicInput = false) public void StartParsing(Stream source, bool dynamicInput = false)
{ {
if ((byte)source.ReadByte() == '{')
{
while (true)
{
if (dynamicInput)
{
Console.WriteLine("Enter a command: ");
string s = Console.ReadLine();
long pos = source.Position;
source.Write(Encoding.UTF8.GetBytes(s));
source.Position = pos;
int g = 0;
}
// parse the statement or list of statements;
// This is done by reading the next word
SkipWhitespace(source);
string word = "";
long position = FindNextWord(source, out word);
object statementType;
if (Enum.TryParse(typeof(statements), word, out statementType))
{
source.Position = position;
switch ((statements)statementType)
{
case statements.exit:
Exit();
break;
case statements.append:
Append(source);
break;
case statements.list:
List();
break;
case statements.print:
Print(source, 0);
break;
case statements.printlength:
Print(source, 1);
break;
case statements.printwordcount:
Print(source, 2);
break;
case statements.printwords:
Print(source, 3);
break;
case statements.set:
Set(source);
break;
case statements.reverse:
Reverse(source);
break;
}
}
else Console.WriteLine("Failed parsing statement");
}
}
} }
#region Function Handling #region Function Handling
@ -118,13 +176,13 @@ namespace Assignment_1
void List() void List()
{ {
Console.WriteLine("┌" + new string('─', 49) + "┐"); Console.WriteLine("┌" + new string('─', 15) + "┬" + new string('─', 25) + "┬" + new string('─', 9) + "┐");
Console.WriteLine("│{0:-15}│{1:-25}│{2:9}│", "Symbol", "Value", "Flags"); Console.WriteLine("│{0,-15}│{1,-25}│{2,9}│", "Symbol", "Value", "Flags");
Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤"); Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤");
int keyPos = Symbols.Count; int keyPos = 0;
foreach (var item in Symbols) foreach (var item in Symbols)
{ {
Console.WriteLine("│{0:-15}│{1:-25}│{2:9}│", item.Key, item.Value.Item1, Convert.ToString((byte)item.Value.Item2,2).PadLeft(8,'0')); Console.WriteLine("│{0,-15}│{1,-25}│{2,9}│", item.Key, item.Value.Item1.Replace("\r","\\r").Replace("\n","\\n").Replace("\t", "\\t"), Convert.ToString((byte)item.Value.Item2,2).PadLeft(8,'0'));
if (keyPos == Symbols.Count-1) if (keyPos == Symbols.Count-1)
{ {
Console.WriteLine("└" + new string('─', 15) + "┴" + new string('─', 25) + "┴" + new string('─', 9) + "┘"); Console.WriteLine("└" + new string('─', 15) + "┴" + new string('─', 25) + "┴" + new string('─', 9) + "┘");
@ -133,6 +191,7 @@ namespace Assignment_1
{ {
Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤"); Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤");
} }
keyPos++;
} }
} }
void Exit() void Exit()
@ -176,7 +235,7 @@ namespace Assignment_1
} }
} }
source.Position = result;
return true; return true;
} }
@ -200,9 +259,10 @@ namespace Assignment_1
} }
if (Symbols.ContainsKey(identifier)) if (Symbols.ContainsKey(identifier))
{ {
if (Symbols[identifier].Item2 == VariableFlags.Reserved) if (Symbols[identifier].Item2.HasFlag(VariableFlags.Reserved))
{ {
Console.WriteLine("Error: Cannot assign to {0} as it is a reserved constant."); Console.WriteLine("Error: Cannot assign to {0} as it is a reserved constant.", identifier);
source.SetLength(source.Position); // Wipe the remainder of the stream, so that it doesn't get read
return false; return false;
} }
Symbols[identifier] = new Tuple<string, VariableFlags>(expression, Symbols[identifier].Item2); Symbols[identifier] = new Tuple<string, VariableFlags>(expression, Symbols[identifier].Item2);
@ -211,6 +271,7 @@ namespace Assignment_1
{ {
Symbols.Add(identifier, new Tuple<string, VariableFlags>(expression, VariableFlags.Empty)); Symbols.Add(identifier, new Tuple<string, VariableFlags>(expression, VariableFlags.Empty));
} }
source.Position = resultPos;
return true; return true;
} }
@ -289,8 +350,9 @@ namespace Assignment_1
} }
else else
{ {
s.Position--;
string keyValue; string keyValue;
long t = FindIdentifier(s, out keyValue); long t = FindExistingIdentifier(s, out keyValue);
returnedValue = Symbols[keyValue].Item1; returnedValue = Symbols[keyValue].Item1;
return t; return t;
@ -298,21 +360,16 @@ namespace Assignment_1
} }
long FindIdentifier(Stream s, out string returnedKey) long FindIdentifier(Stream s, out string returnedKey)
{
long wordEnd = FindNextWord(s, out returnedKey);
return wordEnd;
}
long FindExistingIdentifier(Stream s, out string returnedKey)
{ {
string identifier; string identifier;
long wordEnd = FindNextWord(s, out identifier); long wordEnd = FindNextWord(s, out identifier);
// Lookup the value in the symbol table // Lookup the value in the symbol table
try returnedKey = identifier;
{
returnedKey = Symbols[identifier].Item1;
}
catch (KeyNotFoundException e)
{
Console.WriteLine("Could not find a defined variable with the name {0}", identifier);
Console.Error.WriteLine(e);
returnedKey = "";
return -1;
}
return wordEnd; return wordEnd;
} }
@ -418,7 +475,11 @@ namespace Assignment_1
while (!charFound) while (!charFound)
{ {
char nextChar = GetChar(s); char nextChar = GetChar(s);
if (p(nextChar, s)) if (nextChar == 0)
{
charFound = true;
}
else if (p(nextChar, s))
{ {
/*if (c == '\n') /*if (c == '\n')
{ {
@ -437,7 +498,7 @@ namespace Assignment_1
result = sb.ToString(); result = sb.ToString();
long newPosition = s.Position; long newPosition = s.Position;
s.Position = start; s.Position = start;
return newPosition; return newPosition--;
} }
/// <summary> /// <summary>
@ -459,6 +520,10 @@ namespace Assignment_1
// Fortunately, the standard ASCII table is 7-bits long. The 8th bit is used to determine the character size // Fortunately, the standard ASCII table is 7-bits long. The 8th bit is used to determine the character size
int readAmount = 0; int readAmount = 0;
int firstChar = s.ReadByte(); int firstChar = s.ReadByte();
if (firstChar == -1)
{
return (char)0;
}
if ((firstChar >> 3) == 0x1E) // 11110xxx implies a 4-byte length character if ((firstChar >> 3) == 0x1E) // 11110xxx implies a 4-byte length character
{ {
readAmount = 3; readAmount = 3;
@ -480,6 +545,7 @@ namespace Assignment_1
if (nextChar >> 6 != 2) throw new Exception("Character is not a valid UTF-8 code point!"); if (nextChar >> 6 != 2) throw new Exception("Character is not a valid UTF-8 code point!");
charBytes[i] = (byte)nextChar; charBytes[i] = (byte)nextChar;
} }
s.Position += readAmount;
string converted = Encoding.UTF8.GetString(charBytes); string converted = Encoding.UTF8.GetString(charBytes);
return converted[0]; return converted[0];
} }
@ -491,6 +557,7 @@ namespace Assignment_1
{ {
readByte = s.ReadByte(); readByte = s.ReadByte();
} }
s.Position--;
} }
} }
} }