Homework 2 - Design Language

Information

Deadline: 4th of April, 23:55
Points: 1 point out of the final grade
Link: Devoir 2
Late upload: 1 point / day (maximum 2 days)

Purpose

The purpose of this homework is to get you familiarized with the following concepts:

  1. basic TypeScript development
  2. use of data structures in TypeScript
  3. use of command line parameters
  4. reading data from files
  5. basic string splitting and processing

What do you have to do

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:

  • the file with statements
  • the svg output file

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.

Line endings in the commands file are Linux like, meaning just \n.

Example

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

Initial values

  • The initial position is 0, 0.
  • The initial pen color is black.
  • The initial fill color is white.
  • The pen width is 1.

Copying

The homework is individual. Any attempt of copying will result in 0p for the homework. Automated anti copying system will be used.

Questions

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.

DO NO POST ANY CODE. This is considered copying and will result in a 0p homework for you.

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.

Design Language

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.

Variables

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 format

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.

Statements

POSITION

Moves the pen to a position, without drawing anything.

Syntax

The parameters are x and y.

POSITION: 150 150
POSITION: %here %there
& %here and %there are variables that are previously assigned
Errors

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)

LINE

Draws a line from the current position to a specified position or length and angle. This will be the new position of the pen.

Syntax
LINE: x y position
LINE: l a polar
Parameters

It takes exactly three parameters

  • type - is either position either polar
  • x - if type is position, the x coordinate of the new position
  • y - if type is position, the y coordinate of the new position
  • l - if type is polar, the length of the line
  • a - if type is polar, the angle (from the trigonometric circle) in degrees
Errors

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)

CIRCLE

Draw a circle

This does not move the current position.

Syntax
CIRCLE: x y r
Parameters

It takes exactly three parameter

  • x - the x coordinate for the center
  • y - the y coordinate for the center
  • r - the radius
Errors

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)

ELLIPSE

Draw an ellipse

This does not move the current position.

Syntax
ELLIPSE: x y r1 r2
Parameters

It takes exactly four parameter

  • x - the x coordinate for the center
  • y - the y coordinate for the center
  • r1 - the horizontal radius
  • r2 - the vertical radius
Errors

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)

RECTANGLE

Draw a rectangle

This does not move the current position.

Syntax
RECTANGLE: x1 y1 x2 y2
Parameters

It takes exactly four parameter

  • x1 - the x coordinate of the top left corner
  • y1 - the y coordinate of the top left corner
  • x2 - the x coordinate of the lower right corner
  • y2 - the y coordinate of the lower right corner
Errors

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)

COLOR

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.

Syntax
COLOR: pen r g b
COLOR: fill r g b
Parameters

It takes exactly four parameters

  • type - is either pen (the color of the line or broder) either fill (the color inside)
  • r - the red component of the color
  • g - the green component of the color
  • b - the blue component of the color

Values are between 0 and 255.

Errors

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)

LOOP

Repeats some lines of statements until REPEAT

LOOP: times

LOOP statements are not imbricated (LOOP in LOOP)

Parameters

It takes exactly one parameter

* times - an integer number or a variable specifying the number of times to repeat the statements up to REPEAT

Errors

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

WHILE

Repeats some lines of statements until REPEAT

WHILE: variable

WHILE statements are not imbricated (WHILE in WHILE)

Parameters

It takes exactly one parameter

* variable - a variable, if the variable is different from 0, repeat the statements up to REPEAT

Errors

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

REPEAT

Ends a loop or a while

Parameters

It takes no parameters

Errors

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

IF

Execute some lines of statements until ELSE or END

IF: variable

IF statements are not imbricated (IF in IF)

Parameters

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)

Errors

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

Else part of the if

Parameters

It takes no parameters

Errors

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

END

Ends an if

Parameters

It takes no parameters

Errors

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

Set the value of a variable

SET: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value, may be a number or another variable
Errors

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

Add a value to the value of a variable

ADD: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to add, may be a number or another variable
Errors

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)

SUB

Subtract a value from the value of a variable

SUB: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to subtract, may be a number or another variable
Errors

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)

MUL

Multiply a variable's value with a value.

MUL: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to multiply the variable's value with, may be a number or another variable
Errors

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)

DIV

Divide the value of a variable with a value

DIV: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to divide the variable's value with, may be a number or another variable
Errors

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)

IDIV

Divide the value of a variable with a value (integer value)

IDIV: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to divide the variable's value with, may be a number or another variable
Errors

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)

MOD

The remainder of the division of a variable with a value

MOD: variable value
Parameters

It takes exactly two parameter

  • variable - a variable
  • value - the value to divide the variable's value with, may be a number or another variable
Errors

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)

Bonus

For an additional 0.20p, implement the LOOP, WHILE and IF imbrication.

Bonus will be awarded only if all other tests pass.

alf/teme/tema_en_2.txt · Last modified: 2022/03/24 17:13 by diana.ghindaoanu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0