<aside> <img src="/icons/code_blue.svg" alt="/icons/code_blue.svg" width="40px" /> Name: Westly Bouchard CSCI 200 Section: F

Project Title: Mash (Mini - Bash)

</aside>

<aside> ⚠️ This PDF was generated from a notion document, so some formatting items and links may not function as expected. For an optimal experience, please view the web version: https://wcbouchard.notion.site/Mash-Mini-Bash-6ac7d512f7d44a979ff949cee4f2b052?pvs=4

</aside>

Table of Contents

Problem Description

I have often found myself wanting to automate small tasks around my computer, however I have never really found an easy enough way to do this (to me at least) that I actually go about implementing the automations. For this project I wanted to try my hand at writing an interpreter for a small language of my own design. Specifically, a language that feels kind of like c++ or java, but that has built in functionality for executing commands in the terminal and interacting with the filesystem. While these things are possible in c++ and java, I also wanted my language to be a scripting language in order for it to be easier to use.

An example program

The language I will design will be able to perform simple tasks such as the ones in the following example program

// This program will first create a directory called `files`, it will then
// cd into that directory and create three files inside. Finally, it will use
// ls to check that the files were created successfully.
int numFiles = 3;
string baseName = "file";

string cmd = "mkdir files";

exec(cmd);

exec("cd files");

int counter = 0;

for (numFiles) {
    echo("Creating file number " + (counter + 1));
    exec("touch " + baseName + (counter + 1));
    counter = counter + 1
}

echo("Verifying command success with 'ls'");
string result;
exec("ls", result);

if (result == "") {
    echo("File creation failed, ls returned nothing");
} else {
    echo("File creation successful");
}

This example program showcases essentially all components of Mash. Mash supports four data types:

Name Keyword Description
Integer int A whole number, with no fractional part
Double double A number that may contain a fractional part (i.e. can have a decimal point and numbers after it)
String string An array of characters, can be one character long in lieu of a dedicated character type
Boolean boolean A value that is either true or false

Additionally, Mash supports basic control flow and looping constructs such as if else forand while. Finally, Mash includes two standard library functions exec() and echo(). Exec will execute the command it is passed as its parameter and, if provided, will store the result of the command in the second parameter given. Echo will simply print the parameter is given to the standard output stream.

Mash is an incredibly small programming language, with very few keywords and very basic functionality, however I think this is an adequate challenge for a final project, and I believe it will allow me to put my computer science skills to use.

Data Description

Mash will include many classes, some of which I have not completely designed or thought through yet. However, the core classes that provide the main dataflow for Mash are described below.

Also, it seems that the program I am using to render the UML diagrams does not actually render the text to UML when I export this document as a PDF. To view the fully rendered UML diagrams for a given section, please refer to the subsection of Appendix B with the same name. Thanks!

Scanning and Tokenization

The scanner is the first step in the interpretation process, this class takes in the raw source code and produces a sequence of tokens that represent the source file.

classDiagram
	direction RL
	class Scanner {
		- source: std::ifstream&

		- peekNext() char
		- advance() char
		
		+ Scanner(std::ifstream& source)
		+ scanTokens() std::vector~Token~
	}

When constructed, the scanner takes in a reference to an input stream to read characters from. The private methods peekNext() and advance() are used to traverse through the stream and to look ahead to the next, and second next character in the stream. The main method from Scanner is the scanTokens() method, this invokes the scanning process and returns a vector of tokens from its source characters.

Tokens and Token Types