// Assignment 1 (C++).cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include #include #include 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 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 symbolNames; std::vector 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) { } } } };