Got basic stream piping (Closes #6)
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
This commit is contained in:
parent
ac6a192fb0
commit
83a8631d72
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
@ -39,6 +39,7 @@ namespace Assignment_1
|
|||||||
'$', '\\', '\"', '\''
|
'$', '\\', '\"', '\''
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static bool nonStrict = false;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
@ -48,14 +49,33 @@ namespace Assignment_1
|
|||||||
Console.WriteLine("└──────────────────────────────────────────┘");
|
Console.WriteLine("└──────────────────────────────────────────┘");
|
||||||
// Parse the source from the memory stream
|
// Parse the source from the memory stream
|
||||||
MemoryStream sourceStream = new MemoryStream(1024);
|
MemoryStream sourceStream = new MemoryStream(1024);
|
||||||
Parser parser = new Parser();
|
Parser parser = new();
|
||||||
bool dynamicInput = false;
|
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
|
// 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
|
// Reading from pipes is equivalent to reading user input, though the input is redirected
|
||||||
if (Console.IsInputRedirected)
|
if (Console.IsInputRedirected)
|
||||||
{
|
{
|
||||||
sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
|
sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
|
||||||
|
|
||||||
sourceStream.Position = 0;
|
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
|
else
|
||||||
{
|
{
|
||||||
@ -86,8 +106,12 @@ namespace Assignment_1
|
|||||||
public void StartParsing(Stream source, bool dynamicInput = false)
|
public void StartParsing(Stream source, bool dynamicInput = false)
|
||||||
{
|
{
|
||||||
long initSourceLength = source.Length;
|
long initSourceLength = source.Length;
|
||||||
if ((byte)source.ReadByte() == '{')
|
if (nonStrict || PeekChar(source) == '{')
|
||||||
{
|
{
|
||||||
|
if (PeekChar(source) == '{')
|
||||||
|
{
|
||||||
|
source.ReadByte();
|
||||||
|
}
|
||||||
long lastLinePos = 0;
|
long lastLinePos = 0;
|
||||||
long initPos = 0;
|
long initPos = 0;
|
||||||
bool cont = false;
|
bool cont = false;
|
||||||
@ -206,9 +230,10 @@ namespace Assignment_1
|
|||||||
{
|
{
|
||||||
// Statement parse failed,
|
// Statement parse failed,
|
||||||
// Ensure stream gets trimmed back to the correct position
|
// Ensure stream gets trimmed back to the correct position
|
||||||
Console.WriteLine("Failed parsing statement");
|
//Console.WriteLine("Failed parsing statement");
|
||||||
source.Position = initPos;
|
Console.WriteLine("Position is " + source.Position);
|
||||||
source.SetLength(initPos);
|
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,
|
// 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.Position = initPos;
|
||||||
source.SetLength(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
|
#region Function Handling
|
||||||
|
12
Assignment 1/Properties/launchSettings.json
Normal file
12
Assignment 1/Properties/launchSettings.json
Normal 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'\""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user