Added multi-line parsing

This commit is contained in:
Brychan Dempsey 2021-03-17 17:27:48 +13:00
parent 087d8d0754
commit ac6a192fb0

View File

@ -88,20 +88,35 @@ namespace Assignment_1
long initSourceLength = source.Length; long initSourceLength = source.Length;
if ((byte)source.ReadByte() == '{') if ((byte)source.ReadByte() == '{')
{ {
long lastLinePos = 0;
long initPos = 0;
bool cont = false;
while (true) while (true)
{ {
if (dynamicInput) if (dynamicInput)
{ {
Console.WriteLine("Enter a command: "); lastLinePos = source.Position;
if (!cont)
{
Console.WriteLine("Enter a command: ");
}
string s = Console.ReadLine(); string s = Console.ReadLine();
long pos = source.Position; long pos = source.Position;
source.Write(Encoding.UTF8.GetBytes(s)); source.Write(Encoding.UTF8.GetBytes(s));
source.Position = pos; source.Position = pos;
} }
// parse the statement or list of statements; // parse the statement or list of statements;
// This is done by reading the next word // This is done by reading the next word
if (!cont)
{
initPos = source.Position;
}
else
{
source.Position = initPos;
}
SkipWhitespace(source); SkipWhitespace(source);
long initPos = source.Position;
long position = FindNextWord(source, out string word); long position = FindNextWord(source, out string word);
try try
{ {
@ -165,6 +180,7 @@ namespace Assignment_1
if (IsNextEoS(source)) if (IsNextEoS(source))
{ {
// Increment the source pos past the semi-colon // Increment the source pos past the semi-colon
cont = false;
source.Position++; source.Position++;
if (dynamicInput) if (dynamicInput)
{ {
@ -173,6 +189,13 @@ namespace Assignment_1
} }
result(); result();
} }
else if(source.Position != lastLinePos)
{
// In the case that we expect some more data, we must keep tabs of our current line, and keep accumulating data until we're finished
cont = true;
source.WriteByte((byte)' ');
Console.Write(">");
}
else else
{ {
throw new ParserException("expected a semi-colon", 0, source.Position); throw new ParserException("expected a semi-colon", 0, source.Position);