<commit_before># SLogo: Design Document

## Introduction
For this project, we are creating a interactive development environment that supports various Simple Logo commands. We will be using a Model-View-Controller design framework, with separate GUI, Controller, and Parser classes that work together to receive user input, interpret/parse the input and check its validity, and perform whatever action(s) given the parameter(s). The frontend and backend will be connected together through the controller, and will be restricted on which methods they have access to through use of interfaces. By doing so, the GUI and parser will have minimal interference with each other and will each be closed for modification (as much as possible). The GUI and parser will each have their own unique responsibilities, such as changing the position of a turtle visually or interpreting a command with its given parameters respectively, and these changes will be communicated across through method calls in the controller. Each possible command will extend an abstract parent class and have its own properties associated with the command, and thus adding new commands in SLogo will be open for extension. We will also utilize reflection within our parser while checking for valid commands and parameters, and also we plan to use binding properties in order to keep the locations of each turtle aligned between the GUI and backend coordinates.

## Overview

##### Front-End+Controller: External API

``void addTurtle(Turtle turtle)`` - adds a Turtle to the List of turtles stored in the controller <br>
``Turtle getTurtle(int turtleId)`` - returns a Turtle from the List of turtles specified by the given id <br>
``void moveTurtle(int turtleId, SlogoPath path)`` - calls the moveTurtle method in the GUI <br>
``void updateHistory(int turtleId, String command)`` - adds to the List of past commands

##### Front-End+Controller: Internal API

The internal API of the front end allows for extension through creation GUI components that can be placed in the main layout, as well as methods for manipulating the turtle’s display parameters.

``Node returnNodeToDraw()`` - returns a ``Node`` to draw depending on what new component will be drawn on the screen <br>
``Image getImage()`` - gets the turtle’s display image <br>
``void setImage(Image image)`` - sets the turtle’s display image <br>
``double getXOnGrid()`` - gets the turtle’s x-position <br>
``void setXOnGrid(double x)`` - sets the turtle’s x-position <br>
``double getYOnGrid()`` - gets the turtle’s y-position <br>
``void setYOnGrid(double y)`` - sets the turtle’s y-position <br>
``void setWidth(double width)`` - sets the turtle’s pen width <br>
``Color getPenColor()`` - gets the turtle’s pen color <br>
``void setPenColor(Color penCol)`` - sets the turtle’s pen color <br>
``double getWidth()`` - gets the turtle’s pen width <br>
``double getAngle()`` - sets the turtle’s heading angle <br>
``boolean getIsVisible()`` - gets if the turtle is visible in the GUI 

#####Front-End+Controller Main Classes:

``MainGUI`` - the outermost GUI element that holds all other GUI elements and variables <br>
``GUIComponent`` - an abstract class that all other SLogo GUI elements extend <br>
``GUIConsole`` - the GUI element that displays and allows user input and sends that input to the SlogoController <br>
``GUIHistory`` - the GUI element that displays the command history and allows the user to interact by clicking which calls the ``SlogoController`` <br>
``GUIPalette`` - an abstract GUI element that displays the color chooser for different parts of the GUI and listens for the user’s clicks <br>
``GUIToolbar`` - the GUI element that holds the buttons at the top of the screen to configure the program <br>
``GUITurtleArea`` - the GUI element that displays the turtle and its path determened by the SlogoController <br>
``GUITurtle`` - a representation of the turtle which is stripped down to parts that only the GUI needs to know about <br>
``GUIController`` - a representation of the Slogo controller which is stripped down to parts that only the GUI needs to know about <br>
``SLogoLanguage`` - just holds a string but allows for more control over pointer accesses <br>
``SlogoController`` - provides the interface through which the front and back ends communicate <br>


###GUI UML Diagram

![GUI UML Diagram]
(sampleUML.png)




##### Back-End: External API
The external API for the backend will be fairly limited, and will be accessed through the SlogoController class’s myParser link. myParser will be a link to the SlogoParser class through the ParserInterface interface. This interface will provide the following methods:

``runCommand(String command)`` - executes command and adds it to the history, throws ParserException if the command is unable to be executed. <br>
``runCommand(CommandString command)`` - executes command and adds it to the history (used to execute previously executed commands) <br>
``setLanguage(String language)`` - sets the current input language to the resource bundle specified by language. If the string is invalid, throws a ``ParserException``

#####Back-End: Internal API
The internal API for the backend will be more robust, providing functionality for adding new commands. It will consist of several classes, which can be extended to add new functionality.

### Back-End UML Diagram:
![Backend UML Diagram]
(SlogoParser.jpg)

##### Main Classes

#####**SlogoParser.java** implements ParserInterface
	
This will be the primary class responsible for implementing back-end functionality. It will maintain a link to the Controller and will be responsible for interacting with the Controller and the GUI. It will provide several functions:
	
```addTurtle(int turtleId)``` - creates a new turtle, if one does not already exist
```setTurtle(int turtleId)``` - sets the current turtle
```getCurrentTurtle()``` - returns a reference to the current turtle
```getTurtles()``` - returns a list of all turtles

```moveCurrentTurtle(SlogoPath path)``` - moves the current turtle using path
```moveTurtle(int turtleId, SlogoPath path)``` - moves the specified turtle using path

#####**CommandString.java**
	
This is an interface which is passed to the GUI to access ```CommandMap``` objects - allows them to be accessed without the GUI executing them or modifying their contents by accident.

```getFormattedString()``` - returns formatted string
```getNativeString()``` - returns native formatted string

#####**CommandInterpreter.java**
	
Class which actually executes commands. Iterates through a ```CommandMap```, creating new ```Evaluable``` objects as necessary. Uses reflection to instantiate objects, throwing a ```ParserException``` if a class is not found.

```public void prepareMap (CommandMap map) throws ParserException``` - steps through map and builds its Evaluation list.

```public void evaluateMap(CommandMap map) throws ParserException``` - executes map.

```public static Evaluable reflect(String command, CommandMap map) throws ParserException``` - returns a new ```Evaluable``` instance based on command, providing map as an argument.

#####**ParserResource.java**

Helper class which interacts with resource files.

```static getNativeCommand(String command)``` - returns a native version of command. Throws exception if it wasn’t found.

```static setCurrentLanguage(String language)``` - sets the current language, throwing exception if resource not found.

#####**Argument.java** implements Evaluable

Abstract class for all primitives, shapes, colors, symbols, etc which a user may type. Each subclass of ```Argument``` implements a specific type of element, providing functions to convert it into native class names.

#####**Primitive.java** extends Argument implements Evaluable

Wrapper class for primitive types (numbers, strings, etc) which is created via Reflection and allows them to be evaluated.

```public static String getNativeText(String primitiveString)``` - returns primitiveString - implemented to make abstraction possible with other types of arguments.

#####**Symbol.java** extends Argument implements Evaluable

Wrapper class for symbols (i.e. [, (, etc) which may be in code 

```public static String getNativeText(String symbolString)``` - returns the class name associated with this symbol, for building a native string map.

#####**Command.java** implements Executable, Evaluable
	
Abstract parent class for all commands. Implements ```Executable``` and ```Evaluable``` interfaces.

Each concrete command class which extends this parent class will be responsible for knowing how many arguments of type ```Evaluable``` it takes. Abstract child classes for each category of commands will be created (i.e. ```TurtleCommand```, ```MoveCommand```, etc). By implementing each type of command in a category, each command can specify what type of commands it will accept as arguments. For example, while ```fd 50``` returns 50, ```fd fd 50``` throws an exception in an online logo interpreter, suggesting that “fd 50” is not a valid way to pass 50 to the first fd command.

Each command will be contained in its own class, and will implement the ```execute()``` method, responsible for running the command. Each command will also have a ```getRemainder()``` function, which will return unused portions of its CommandMap, allowing them to be evaluated separately.

```public Command(CommandMap map)``` - creates a new Command object with the CommandMap map.

```public static String getNativeString(String command)``` - returns a native string for this command, using ParserResource class to help.

```public double evaluate() throws ParserException``` - calls execute() on the command and returns any numeric value from the command.

```public void execute() throws ParserException``` - runs the command.

#####**TurtleCommand/GUICommand** etc etc
Parent classes for each category of command.

#####**CommandMap.java** implements Evaluable, CommandString

Class used to parse commands - links every element of a command with its corresponding ```SyntaxElement``` type. Impelements ```CommandString``` interface to pass to GUI.. Also builds a ```List<Evaluable>``` which can be evaluated, once the ```CommandInterpreter``` has determined that the ```CommandMap``` is valid. Useful also for defining user commands, which are stored as a command map which has already been evaluated.

```public CommandMap(String command) throws ParserException``` - parses command into its corresponding Class pairs. Builds map. Throws Exception if the process failed.

```public void addEvaluable(Evaluable e)``` - adds e to the next position in the Evaluation list.

```public CommandMap subMap(int startPos)``` - returns a new CommandMap starting at the specified position. Used by Executable objects to return the remainder of their CommandString for evaluation.

```public String getFirst()``` - returns the first element of the map in String form for reflecting.

```public boolean isEmpty()``` - returns true if map is empty, false otherwise.

```public void evaluate() throws ParserException ```- iterates through Evaluation list, throwing an exception if anything fails to evaluate. Does nothing if evaluation list has not been defined (as in the case of a submap).

#####**SyntaxElement.java**
	
Helper class used to assist with code formatting and addition of new syntax elements. Defines several constants, used to map to types of syntax elements (for code highlighting). Each of these constants corresponds to a class string instance which can be used to reflect and create a new instance of the class for each element. New syntactical elements can be added in this manner.

```SyntaxElement.COMMAND
SyntaxElement.USERCOMMAND
SyntaxElement.VARIABLE
SyntaxElement.SYMBOL
SyntaxElement.PRIMITIVE
SyntaxElement.COLOR
SyntaxElement.SHAPE```

```static Color getColor(String element)``` - returns a Color object for the given element.

#####**Regex.java**

Helper class used by other Parser classes. As of now, there are no public methods (it is used internally by ```CommandMap``` and other classes, however).


#####**ParserResource.java**

Helper class which converts raw command strings to their native class names by interacting with 

```public static String getNativeString(String command) throws ParserException``` - returns native String name for command or throws an exception.

```public static void setLanguage(String language) throws ParserException``` - sets language to language or throws exception.

#####**Evaluable.java**
Interface which specifies the following functions:
```public double evaluate()``` - evaluates this command and returns the double result.

#####**Executable.java** extends Evaluable
Interface which specifies the following functions:

```public double execute()``` - runs the current command and returns the result of its evaluation. Used for commands which can be executed, as opposed to just evaluated. Calling execute actually modifies state.<commit_msg>fixed bug in syntaxelement code<commit_after># SLogo: Design Document

## Introduction
For this project, we are creating a interactive development environment that supports various Simple Logo commands. We will be using a Model-View-Controller design framework, with separate GUI, Controller, and Parser classes that work together to receive user input, interpret/parse the input and check its validity, and perform whatever action(s) given the parameter(s). The frontend and backend will be connected together through the controller, and will be restricted on which methods they have access to through use of interfaces. By doing so, the GUI and parser will have minimal interference with each other and will each be closed for modification (as much as possible). The GUI and parser will each have their own unique responsibilities, such as changing the position of a turtle visually or interpreting a command with its given parameters respectively, and these changes will be communicated across through method calls in the controller. Each possible command will extend an abstract parent class and have its own properties associated with the command, and thus adding new commands in SLogo will be open for extension. We will also utilize reflection within our parser while checking for valid commands and parameters, and also we plan to use binding properties in order to keep the locations of each turtle aligned between the GUI and backend coordinates.

## Overview

##### Front-End+Controller: External API

``void addTurtle(Turtle turtle)`` - adds a Turtle to the List of turtles stored in the controller <br>
``Turtle getTurtle(int turtleId)`` - returns a Turtle from the List of turtles specified by the given id <br>
``void moveTurtle(int turtleId, SlogoPath path)`` - calls the moveTurtle method in the GUI <br>
``void updateHistory(int turtleId, String command)`` - adds to the List of past commands

##### Front-End+Controller: Internal API

The internal API of the front end allows for extension through creation GUI components that can be placed in the main layout, as well as methods for manipulating the turtle’s display parameters.

``Node returnNodeToDraw()`` - returns a ``Node`` to draw depending on what new component will be drawn on the screen <br>
``Image getImage()`` - gets the turtle’s display image <br>
``void setImage(Image image)`` - sets the turtle’s display image <br>
``double getXOnGrid()`` - gets the turtle’s x-position <br>
``void setXOnGrid(double x)`` - sets the turtle’s x-position <br>
``double getYOnGrid()`` - gets the turtle’s y-position <br>
``void setYOnGrid(double y)`` - sets the turtle’s y-position <br>
``void setWidth(double width)`` - sets the turtle’s pen width <br>
``Color getPenColor()`` - gets the turtle’s pen color <br>
``void setPenColor(Color penCol)`` - sets the turtle’s pen color <br>
``double getWidth()`` - gets the turtle’s pen width <br>
``double getAngle()`` - sets the turtle’s heading angle <br>
``boolean getIsVisible()`` - gets if the turtle is visible in the GUI 

#####Front-End+Controller Main Classes:

``MainGUI`` - the outermost GUI element that holds all other GUI elements and variables <br>
``GUIComponent`` - an abstract class that all other SLogo GUI elements extend <br>
``GUIConsole`` - the GUI element that displays and allows user input and sends that input to the SlogoController <br>
``GUIHistory`` - the GUI element that displays the command history and allows the user to interact by clicking which calls the ``SlogoController`` <br>
``GUIPalette`` - an abstract GUI element that displays the color chooser for different parts of the GUI and listens for the user’s clicks <br>
``GUIToolbar`` - the GUI element that holds the buttons at the top of the screen to configure the program <br>
``GUITurtleArea`` - the GUI element that displays the turtle and its path determened by the SlogoController <br>
``GUITurtle`` - a representation of the turtle which is stripped down to parts that only the GUI needs to know about <br>
``GUIController`` - a representation of the Slogo controller which is stripped down to parts that only the GUI needs to know about <br>
``SLogoLanguage`` - just holds a string but allows for more control over pointer accesses <br>
``SlogoController`` - provides the interface through which the front and back ends communicate <br>


###GUI UML Diagram

![GUI UML Diagram]
(sampleUML.png)




##### Back-End: External API
The external API for the backend will be fairly limited, and will be accessed through the SlogoController class’s myParser link. myParser will be a link to the SlogoParser class through the ParserInterface interface. This interface will provide the following methods:

``runCommand(String command)`` - executes command and adds it to the history, throws ParserException if the command is unable to be executed. <br>
``runCommand(CommandString command)`` - executes command and adds it to the history (used to execute previously executed commands) <br>
``setLanguage(String language)`` - sets the current input language to the resource bundle specified by language. If the string is invalid, throws a ``ParserException``

#####Back-End: Internal API
The internal API for the backend will be more robust, providing functionality for adding new commands. It will consist of several classes, which can be extended to add new functionality.

### Back-End UML Diagram:
![Backend UML Diagram]
(SlogoParser.jpg)

##### Main Classes

#####**SlogoParser.java** implements ParserInterface
	
This will be the primary class responsible for implementing back-end functionality. It will maintain a link to the Controller and will be responsible for interacting with the Controller and the GUI. It will provide several functions:
	
```addTurtle(int turtleId)``` - creates a new turtle, if one does not already exist
```setTurtle(int turtleId)``` - sets the current turtle
```getCurrentTurtle()``` - returns a reference to the current turtle
```getTurtles()``` - returns a list of all turtles

```moveCurrentTurtle(SlogoPath path)``` - moves the current turtle using path
```moveTurtle(int turtleId, SlogoPath path)``` - moves the specified turtle using path

#####**CommandString.java**
	
This is an interface which is passed to the GUI to access ```CommandMap``` objects - allows them to be accessed without the GUI executing them or modifying their contents by accident.

```getFormattedString()``` - returns formatted string
```getNativeString()``` - returns native formatted string

#####**CommandInterpreter.java**
	
Class which actually executes commands. Iterates through a ```CommandMap```, creating new ```Evaluable``` objects as necessary. Uses reflection to instantiate objects, throwing a ```ParserException``` if a class is not found.

```public void prepareMap (CommandMap map) throws ParserException``` - steps through map and builds its Evaluation list.

```public void evaluateMap(CommandMap map) throws ParserException``` - executes map.

```public static Evaluable reflect(String command, CommandMap map) throws ParserException``` - returns a new ```Evaluable``` instance based on command, providing map as an argument.

#####**ParserResource.java**

Helper class which interacts with resource files.

```static getNativeCommand(String command)``` - returns a native version of command. Throws exception if it wasn’t found.

```static setCurrentLanguage(String language)``` - sets the current language, throwing exception if resource not found.

#####**Argument.java** implements Evaluable

Abstract class for all primitives, shapes, colors, symbols, etc which a user may type. Each subclass of ```Argument``` implements a specific type of element, providing functions to convert it into native class names.

#####**Primitive.java** extends Argument implements Evaluable

Wrapper class for primitive types (numbers, strings, etc) which is created via Reflection and allows them to be evaluated.

```public static String getNativeText(String primitiveString)``` - returns primitiveString - implemented to make abstraction possible with other types of arguments.

#####**Symbol.java** extends Argument implements Evaluable

Wrapper class for symbols (i.e. [, (, etc) which may be in code 

```public static String getNativeText(String symbolString)``` - returns the class name associated with this symbol, for building a native string map.

#####**Command.java** implements Executable, Evaluable
	
Abstract parent class for all commands. Implements ```Executable``` and ```Evaluable``` interfaces.

Each concrete command class which extends this parent class will be responsible for knowing how many arguments of type ```Evaluable``` it takes. Abstract child classes for each category of commands will be created (i.e. ```TurtleCommand```, ```MoveCommand```, etc). By implementing each type of command in a category, each command can specify what type of commands it will accept as arguments. For example, while ```fd 50``` returns 50, ```fd fd 50``` throws an exception in an online logo interpreter, suggesting that “fd 50” is not a valid way to pass 50 to the first fd command.

Each command will be contained in its own class, and will implement the ```execute()``` method, responsible for running the command. Each command will also have a ```getRemainder()``` function, which will return unused portions of its CommandMap, allowing them to be evaluated separately.

```public Command(CommandMap map)``` - creates a new Command object with the CommandMap map.

```public static String getNativeString(String command)``` - returns a native string for this command, using ParserResource class to help.

```public double evaluate() throws ParserException``` - calls execute() on the command and returns any numeric value from the command.

```public void execute() throws ParserException``` - runs the command.

#####**TurtleCommand/GUICommand** etc etc
Parent classes for each category of command.

#####**CommandMap.java** implements Evaluable, CommandString

Class used to parse commands - links every element of a command with its corresponding ```SyntaxElement``` type. Impelements ```CommandString``` interface to pass to GUI.. Also builds a ```List<Evaluable>``` which can be evaluated, once the ```CommandInterpreter``` has determined that the ```CommandMap``` is valid. Useful also for defining user commands, which are stored as a command map which has already been evaluated.

```public CommandMap(String command) throws ParserException``` - parses command into its corresponding Class pairs. Builds map. Throws Exception if the process failed.

```public void addEvaluable(Evaluable e)``` - adds e to the next position in the Evaluation list.

```public CommandMap subMap(int startPos)``` - returns a new CommandMap starting at the specified position. Used by Executable objects to return the remainder of their CommandString for evaluation.

```public String getFirst()``` - returns the first element of the map in String form for reflecting.

```public boolean isEmpty()``` - returns true if map is empty, false otherwise.

```public void evaluate() throws ParserException ```- iterates through Evaluation list, throwing an exception if anything fails to evaluate. Does nothing if evaluation list has not been defined (as in the case of a submap).

#####**SyntaxElement.java**
	
Helper class used to assist with code formatting and addition of new syntax elements. Defines several constants, used to map to types of syntax elements (for code highlighting). Each of these constants corresponds to a class string instance which can be used to reflect and create a new instance of the class for each element. New syntactical elements can be added in this manner.

```Java
SyntaxElement.COMMAND
SyntaxElement.USERCOMMAND
SyntaxElement.VARIABLE
SyntaxElement.SYMBOL
SyntaxElement.PRIMITIVE
SyntaxElement.COLOR
SyntaxElement.SHAPE
```

```static Color getColor(String element)``` - returns a Color object for the given element.

#####**Regex.java**

Helper class used by other Parser classes. As of now, there are no public methods (it is used internally by ```CommandMap``` and other classes, however).


#####**ParserResource.java**

Helper class which converts raw command strings to their native class names by interacting with 

```public static String getNativeString(String command) throws ParserException``` - returns native String name for command or throws an exception.

```public static void setLanguage(String language) throws ParserException``` - sets language to language or throws exception.

#####**Evaluable.java**
Interface which specifies the following functions:
```public double evaluate()``` - evaluates this command and returns the double result.

#####**Executable.java** extends Evaluable
Interface which specifies the following functions:

```public double execute()``` - runs the current command and returns the result of its evaluation. Used for commands which can be executed, as opposed to just evaluated. Calling execute actually modifies state.
{'fraction_non_alphanumeric': 0.08383923768539153, 'fraction_numerical': 0.0007684623069238454, 'mean_word_length': 5.231027052908786, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1282166', 'n_tokens_mistral': 6502, 'n_tokens_neox': 6033, 'n_words': 3504}