The purpose of this homework is to get you familiarized with the following concepts:
The purpose of the homework is to write an interpreter for the Design language. The language is used to draw geometric figures and and points.
Write an interpreter that receives a file with statements as parameter and writes a SVG file. If there was an error, it will output ERROR LINE followed by the line number: and the error text.
The program receives two parameters:
A usage example:
node main.js design.dsn canvas.svg
This will run the design.dsn file and write the drawing into the canvas.svg file.
For this design.dsn file
& Move POSITION: 200, 200 COMMAND_WITH_ERROR LINE: 300, 350 location
the interpreter should output
ERROR LINE 2: Unknown statement STATEMENT_WITH_ERROR
The homework is individual. Any attempt of copying will result in 0p for the homework. Automated anti copying system will be used.
If you have any questions related to the homework, please ask them by posting an issue on the github quiestions repository with the title format [design] <your question title>. You will need a github account for that.
If you want to receive an email when issues are posted or when there are new messages, got to the github questions repository and click Watch.
The language is actually moving a pen. The pen's initial position is 0, 0. When drawing, the pen will move to the new position.
All statements accept as parameters numbers or variables. Variables have the following format:
%variable
Before it can be used, a variable needs to be assigned a value (using the set statement).
Statements are stored inside a file. Each statement is on a separate line.
The format for a statement is the following:
STATEMENT : parameter1 parameter2 etc
Statements may have any number of spaces before their name, between parameters and any number of spaces after the last parameter. Here are some examples of valid statements:
STATEMENT: p1 p2 p3 STATEMENT : p1 p2 p3 STATEMENT:p1 p2 p3
All statements and parameters are case insensitive.
Moves the pen to a position, without drawing anything.
The parameters are x and y.
POSITION: 150 150 POSITION: %here %there & %here and %there are variables that are previously assigned
Wrong number of parameters
ERROR LINE (line_number):POSITION has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter type
ERROR LINE (line_number): POSITION parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Draws a line from the current position to a specified position or length and angle. This will be the new position of the pen.
LINE: x y position LINE: l a polar
It takes exactly three parameters
Wrong number of parameters
ERROR LINE (line_number):LINE has 3 parameters, you wrote (number_of_written_parameters)
Wrong parameter type for type
ERROR LINE (line_number): LINE parameter 3 requires one of (position, polar), you wrote (actual_written_parameter_value)
Wrong parameter type
ERROR LINE (line_number): LINE parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Draw a circle
This does not move the current position.
CIRCLE: x y r
It takes exactly three parameter
Wrong number of parameters
ERROR LINE (line_number): CIRCLE has 3 parameters, you wrote (number_of_written_parameters)
Wrong parameter type
ERROR LINE (line_number): CIRCLE parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Draw an ellipse
This does not move the current position.
ELLIPSE: x y r1 r2
It takes exactly four parameter
Wrong number of parameters
ERROR LINE (line_number): ELLIPSE has 3 parameters, you wrote (number_of_written_parameters)
Wrong parameter type
ERROR LINE (line_number): ELLIPSE parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Draw a rectangle
This does not move the current position.
RECTANGLE: x1 y1 x2 y2
It takes exactly four parameter
Wrong number of parameters
ERROR LINE (line_number): RECTANGLE has 4 parameters, you wrote (number_of_written_parameters)
Wrong parameter type
ERROR LINE (line_number): RECTANGLE parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Selects the drawing color. This will be the new position of the pen.
The initial pen color is black. The initial fill color is white.
COLOR: pen r g b COLOR: fill r g b
It takes exactly four parameters
Values are between 0 and 255.
Wrong number of parameters
ERROR LINE (line_number):COLOR has 4 parameters, you wrote (number_of_written_parameters)
Wrong parameter type for type
ERROR LINE (line_number): COLOR parameter 1 requires one of (pen, fill), you wrote (actual_written_parameter_value)
Wrong parameter type
ERROR LINE (line_number): COLOR parameter (the number of the parameter) requires a number or a variable, you wrote (actual_written_parameter_value)
Value of of range
ERROR LINE (line_number): COLOR parameter (the number of the parameter) requires a number or a variable between [0, 255], you wrote (actual_written_parameter_value, if it is a variable write the value of the variable)
Repeats some lines of statements until REPEAT
LOOP: times
LOOP statements are not imbricated (LOOP in LOOP)
It takes exactly one parameter
* times - an integer number or a variable specifying the number of times to repeat the statements up to REPEAT
Wrong number of parameters
ERROR LINE (line_number): LOOP has 1 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): LOOP parameter 1 requires a number or a variable, you wrote (actual_written_parameter_value)
The file ends and there still is at least one LOOP without REPEAT
ERROR LINE (line_number): LOOP with no REPEAT
Repeats some lines of statements until REPEAT
WHILE: variable
WHILE statements are not imbricated (WHILE in WHILE)
It takes exactly one parameter
* variable - a variable, if the variable is different from 0, repeat the statements up to REPEAT
Wrong number of parameters
ERROR LINE (line_number): WHILE has 1 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): WHILE parameter 1 requires a variable, you wrote (actual_written_parameter_value)
The file ends and there still is at least one WHILE without REPEAT
ERROR LINE (line_number): WHILE with no REPEAT
Ends a loop or a while
It takes no parameters
Wrong number of parameters
ERROR LINE (line_number): REPEAT has 0 parameters, you wrote (number_of_written_parameters)
END is used without a previous LOOP
ERROR LINE (line_number): REPEAT and no LOOP
Execute some lines of statements until ELSE or END
IF: variable
IF statements are not imbricated (IF in IF)
It takes exactly one parameter
* variable - a variable, if the variable is different from 0, execute the statements up to ELSE or END (if there is no ELSE)
Wrong number of parameters
ERROR LINE (line_number): IF has 1 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): IF parameter 1 requires a variable, you wrote (actual_written_parameter_value)
The file ends and there still is at least one IF without ELSE or END
You have number_of_repeats IF without END
Else part of the if
It takes no parameters
Wrong number of parameters
ERROR LINE (line_number): ELSE has 0 parameters, you wrote (number_of_written_parameters)
END is used without a previous IF
ERROR LINE (line_number): ELSE and no IF
Ends an if
It takes no parameters
Wrong number of parameters
ERROR LINE (line_number): END has 0 parameters, you wrote (number_of_written_parameters)
END is used without a previous IF
ERROR LINE (line_number): END and no IF
Set the value of a variable
SET: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): SET has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): SET parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): SET parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
Add a value to the value of a variable
ADD: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): ADD has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): ADD parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): ADD parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
Subtract a value from the value of a variable
SUB: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): SUB has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): SUB parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): SUB parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
Multiply a variable's value with a value.
MUL: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): MUL has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): MUL parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): MUL parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
Divide the value of a variable with a value
DIV: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): DIV has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): DIV parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): DIV parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
Divide the value of a variable with a value (integer value)
IDIV: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): IDIV has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): IDIV parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): IDIV parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
The remainder of the division of a variable with a value
MOD: variable value
It takes exactly two parameter
Wrong number of parameters
ERROR LINE (line_number): MOD has 2 parameters, you wrote (number_of_written_parameters)
Wrong parameter for times
ERROR LINE (line_number): MOD parameter 1 requires a variable, you wrote (actual_written_parameter_value)
Wrong parameter for times
ERROR LINE (line_number): MOD parameter 2 requires a number or a variable, you wrote (actual_written_parameter_value)
For an additional 0.20p, implement the LOOP, WHILE and IF imbrication.