Compare commits

..

3 Commits

Author SHA1 Message Date
7967fc4d21 Filled in a lot of code
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed
2021-03-26 11:30:26 +13:00
3ed9bc885a Created a memory stream
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed
2021-03-26 10:35:41 +13:00
f3404e7b11 Added a C++ variant
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
2021-03-26 10:08:04 +13:00
15 changed files with 544 additions and 309 deletions

3
.gitignore vendored
View File

@ -361,6 +361,3 @@ MigrationBackup/
# Fody - auto-generated XML schema # Fody - auto-generated XML schema
FodyWeavers.xsd FodyWeavers.xsd
# Assignment 1 instructions
assignment_instructions.txt

View File

@ -0,0 +1,171 @@
// Assignment 1 (C++).cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
int main()
{
std::cout << "┌────────────────────────────────────────────────┐\n";
std::cout << "│ 159.341 2021 Semester 1, Assignment 1 (C++) │\n";
std::cout << "│ Submitted by Brychan Dempsey, 14299890 │\n";
std::cout << "└────────────────────────────────────────────────┘\n";
bool exit = false;
while (!exit) {
MemoryStream ms(1024);
}
}
bool IsWhiteSpace(char c) {
if (c == ' ' || c == '\r' || c == '\n') {
return true;
}
return false;
}
void skipWhitespace(MemoryStream ms) {
char c = ms.PeekChar();
while (ms.position < ms.length && IsWhiteSpace(c)) {
ms.ReadChar();
c = ms.PeekChar();
}
}
std::string FindNextWord(MemoryStream ms, long &position) {
std::string nextWord;
long start = ms.position;
char curr = ms.ReadChar();
while (ms.position < ms.length && IsWhiteSpace(curr))
{
curr = ms.ReadChar();
}
nextWord.push_back(curr);
while (ms.position < ms.length)
{
curr = ms.ReadChar();
if (IsWhiteSpace(curr) || curr == ';')
{
ms.position--;
break;
}
else
{
nextWord.push_back(curr);
}
}
position = ms.position;
ms.position = start;
return nextWord;
}
struct MemoryStream {
public:
long position = 0;
long length = 0;
private:
std::vector<char> chars;
bool ensureCapacity(long step) {
// Size exceeds the bounds of the vector, resize
if (position + step >= chars.size()) {
chars.resize(position + step);
}
}
public:
MemoryStream(long Reserved) {
chars.assign(Reserved, 0);
}
char ReadChar() {
if (++position >= length) return -1;
return chars[position];
}
char PeekChar() {
char result = ReadChar();
position--;
return result;
}
bool writeChar(char c) {
ensureCapacity(1);
if (position == length) length++;
chars[++position] = c;
}
bool writeChars(const char* c, int count) {
ensureCapacity(count);
if (position >= length - count) length = position + count;
for (int i = 0; i < count; i++)
{
chars[i + position] = *(c + i);
}
}
};
struct Parser {
std::vector<std::string> symbolNames;
std::vector<std::string> symbolValues;
void Exit() {
std::exit(0);
}
void StartParsing(MemoryStream ms, bool dynamicInput = false) {
long startLength = ms.length;
long lastLinePos = 0;
long initPos = 0;
bool cont = false;
while (true) {
if (dynamicInput) {
lastLinePos = ms.position;
if (!cont)
{
std::cout << "Enter a command: " << std::endl;
}
std::string s;
std::getline(std::cin, s);
long pos = ms.position;
ms.writeChars(s.c_str(), s.size());
ms.position = pos;
}
if (!cont) {
initPos = ms.position;
}
else {
ms.position = initPos;
}
skipWhitespace(ms);
long position = 0;
std::string word = FindNextWord(ms, position);
try {
if (word._Equal("set")) {
}
else if (word._Equal("exit")) {
}
else if (word._Equal("append")) {
}
else if (word._Equal("list")) {
}
else if (word._Equal("print")) {
}
}
catch (std::exception e)
{
}
}
}
};

View File

@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{d15c6fed-4af0-4afd-b109-b9755a23c909}</ProjectGuid>
<RootNamespace>Assignment1C</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Assignment 1 (C++).cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Assignment 1 (C++).cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -3,18 +3,42 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.31025.194 VisualStudioVersion = 16.0.31025.194
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assignment 1", "Assignment 1\Assignment 1.csproj", "{90821384-3BA4-4373-A08C-DA6BC25D688A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Assignment 1", "Assignment 1\Assignment 1.csproj", "{90821384-3BA4-4373-A08C-DA6BC25D688A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Assignment 1 (C++)", "Assignment 1 (C++)\Assignment 1 (C++).vcxproj", "{D15C6FED-4AF0-4AFD-B109-B9755A23C909}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|Any CPU.Build.0 = Debug|Any CPU {90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|x64.ActiveCfg = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|x64.Build.0 = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|x86.ActiveCfg = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Debug|x86.Build.0 = Debug|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|Any CPU.ActiveCfg = Release|Any CPU {90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|Any CPU.Build.0 = Release|Any CPU {90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|Any CPU.Build.0 = Release|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|x64.ActiveCfg = Release|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|x64.Build.0 = Release|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|x86.ActiveCfg = Release|Any CPU
{90821384-3BA4-4373-A08C-DA6BC25D688A}.Release|x86.Build.0 = Release|Any CPU
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Debug|Any CPU.ActiveCfg = Debug|Win32
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Debug|x64.ActiveCfg = Debug|x64
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Debug|x64.Build.0 = Debug|x64
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Debug|x86.ActiveCfg = Debug|Win32
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Debug|x86.Build.0 = Debug|Win32
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Release|Any CPU.ActiveCfg = Release|Win32
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Release|x64.ActiveCfg = Release|x64
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Release|x64.Build.0 = Release|x64
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Release|x86.ActiveCfg = Release|Win32
{D15C6FED-4AF0-4AFD-B109-B9755A23C909}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -2,12 +2,8 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<RootNamespace>Assignment_1</RootNamespace> <RootNamespace>Assignment_1</RootNamespace>
</PropertyGroup> </PropertyGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="xcopy &quot;$(ProjectDir)\Examples\*.txt&quot; &quot;$(OutDir)&quot; /Y /I" />
</Target>
</Project> </Project>

View File

@ -1,12 +0,0 @@
set one "The cat";
set two "sat on the mat";
set sentence one + SPACE + two;
append sentence " by itself.";
print sentence;
printwords sentence;
printwordcount sentence;
printlength sentence;
reverse sentence;
print sentence;
list;
exit;

View File

@ -1,11 +0,0 @@
set var1 "The";
set var2 "pesky";
set var3 "feline";
set var4 "sat,";
set var5 "for what is hopefully the last time,";
set var6 "on the mat.";
set longvar var1 + SPACE;
append longvar var2 + SPACE + var3 + SPACE + var4 + SPACE;
append longvar var5 + SPACE + var6 + NEWLINE;
list;
exit;

View File

@ -1,6 +0,0 @@
set a1 "To be, or not to be - that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles, And by opposing end them. To die - to sleep - No more, and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wish'd. To die - to sleep. To sleep - perchance to dream: ay, there's the rub! For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause. There's the respect That makes calamity of so long life. For who would bear the whips and scorns of time, The oppressor's wrong, the proud man's contumely, The pangs of despised love, the law's delay, The insolence of office, and the spurns That patient merit of the unworthy takes, When he himself might his quietus make";
print a1;
printwords a1;
printwordcount a1;
printlength a1;
exit;

View File

@ -1,49 +0,0 @@
set a1 "
To be, or not to be - that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles,
And by opposing end them. To die - to sleep -
No more; and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wish'd. To die - to sleep.
To sleep - perchance to dream: ay, there's the rub!
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause. There's the respect
That makes calamity of so long life.
For who would bear the whips and scorns of time,
The oppressor's wrong, the proud man's contumely,
The pangs of despised love, the law's delay,
The insolence of office, and the spurns
That patient merit of the unworthy takes,
When he himself might his quietus make" + NEWLINE +
"With a bare bodkin?
";
set averylongvariablethathasbeencontrivedjusttobeanuisancetothe341classwhohaveprobablythoughtthattheycouldgetawaywithallocatingshortvariablenamesofashortlengthbutaregoingtobereallydisappointediftheydidbecausethisisnotashortvariableidentifieratall "
Who would these fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death -
The undiscover'd country, from whose bourn
No traveller returns - puzzles the will,
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience does make cowards of us all,
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry
And lose the name of action.";
set soliloquy a1 + NEWLINE + averylongvariablethathasbeencontrivedjusttobeanuisancetothe341classwhohaveprobablythoughtthattheycouldgetawaywithallocatingshortvariablenamesofashortlengthbutaregoingtobereallydisappointediftheydidbecausethisisnotashortvariableidentifieratall;
set dummy "dummy1";
print soliloquy;
printwordcount soliloquy;
printwords soliloquy;
printlength soliloquy;
list;
exit;

View File

@ -1,3 +0,0 @@
set apple "Apple trees are small.";
reverse apple;
print apple;

View File

@ -1,10 +1,11 @@
/* Dempsey-Jensen, Brychan, 14299890, Assignment 1, 159.341 */ using System;
/* Parses and interprets a simple programming language line-by-line */
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
namespace Assignment_1 namespace Assignment_1
@ -12,7 +13,7 @@ namespace Assignment_1
class Program class Program
{ {
/// <summary> /// <summary>
/// Flags to set symbol properties /// Flags to set object properties.
/// </summary> /// </summary>
[Flags] [Flags]
enum VariableFlags enum VariableFlags
@ -20,53 +21,64 @@ namespace Assignment_1
Empty = 0, Empty = 0,
Reserved = 1, Reserved = 1,
NoPrint = 2, NoPrint = 2,
Static = 4 Static = 4,
Undef = 8
} }
/// <summary>
// Max display width for tables etc. /// Characters that cannot appear in a normal string
static readonly int ConsoleWidthLimit = 80; /// </summary>
static readonly List<char> ForbiddenChars = new()
{
'$',
'\\',
'\"',
'\''
};
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine(CenterString("┌──────────────────────────────────────────┐", ConsoleWidthLimit)); Console.WriteLine("┌──────────────────────────────────────────────┐");
Console.WriteLine(CenterString("│ 159.341 2021 Semester 1, Assignment 1 │", ConsoleWidthLimit)); Console.WriteLine("│ 159.341 2021 Semester 1, Assignment 1 (C#) │");
Console.WriteLine(CenterString("│ Submitted by Brychan Dempsey, 14299890 │", ConsoleWidthLimit)); Console.WriteLine("│ Submitted by Brychan Dempsey, 14299890 │");
Console.WriteLine(CenterString("└──────────────────────────────────────────┘", ConsoleWidthLimit)); Console.WriteLine("└──────────────────────────────────────────────┘");
bool loadedFromFile = false; // Parse the source from the memory stream
bool exit = false; bool exit = false;
while (!exit) while (!exit)
{ {
MemoryStream sourceStream = new MemoryStream(1024); MemoryStream sourceStream = new(1024);
Parser parser = new Parser(); Parser parser = new();
bool dynamicInput = false; bool dynamicInput = false;
// 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, but the input is redirected // Reading from pipes is equivalent to reading user input, though the input is redirected
if (Console.IsInputRedirected || loadedFromFile) if (Console.IsInputRedirected)
{ {
// To simplify reading, we read all input bytes from the piped input to the stream. // To simplify reading, we read all input bytes from the piped input to the stream.
// Not the best way to do it; we don't need to keep any data that has already been read and parsed successfully, // Not the best way to do it; we don't need to keep any data that has already been read.
// and we could avoid copying the supplied stream. But keeps interactivity with the console simple // Whilst the stream could be copied excluding already parsed data at each input step, this would rely
// on GC to cleanup afterwards
sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd())); sourceStream.Write(Encoding.UTF8.GetBytes(Console.In.ReadToEnd()));
// Dispose will close a piped input, or piped file in further iterations of the program
Console.In.Dispose(); Console.In.Dispose();
Console.SetIn(new StreamReader(Console.OpenStandardInput()));
Console.OpenStandardInput();
sourceStream.Position = 0; sourceStream.Position = 0;
} }
else else
{ {
//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.WriteLine(Environment.NewLine + new string('─', 40)); Console.WriteLine(Environment.NewLine + new string('─', 40));
Console.WriteLine("\nProgram Parsed Successfully!"); // Not strictly required, but could have problems if we try loading a large program immediately after unloading the last
// NB: parser is out-of-scope from now
if (Console.IsInputRedirected) GC.Collect();
Console.WriteLine("\nProgram parsing complete.");
if (dynamicInput == false)
{ {
Thread.Sleep(3000); Thread.Sleep(2000);
Environment.Exit(0); Environment.Exit(0);
} }
ConsoleKeyInfo ck = new ConsoleKeyInfo(); ConsoleKeyInfo ck = new();
while (ck.Key != ConsoleKey.Y && ck.Key != ConsoleKey.N) while (ck.Key != ConsoleKey.Y && ck.Key != ConsoleKey.N)
{ {
Console.WriteLine("\nWould you like to parse another program? Y/n:"); Console.WriteLine("\nWould you like to parse another program? Y/n:");
@ -78,17 +90,17 @@ namespace Assignment_1
} }
else else
{ {
ck = new ConsoleKeyInfo(); // Need the logic to prep the next source stream
ck = new();
while (ck.Key != ConsoleKey.Y && ck.Key != ConsoleKey.N) while (ck.Key != ConsoleKey.Y && ck.Key != ConsoleKey.N)
{ {
Console.WriteLine("\nWould you like to pipe data from a source file? Y/n:"); Console.WriteLine("\nWould you like to pipe data from source file? Y/n:");
ck = Console.ReadKey(true); ck = Console.ReadKey(true);
} }
if (ck.Key == ConsoleKey.N) if (ck.Key == ConsoleKey.N)
{ {
// Set the input to standard input stream // Set the input to standard input stream
Console.SetIn(Console.In); Console.SetIn(Console.In);
loadedFromFile = false;
} }
else else
{ {
@ -99,7 +111,6 @@ namespace Assignment_1
try try
{ {
Console.SetIn(File.OpenText(sourcePath)); Console.SetIn(File.OpenText(sourcePath));
loadedFromFile = true;
} }
catch (Exception e) catch (Exception e)
{ {
@ -113,7 +124,7 @@ namespace Assignment_1
public class Parser public class Parser
{ {
Dictionary<string, Tuple<string, VariableFlags>> Symbols = new Dictionary<string, Tuple<string, VariableFlags>>() Dictionary<string, Tuple<string, VariableFlags>> Symbols = new()
{ {
{ "SPACE", new Tuple<string, VariableFlags>(" ", VariableFlags.Reserved | VariableFlags.NoPrint) }, { "SPACE", new Tuple<string, VariableFlags>(" ", VariableFlags.Reserved | VariableFlags.NoPrint) },
{ "TAB", new Tuple<string, VariableFlags>("\t", VariableFlags.Reserved | VariableFlags.NoPrint) }, { "TAB", new Tuple<string, VariableFlags>("\t", VariableFlags.Reserved | VariableFlags.NoPrint) },
@ -133,6 +144,7 @@ namespace Assignment_1
set, set,
reverse, reverse,
h, h,
writeout
} }
public void StartParsing(Stream source, bool dynamicInput = false) public void StartParsing(Stream source, bool dynamicInput = false)
{ {
@ -140,10 +152,9 @@ namespace Assignment_1
long lastLinePos = 0; long lastLinePos = 0;
long initPos = 0; long initPos = 0;
bool cont = false; bool cont = false;
bool isLineFinished = true;
while (true) while (true)
{ {
if (dynamicInput && isLineFinished) if (dynamicInput)
{ {
lastLinePos = source.Position; lastLinePos = source.Position;
if (!cont) if (!cont)
@ -156,6 +167,8 @@ namespace Assignment_1
source.Position = pos; source.Position = pos;
} }
// parse the statement or list of statements;
// This is done by reading the next word
if (!cont) if (!cont)
{ {
initPos = source.Position; initPos = source.Position;
@ -172,6 +185,8 @@ namespace Assignment_1
{ {
// By turning the result of the command into an action, // By turning the result of the command into an action,
// we can defer processing the final result until the end of this control flow // we can defer processing the final result until the end of this control flow
// This simplifies parsing the eol, and prevents the result being processed too
// optimistically.
Action result = () => { }; Action result = () => { };
source.Position = position; source.Position = position;
switch ((Statements)statementType) switch ((Statements)statementType)
@ -212,27 +227,34 @@ namespace Assignment_1
case Statements.reverse: case Statements.reverse:
result = Reverse(source); result = Reverse(source);
break; break;
// These are additional helper functions. Thier input gets excluded from the MemoryStream
case Statements.h: case Statements.h:
Console.WriteLine("Commands are: "); Console.WriteLine("Commands are: ");
foreach (var item in Enum.GetValues(typeof(Statements))) foreach (var item in Enum.GetValues(typeof(Statements)))
{ {
Console.WriteLine("\t{0}", ((Statements)item).ToString()); Console.WriteLine("\t{0}", ((Statements)item).ToString());
} }
// Stream ignores this command // Ignore these as actual commands
source.Position = initPos; source.Position = initPos;
source.SetLength(initPos); source.SetLength(initPos);
break; break;
/*case Statements.writeout:
// Writes the full command history to the stream.
Console.WriteLine("Writing input commands to {0}...");
source.Position = initPos;
source.SetLength(initPos);
break;*/
} }
// Do a check semicolons etc
if (IsNextEoS(source)) if (IsNextEoS(source))
{ {
// Increment the source pos past the semi-colon
cont = false; cont = false;
source.Position++; source.Position++;
SkipWhitespace(source); if (dynamicInput)
if (source.Position == source.Length) isLineFinished = true;
else isLineFinished = false;
if (dynamicInput && isLineFinished)
{ {
source.WriteByte((byte)'\n'); // put the next statement on a newline so the exported list of commands is clean // Nicely format the output stream, so we may print it cleanly
source.WriteByte((byte)'\n');
} }
result(); result();
if (((Statements)statementType).Equals(Statements.exit)) if (((Statements)statementType).Equals(Statements.exit))
@ -240,19 +262,17 @@ namespace Assignment_1
return; return;
} }
} }
else if (source.Position != lastLinePos) else if (source.Position != lastLinePos)// - 1)
{ {
// If the semicolon is missing *once*, assume we need more data. // 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
lastLinePos = source.Position; lastLinePos = source.Position;
cont = true; cont = true;
source.WriteByte((byte)' ');
Console.Write(">"); Console.Write(">");
} }
else else
{ {
if ((Statements)statementType != Statements.h) throw new ParserException("expected a semi-colon", 0, source.Position);
{
throw new ParserException("expected a semi-colon", 0, source.Position);
}
} }
} }
else else
@ -260,12 +280,11 @@ namespace Assignment_1
throw new ParserException("Failed parsing statement", 0, source.Position); 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,
// and the excepion printed to the console window. // and the excepion printed to the console window.
// This means that each function does not need to keep track of our current position in the stream // This means that each function does not need to keep track of our current position in the stream
catch (ParserException e) catch (ParserException e)
{ {
isLineFinished = true;
if (e.Importance > 3) if (e.Importance > 3)
{ {
throw new ApplicationException("A critical error occurred."); throw new ApplicationException("A critical error occurred.");
@ -276,7 +295,9 @@ namespace Assignment_1
} }
else else
{ {
WriteDebugLine(0, 0, e.Message, source); Console.WriteLine(e.LinePosition + ": " + e.Message);
source.Position = initPos;
source.SetLength(initPos);
} }
if (!dynamicInput) if (!dynamicInput)
{ {
@ -286,11 +307,15 @@ namespace Assignment_1
} }
} }
#region Function Handling
/// <summary> /// <summary>
/// Checks if the next expression in the source meets the requirements of being a key, /// Checks if the next expression in the source meets the requirements of being a key,
/// and optionally verify that key exists. Acoids overidding the value of constants, and /// and optionally verify that key exists.
/// ensures all characters are valid /// Also contracts the key is not reserved or constant
/// </summary> /// </summary>
/// <param name="source"></param>
/// <param name="checkExist"></param>
/// <returns></returns>
private string ValidateKey(Stream source, bool checkExist) private string ValidateKey(Stream source, bool checkExist)
{ {
long keyEndPos = FindIdentifier(source, out string key); long keyEndPos = FindIdentifier(source, out string key);
@ -302,16 +327,11 @@ namespace Assignment_1
{ {
throw new ParserException("Key not found", 0, source.Position); throw new ParserException("Key not found", 0, source.Position);
} }
else if (Symbols.ContainsKey(key) && Symbols[key].Item2.HasFlag(VariableFlags.Reserved))
{
throw new ParserException("Cannot assign a value to a reserved constant", 0, keyEndPos - (key.Length + 1));
}
else else
{ {
int indx = Array.FindIndex(key.ToCharArray(), (c) => (c > 122 || c > 90 && c < 97 && c != '_' || c > 57 && c < 65 || c < 48)); if (Symbols.ContainsKey(key) && Symbols[key].Item2.HasFlag(VariableFlags.Reserved))
if (indx > -1)
{ {
throw new ParserException(string.Format("Character \'{0}\' is not valid for an identifier",key[indx]), 0, keyEndPos-key.Length + indx); throw new ParserException("Cannot assign a value to a reserved constant", 0, keyEndPos - (key.Length + 1));
} }
source.Position = keyEndPos; source.Position = keyEndPos;
} }
@ -321,6 +341,8 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Checks if the next expression meets the requirements of being a value /// Checks if the next expression meets the requirements of being a value
/// </summary> /// </summary>
/// <param name="source"></param>
/// <returns></returns>
private string ValidateValue(Stream source) private string ValidateValue(Stream source)
{ {
long valuePos = FindExpression(source, out string value); long valuePos = FindExpression(source, out string value);
@ -336,9 +358,11 @@ namespace Assignment_1
} }
/// <summary> /// <summary>
/// Handles the 'append x y [ + z];' case <br /> /// Handles the 'append x y [ + z];' case &
/// And the 'set x y [ + z];' case /// And the 'set x y [ + z];' case
/// </summary> /// </summary>
/// <param name="source"></param>
/// <returns>An Action that will add the key to the dictionary</returns>
Action AppendSet(Stream source, bool appendMode = true) Action AppendSet(Stream source, bool appendMode = true)
{ {
string key = ValidateKey(source, appendMode); string key = ValidateKey(source, appendMode);
@ -359,21 +383,20 @@ namespace Assignment_1
} }
} }
} }
/// <summary> /// <summary>
/// Creates and prints a nicely formatted table of all values /// Creates and prints a nicely formatted table of all values
/// </summary> /// </summary>
/// <param name="printUnprint">List values normally excluded from printing</param> /// <param name="printUnprint">List values normally excluded from printing</param>
Action List(bool printUnprint = false) Action List(bool printUnprint = false)
{ {
int flagWidth = Math.Max(Enum.GetNames(typeof(VariableFlags)).Length, "Flags".Length); int keyWidth = 21;
int keyWidth = (int)((ConsoleWidthLimit - flagWidth) * 0.2); // 20% - flag width int valueWidth = 50;
int valueWidth = (int)((ConsoleWidthLimit - flagWidth) * 0.8); // 80% - flag width int flagWidth = 9;
StringBuilder consoleOutput = new();
StringBuilder consoleOutput = new StringBuilder();
consoleOutput.Append(string.Format("┌" + new string('─', keyWidth) + "┬" + new string('─', valueWidth) + "┬" + new string('─', flagWidth) + "┐\n")); consoleOutput.Append(string.Format("┌" + new string('─', keyWidth) + "┬" + new string('─', valueWidth) + "┬" + new string('─', flagWidth) + "┐\n"));
consoleOutput.Append(string.Format("│{0}│{1}│{2}│\n", CenterString("Symbol", keyWidth), CenterString("Value", valueWidth), CenterString("Flags", flagWidth))); consoleOutput.Append(string.Format("│{0}│{1}│{2}│\n", CenterString("Symbol", keyWidth), CenterString("Value", valueWidth), CenterString("Flags", flagWidth)));
List<string> eligibleKeys = new List<string>(Symbols.Count); // Figure out how many symbols are eligible for printing
List<string> eligibleKeys = new(Symbols.Count);
foreach (var item in Symbols.Keys) foreach (var item in Symbols.Keys)
{ {
if (!Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) || (Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) && printUnprint)) if (!Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) || (Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) && printUnprint))
@ -381,6 +404,7 @@ namespace Assignment_1
eligibleKeys.Add(item); eligibleKeys.Add(item);
} }
} }
// Control printing based on how many keys are available
if (eligibleKeys.Count > 0) if (eligibleKeys.Count > 0)
{ {
consoleOutput.Append(string.Format("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤\n")); consoleOutput.Append(string.Format("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤\n"));
@ -392,7 +416,7 @@ namespace Assignment_1
for (int j = 0; j < (keyLines.Count > valueLines.Count ? keyLines.Count : valueLines.Count); j++) for (int j = 0; j < (keyLines.Count > valueLines.Count ? keyLines.Count : valueLines.Count); j++)
{ {
consoleOutput.Append(string.Format(entryFormat, j >= keyLines.Count ? "" : keyLines[j], j >= valueLines.Count ? "" : valueLines[j], j == 0 ? Convert.ToString((byte)Symbols[eligibleKeys[i]].Item2, 2).PadLeft(flagWidth, '0'): "")); consoleOutput.Append(string.Format(entryFormat, j >= keyLines.Count ? "" : keyLines[j], j >= valueLines.Count ? "" : valueLines[j], j == 0 ? Convert.ToString((byte)Symbols[eligibleKeys[i]].Item2, 2).PadLeft(8, '0'): ""));
} }
if (i + 1 < eligibleKeys.Count) if (i + 1 < eligibleKeys.Count)
{ {
@ -401,11 +425,9 @@ namespace Assignment_1
} }
} }
consoleOutput.Append(string.Format("└" + new string('─', keyWidth) + "┴" + new string('─', valueWidth) + "┴" + new string('─', flagWidth) + "┘\n")); consoleOutput.Append(string.Format("└" + new string('─', keyWidth) + "┴" + new string('─', valueWidth) + "┴" + new string('─', flagWidth) + "┘\n"));
return () => Console.WriteLine(consoleOutput.ToString()); return () => Console.WriteLine(consoleOutput.ToString());
} }
/// <summary>
/// Exit Application logic
/// </summary>
Action Exit(Stream source, long initialStreamLength, bool isDynamicInput=false) Action Exit(Stream source, long initialStreamLength, bool isDynamicInput=false)
{ {
void exitAction() void exitAction()
@ -426,6 +448,8 @@ namespace Assignment_1
if (path != "") if (path != "")
{ {
path = Path.Combine(Environment.CurrentDirectory, path); path = Path.Combine(Environment.CurrentDirectory, path);
// insert the final closing bracket
//source.WriteByte((byte)'}');
source.Position = 0; source.Position = 0;
using (FileStream fs = File.OpenWrite(path)) using (FileStream fs = File.OpenWrite(path))
{ {
@ -438,17 +462,9 @@ namespace Assignment_1
} }
return exitAction; return exitAction;
} }
/// <summary>
/// Prints the expression to the console:<br />
/// 0: print the value <br />
/// 1: print the length <br />
/// 2: print the word count <br />
/// 3: print the words in the value <br />
/// </summary>
Action Print(Stream source, int mode = 0) Action Print(Stream source, int mode = 0)
{ {
StringBuilder outputString = new StringBuilder(); StringBuilder outputString = new();
string expression = ValidateValue(source); string expression = ValidateValue(source);
if (mode == 0) if (mode == 0)
{ {
@ -479,15 +495,13 @@ namespace Assignment_1
} }
return () => Console.WriteLine(outputString.ToString()); return () => Console.WriteLine(outputString.ToString());
} }
/// <summary>
/// Reverses the word-order of the symbol (in-place).
/// </summary>
Action Reverse(Stream source) Action Reverse(Stream source)
{ {
string key = ValidateKey(source, true); string key = ValidateKey(source, true);
string ToReverse = Symbols[key].Item1; string ToReverse = Symbols[key].Item1;
string[] words = ToReverse.Split(' '); string[] words = ToReverse.Split(' ');
StringBuilder reversed = new StringBuilder(); StringBuilder reversed = new();
for (int i = words.Length - 1; i >= 0; i--) for (int i = words.Length - 1; i >= 0; i--)
{ {
reversed.Append(words[i]); reversed.Append(words[i]);
@ -498,10 +512,14 @@ namespace Assignment_1
} }
/// <summary> /// <summary>
/// Writes the debug info to the screen in the form: <br/> /// Writes the debug info to the screen in the form:<br/>
/// line read from stream (lineStart) to line end <br/> /// line read from stream (lineStart) to line end<br/>
/// &lt;whitespace@caratPos&gt; ^ &lt;errorMessage&gt; /// &lt;whitespace@caratPos&gt; ^ &lt;errorMessage&gt;
/// </summary> /// </summary>
/// <param name="lineStart"></param>
/// <param name="caratPos"></param>
/// <param name="errorMessage"></param>
/// <param name="source"></param>
static void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source) static void WriteDebugLine(long lineStart, long caratPos, string errorMessage, Stream source)
{ {
source.Position = lineStart; source.Position = lineStart;
@ -512,13 +530,19 @@ namespace Assignment_1
source.Position = lineStart; source.Position = lineStart;
source.SetLength(source.Position); source.SetLength(source.Position);
} }
#endregion
#region Data Handling
// Data Handling
/// <summary> /// <summary>
/// Parses & evaluates the expression from the stream, moving the stream to the end of the last value /// Parses & evaluates the expression from the stream, moving the stream to the end of the last value
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="expression"></param>
/// <returns></returns>
long FindExpression(Stream s, out string expression) long FindExpression(Stream s, out string expression)
{ {
string result = ""; string result = "";
// iterate through values until we reach either the end of the stream or the end-of-statement
bool IsAppendSet = true; bool IsAppendSet = true;
while (s.Position < s.Length && !IsNextEoS(s)) while (s.Position < s.Length && !IsNextEoS(s))
{ {
@ -544,14 +568,17 @@ namespace Assignment_1
{ {
throw new ParserException("Append operator not set", 0, s.Position); throw new ParserException("Append operator not set", 0, s.Position);
} }
} }
} }
expression = result; expression = result;
return s.Position; return s.Position;
} }
/// <summary> /// <summary>
/// Checks ahead to see if the next non-whitespace character is the EoS indicator /// Checks ahead to see if the next non-whitespace character is the EoS indicator (';')
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="EoSChar"></param>
/// <returns>true if the next char is <paramref name="EoSChar"/>, else false</returns> /// <returns>true if the next char is <paramref name="EoSChar"/>, else false</returns>
static bool IsNextEoS(Stream s, char EoSChar = ';') static bool IsNextEoS(Stream s, char EoSChar = ';')
{ {
@ -567,8 +594,11 @@ namespace Assignment_1
} }
/// <summary> /// <summary>
/// Finds the next value expression in the stream /// Finds the next value in the stream
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="returnedValue"></param>
/// <returns></returns>
long FindValue(Stream s, out string returnedValue) long FindValue(Stream s, out string returnedValue)
{ {
SkipWhitespace(s); SkipWhitespace(s);
@ -581,21 +611,25 @@ namespace Assignment_1
else else
{ {
long t = FindExistingIdentifier(s, out string keyValue); long t = FindExistingIdentifier(s, out string keyValue);
// Set the key value to result + this read string
//keyValue = result + keyValue;
if (!Symbols.ContainsKey(keyValue)) if (!Symbols.ContainsKey(keyValue))
{ {
throw new ParserException("Could not find key: " + keyValue, 0, s.Position); throw new ParserException("Could not find key: " + keyValue, 0, s.Position);
} }
returnedValue = Symbols[keyValue].Item1; returnedValue = Symbols[keyValue].Item1;
return t; return t;
} }
} }
static long FindIdentifier(Stream s, out string returnedKey) long FindIdentifier(Stream s, out string returnedKey)
{ {
long wordEnd = FindNextWord(s, out returnedKey); long wordEnd = FindNextWord(s, out returnedKey);
return wordEnd; return wordEnd;
} }
static long FindExistingIdentifier(Stream s, out string returnedKey) long FindExistingIdentifier(Stream s, out string returnedKey)
{ {
long wordEnd = FindNextWord(s, out string identifier); long wordEnd = FindNextWord(s, out string identifier);
if (identifier.Length > 1 && identifier.EndsWith(';')) if (identifier.Length > 1 && identifier.EndsWith(';'))
@ -605,6 +639,7 @@ namespace Assignment_1
wordEnd--; wordEnd--;
s.Position--; s.Position--;
} }
// Lookup the value in the symbol table
returnedKey = identifier; returnedKey = identifier;
return wordEnd; return wordEnd;
} }
@ -612,7 +647,10 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Finds the end of the complete literal definition, returning the stream to the original position /// Finds the end of the complete literal definition, returning the stream to the original position
/// </summary> /// </summary>
static long FindLiteral(Stream s, out string returnedLiteral) /// <param name="s"></param>
/// <param name="returnedLiteral"></param>
/// <returns></returns>
long FindLiteral(Stream s, out string returnedLiteral)
{ {
long pos = s.Position; long pos = s.Position;
// Is a literal. Now we must parse until we find the end of the literal // Is a literal. Now we must parse until we find the end of the literal
@ -622,16 +660,14 @@ namespace Assignment_1
{ {
if (c == '\"') if (c == '\"')
{ {
s.Position--; long pos = s.Position--;
if (PreviousChar(s) == '\\') if (ReadChar(s) == '\\')
{ {
// TODO: handle the \\ escape // TODO: handle the \\ escape
s.Position++;
return false; return false;
} }
else else
{ {
s.Position++;
return true; return true;
} }
} }
@ -639,7 +675,7 @@ namespace Assignment_1
}, out string resultLiteral); }, out string resultLiteral);
if (resultPosition > -1) if (resultPosition > -1)
{ {
returnedLiteral = resultLiteral.Replace("\\\"", "\""); returnedLiteral = resultLiteral;
} }
else else
{ {
@ -648,11 +684,16 @@ namespace Assignment_1
s.Position = pos; s.Position = pos;
return resultPosition; return resultPosition;
} }
#endregion
} }
#region HelperFunctions
/// <summary> /// <summary>
/// Reads the memory stream as a UTF-8 encoded string until the next occurance of '\n' or '\r\n' (consuming, and excluded) /// Reads the memory stream as a UTF-8 encoded string until the next occurance of '\n' or '\r\n' (consuming, and excluded)
/// </summary> /// </summary>
/// <param name="s"></param>
/// <returns></returns>
static string GetNextLine(Stream s) static string GetNextLine(Stream s)
{ {
FindNextOccurance(s, '\n', out string nextLine); FindNextOccurance(s, '\n', out string nextLine);
@ -662,15 +703,26 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Finds the end-boundary of the next word in the stream, and returns the stream to the original position /// Finds the end-boundary of the next word in the stream, and returns the stream to the original position
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="nextWord"></param>
/// <returns></returns>
static long FindNextWord(Stream s, out string nextWord) static long FindNextWord(Stream s, out string nextWord)
{ {
StringBuilder newWord = new StringBuilder(); StringBuilder newWord = new();
// Record our current position
long start = s.Position; long start = s.Position;
// Check if the character at the current pos is whitespace, if so, keep advancing until it isn't.
// NB: Whitespace includes carriage returns and line feeds,
// so 'set\r\n
// var
// "expression";
// should be valid
char currentChar = ReadChar(s); char currentChar = ReadChar(s);
while (s.Position < s.Length && char.IsWhiteSpace(currentChar)) while (s.Position < s.Length && char.IsWhiteSpace(currentChar))
{ {
currentChar = ReadChar(s); currentChar = ReadChar(s);
} }
// Add the last read value to the SB
newWord.Append(currentChar); newWord.Append(currentChar);
// Start a second loop, this time checking we're not a whitespace char // Start a second loop, this time checking we're not a whitespace char
while (s.Position < s.Length) while (s.Position < s.Length)
@ -697,6 +749,10 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Finds and returns the position of the next occurance of the Func returning true. /// Finds and returns the position of the next occurance of the Func returning true.
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="p">A 'predicate'-like Func</param>
/// <param name="result">Returns the string captured while searching for the next char</param>
/// <returns></returns>
static long FindNextOccurance(Stream s, Func<char, Stream, bool> p, out string result) static long FindNextOccurance(Stream s, Func<char, Stream, bool> p, out string result)
{ {
long start = s.Position; long start = s.Position;
@ -726,16 +782,21 @@ namespace Assignment_1
} }
/// <summary> /// <summary>
/// Finds the next position of the supplied character /// Finds the next position of the character
/// </summary> /// </summary>
/// <param name="s"></param>
/// <param name="c"></param>
/// <param name="result">Captures the string read in searching for the character</param>
/// <returns></returns>
static long FindNextOccurance(Stream s, char c, out string result) static long FindNextOccurance(Stream s, char c, out string result)
{ {
return FindNextOccurance(s, (streamChar, s) => streamChar == c, out result); return FindNextOccurance(s, (streamChar, s) => streamChar == c, out result);
} }
/// <summary> /// <summary>
/// Reads the next UTF-8 encoded character in the stream, and advances the stream by the amount of characters read /// Reads the next UTF-8 encoded character in the stream, and advances the stream by the amount of characters read
/// </summary> /// </summary>
/// <param name="s"></param>
/// <returns></returns>
static char ReadChar(Stream s) static char ReadChar(Stream s)
{ {
// As UTF-8 allows codepoints to span multiple bytes, reading a single byte as a character will not always give the expected // As UTF-8 allows codepoints to span multiple bytes, reading a single byte as a character will not always give the expected
@ -772,10 +833,11 @@ namespace Assignment_1
string converted = Encoding.UTF8.GetString(charBytes); string converted = Encoding.UTF8.GetString(charBytes);
return converted[0]; return converted[0];
} }
/// <summary> /// <summary>
/// Reads the next character in the stream, and returns the position to the original position /// Reads the next character in the stream, and returns the position to the original position
/// </summary> /// </summary>
/// <param name="s"></param>
/// <returns></returns>
static char PeekChar(Stream s) static char PeekChar(Stream s)
{ {
long curr = s.Position; long curr = s.Position;
@ -787,6 +849,8 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Reads the previous char /// Reads the previous char
/// </summary> /// </summary>
/// <param name="s"></param>
/// <returns></returns>
static char PreviousChar(Stream s) static char PreviousChar(Stream s)
{ {
Stack<byte> charBytes = new Stack<byte>(4); Stack<byte> charBytes = new Stack<byte>(4);
@ -809,12 +873,13 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Skips whitespace characters /// Skips whitespace characters
/// </summary> /// </summary>
/// <param name="s"></param>
static void SkipWhitespace(Stream s) static void SkipWhitespace(Stream s)
{ {
char c = PeekChar(s); char c = PeekChar(s);
while (s.Position < s.Length && char.IsWhiteSpace(c)) while (s.Position < s.Length && char.IsWhiteSpace(c))
{ {
ReadChar(s); ReadChar(s); // move by the size of that character
c = PeekChar(s); c = PeekChar(s);
} }
} }
@ -832,7 +897,7 @@ namespace Assignment_1
static List<string> GetStringLines(string source, int maxWidth) static List<string> GetStringLines(string source, int maxWidth)
{ {
List<string> lines = new List<string>(); List<string> lines = new();
int j = 0; int j = 0;
while (j < source.Length) while (j < source.Length)
{ {
@ -842,12 +907,9 @@ namespace Assignment_1
} }
return lines; return lines;
} }
#endregion
public class ParserException : Exception public class ParserException : Exception
{ {
/// <summary>
/// Custom expression that will also pass the LinePosition & importance of the error (not implemented)
/// </summary>
public int Importance = 0; public int Importance = 0;
public long LinePosition = -1; public long LinePosition = -1;
public ParserException(string message, int importance, long linePos) : base(message) public ParserException(string message, int importance, long linePos) : base(message)

View File

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

View File

@ -1,49 +0,0 @@
set a1 "
To be, or not to be - that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles,
And by opposing end them. To die - to sleep -
No more; and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wish'd. To die - to sleep.
To sleep - perchance to dream: ay, there's the rub!
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause. There's the respect
That makes calamity of so long life.
For who would bear the whips and scorns of time,
The oppressor's wrong, the proud man's contumely,
The pangs of despised love, the law's delay,
The insolence of office, and the spurns
That patient merit of the unworthy takes,
When he himself might his quietus make" + NEWLINE +
"With a bare bodkin?
";
set averylongvariablethathasbeencontrivedjusttobeanuisancetothe341classwhohaveprobablythoughtthattheycouldgetawaywithallocatingshortvariablenamesofashortlengthbutaregoingtobereallydisappointediftheydidbecausethisisnotashortvariableidentifieratall "
Who would these fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death -
The undiscover'd country, from whose bourn
No traveller returns - puzzles the will,
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience does make cowards of us all,
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry
And lose the name of action.";
set soliloquy a1 + NEWLINE + averylongvariablethathasbeencontrivedjusttobeanuisancetothe341classwhohaveprobablythoughtthattheycouldgetawaywithallocatingshortvariablenamesofashortlengthbutaregoingtobereallydisappointediftheydidbecausethisisnotashortvariableidentifieratall;
set dummy "dummy1";
print soliloquy;
printwordcount soliloquy;
printwords soliloquy;
printlength soliloquy;
list;
exit;

View File

@ -1,64 +1,5 @@
# 159.341 Assignment 1 - String Interpreter [![Build status](https://ci.software.kauripeak.co.nz/api/projects/status/44sgyt4dadc2il99?svg=true)](https://ci.software.kauripeak.co.nz/project/AppVeyor/159-341-assignment-1) [![Build status](https://ci.software.kauripeak.co.nz/api/projects/status/44sgyt4dadc2il99?svg=true)](https://ci.software.kauripeak.co.nz/project/AppVeyor/159-341-assignment-1)
## A basic interpreter for a simple programming language. # 159.341 Assignment 1 - String Interpreter
There is only one implicit type definition - strings. A basic JIT interpreter for a simple, custom programming language.
There exists only one implicit type definition - strings.
<br>
This program is written in C# (.NET Core 3.0*) and specifically avoids the use of the `regex` class and the
```cs
string.split()
```
method to show an understanding of the implementation.
\* *.NET Core 3.0 **should** be supported on the lab machines*
<br>
The application accepts piped input streams via `StreamReader` and piped input via `type <file> | <application>`
There are preconfigured build options that will automatically pipe the example files:
[![VS Build Configuration](https://git.software.kauripeak.co.nz/content/upload/159.341/159-341-assignment-1-build-configuration.png)](https://git.software.kauripeak.co.nz/content/upload/159.341/159-341-assignment-1-build-configuration.png)
These examples are automatically copied to the output directory at build.
<br>
This stream is navigated character-by-character (not byte-by-byte)
Example usage:
```py
set somevar "NewValue";
set othervar somevar + SPACE + ";";
list;
exit;
```
Would output:
```
┌─────────────┬──────────────────────┬─────────┐
│ Symbol │ Value │ Flags │
├─────────────┼──────────────────────┼─────────┤
│somevar │NewValue │00000000 │
├─────────────┼──────────────────────┼─────────┤
│othervar │NewValue ; │00000000 │
└─────────────┴──────────────────────┴─────────┘
```
## Additional Parameters | Behaviours
* The option `list;` may be augmented with the parameter `all` (i.e. `list all;`), which will additionally list the constant/global symbols.
* It is possible to save the command list on `exit;`; this is prompted and guided.
* The program instances its operation; at `exit;` you will have the option to load a new file or write a new program.
* A quick help option is available by using the command `h` (semi-colon not required).
**NB:**
* A double quote can be inserted into the literal by using a backslash immediately beforehand (i.e. `\"`)
```cs
set someVar "this literal \" has a value";
```
prints
`this literal " has a value`