Got basic stream piping (Closes #6)
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Brychan Dempsey 2021-03-17 18:24:31 +13:00
parent ac6a192fb0
commit 83a8631d72
3 changed files with 51 additions and 7 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>

View File

@ -39,6 +39,7 @@ namespace Assignment_1
'$', '\\', '\"', '\''
};
static bool nonStrict = false;
static void Main(string[] args)
{
@ -48,14 +49,33 @@ namespace Assignment_1
Console.WriteLine("└──────────────────────────────────────────┘");
// Parse the source from the memory stream
MemoryStream sourceStream = new MemoryStream(1024);
Parser parser = new Parser();
Parser parser = new();
bool dynamicInput = false;
foreach (var arg in args)
{
if (arg == "-ns")
{
nonStrict = true;
}
}
// From https://stackoverflow.com/questions/3453220/how-to-detect-if-console-in-stdin-has-been-redirected
// Reading from pipes is equivalent to reading user input, though the input is redirected
if (Console.IsInputRedirected)
{
sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
sourceStream.Position = 0;
Console.WriteLine("Input is piped");
/*byte[] bytes = new byte[sourceStream.Length];
sourceStream.Read(bytes);
string readBytes = Encoding.UTF8.GetString(bytes);
Console.WriteLine(readBytes);
sourceStream.Position = 0;
foreach (var item in bytes)
{
Console.Write(item+", ");
}
Console.WriteLine();*/
}
else
{
@ -86,8 +106,12 @@ namespace Assignment_1
public void StartParsing(Stream source, bool dynamicInput = false)
{
long initSourceLength = source.Length;
if ((byte)source.ReadByte() == '{')
if (nonStrict || PeekChar(source) == '{')
{
if (PeekChar(source) == '{')
{
source.ReadByte();
}
long lastLinePos = 0;
long initPos = 0;
bool cont = false;
@ -206,9 +230,10 @@ namespace Assignment_1
{
// Statement parse failed,
// Ensure stream gets trimmed back to the correct position
Console.WriteLine("Failed parsing statement");
source.Position = initPos;
source.SetLength(initPos);
//Console.WriteLine("Failed parsing statement");
Console.WriteLine("Position is " + source.Position);
Console.WriteLine("Length is " + source.Length);
throw new ParserException("Failed parsing statement", 0, source.Position);
}
}
// Throwing a parserexception will return us to this point immediately. From here, the line is automatically restored,
@ -230,12 +255,19 @@ namespace Assignment_1
source.Position = initPos;
source.SetLength(initPos);
}
if (!dynamicInput)
{
Environment.Exit(-1);
}
}
}
}
else
{
Console.WriteLine("First read character was not \'{\'. Use the launch flag -ns for non-strict checking");
}
}
#region Function Handling

View File

@ -0,0 +1,12 @@
{
"profiles": {
"Assignment 1": {
"commandName": "Project"
},
"newProfile1": {
"commandName": "Executable",
"executablePath": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"commandLineArgs": "-command \"& type .\\outtest.txt | & '.\\Assignment 1.exe'\""
}
}
}