DrawScript is a simple scripting library that allows for the generation of basic geometric drawings using Java's Swing library.
This scripting language was developed as the final project for a Programming Language Engineering course as part of elective coursework for the Bachelor's in Computer Engineering at Iscte-IUL.
- Manual creation of Abstract Syntax Tree
- Semantic Validation
- Window dimension must be a (x,y) point
- Window background colour must be an (r,g,b) colour
- Conditional branches must use Boolean-expression guards
- Constant references must reference previously-defined constant
- Colour RGB values must be numeric and in [0..255]
- Point x,y coordinates must be numeric
- Interval bounds must be numeric
- Bounds must be in ascending order
- Binary expressions follow operand-operator context-sensitive restrictions (e.g. can only add together numbers, cannot add a number to an interval, etc.)
- Set-colour instructions must take values of type Colour
- Variables can only be referenced in the scope of their definition (inside declaring loop)
- Parsing
- Interpretation
- Visualisation window instantiated correctly
- Figures drawn correctly
A DrawScript script is composed by three sections:
- Constant definitions;
- Display window parameters;
- Instructions.
Each of these can make use of script expressions, which represent the several data types supported by the language.
An expression is a part of the script that represents a value, as opposed to an instruction telling the script what to do. For example, you can represent the number 2 using a Number expression. Currently, DrawScript supports the following types of expressions:
Expression Type | Examples |
---|---|
Number | 2 ; 3 ; 10, ... |
Bool | true ; false |
Colour | (255, 255, 255) ; (255,0,0) ; ... |
Point | (0, 0) ; (3, 5) ; (2, 8) ; ... |
Interval | [0, 3] ; [2, 7[ ; ]3, 8] ; ]3, 5[ ; ... |
Constant Reference | N ; M ; C ; ... |
Variable Reference | i ; j ; k ; ... |
Binary Expression | 2 + 3 ; 10 - i ; 5 <= j ; ... |
A constant can be defined by associating an alphabetical identifier to any valid expression at the start of your script. For example:
N: 256
GRAY: |128|128|128|
MYINTERVAL: [2, 8[
- ...
Each script can take two parameters that specify the dimensions of the display window and its background colour. These are defined after the constant definitions. For example:
dimension: (512, 512)
background: |128|0|128|
If your script does not define these parameters, they default to (100, 100) and White, respectively.
An instruction specifies some action that the script must take. For example, loops and conditional branches. Currently, DrawScript supports the following types of instructions:
Instruction | Description |
---|---|
line $start, $end |
Draws a line between two points. |
rectangle $start, $width, $height |
Draws a rectangle from its top-left point with the given width and height. |
square $start, $width |
Draws a square from its top-left point with the given side length. |
ellipse $center, $width, $height |
Draws an ellipse from its given center point with the given width and height. |
circle $center, $radius |
Draws a circle from its given center point with the given radius. |
polyline $start, $points |
Draws a polyline starting at a given point and following a list of points. |
setlinecolor $color |
Sets the colour used to draw (out)lines. |
setfillcolor $color |
Sets the colour to fill objects. |
if ($guard) then { $body } else { $alternative } |
If the guard is true, executes the instructions in body, which is a list of instructions. Otherwise, executes alternative. |
for $var_id in $interval { $body } |
Loops over every integer in the given interval and executes the instructions in body. |
Check out the full documentation here!
The following DrawScript code generates a simple black-and-white grid pattern.
N: 8
SIDE: 40
MARGIN: 5
BLACK: |0|
WHITE: |255|
GRAY: |128|
---
dimension: (N * SIDE + MARGIN * 2,N * SIDE + MARGIN * 2)
background: GRAY
---
setlinecolor BLACK
for l in [0, N[ {
for c in [0, N[ {
if ((l + c) % 2 == 0) {
setfillcolor WHITE
} else {
setfillcolor BLACK
}
square (c * SIDE + MARGIN,l * SIDE + MARGIN), SIDE
}
}
DrawScript provides a built-in editor that allows for running scripts in real-time in a simplistic GUI that displays the results of the script and any errors thrown during the parsing or interpretation steps.
The following is an example of running the example code provided above in the editor.
The UML class diagram for this project can be found here.
Full credit for the basic specification of the library's requirements goes to Professor André L. Santos, Assistant Professor at Iscte-IUL and coordinator of the course this library was developed for. The original project specification can be found here.
Credit for all the code present in this repository goes to Afonso Caniço and João Pereira, authors and sole contributors to the project and this repository, unless otherwise explicitly stated.