UPC Script is a project that Aleksander Petrovic and I presented on April 1st, 2003 for our CS4613 - Programming Languages course at the University of New Brunswick, Saint John campus (UNBSJ).
Our goal was to create a programming language with a working interpreter in the form of a Java applet.
It is a very simple language, that somewhat resembles JavaScript or C. The intent was not to create a full programming language, but to go through the excercise of defining and implementing
a programming language.
To be able to run the Java applet, you will be required to have the Java 2 Standard Edition virtual machine installed. You can download a copy of this from java.sun.com
Please take a took at the source code.
The following is the sample script that I demonstrated in my presentation. Try copy and pasting this script into the applet window after you launch it.
// This UPC Script will
// draw a smiling face.
// Face
drawCircle( 160, 160, 150, pink );
// Left eye
drawCircle( 100, 100, 15, blue );
// Right eye
drawCircle( 220, 100, 15, blue );
// Nose
drawCircle( 160, 160, 15, red );
// Mouth
drawLine( 160 - 65, 200, 160, 240, red );
drawLine( 160 + 65, 200, 160, 240, red );
// Hat
drawRectangle( 40 , 10, 240,60, gray );
// Say hello to the class
drawString( 240, 300, "Hello class!" , yellow );
var flag;
// Change this flag to make
// him blink
flag = 1; // 1 = open, 0 = closed
if( flag == 1 )
{
// Open eye
drawCircle( 100, 100, 15, blue );
}
else
{
// Close eye
drawCircle( 100, 100, 15, pink );
}
|
The follwing is the EBNF of the grammar for the UPC Script language. Please not that not all of this grammar was implemented in the interpreter.
***********************************************
* Course: CS4613 Project
* Title: Grammar Specification for UPC Script
* Authors: Aleksandar Petrovic, Terry Sznober
***********************************************
The following is a peseudo EBNF description of the
grammar for UPC Script
---------------------------------------------------------
<program> --> <statement list>
<statement list> --> <statement>
| <statement> <statement list>
<statement> --> <block>
| <variable declaration>
| <assignment>
| <if statement>
| <while statement>
| <drawLine statement>
| <drawRectangle statement>
| <drawCircle statement>
| <drawString statement>
<block> --> { <statement list> }
<variable declaration> --> var <identifier>;
<assignment> --> <identifier> = <expression>;
<if statement> --> if ( <expression> ) <statement>|<block> [else <statement>|<block>]
<while statement> --> while ( <expression ) <statement>|<block>
<drawLine statement> --> drawLine ( <expression>, <expression>, <expression>, <expression>, <color> );
<drawRectangle statement> --> drawRectangle ( <expression>, <expression>, <expression>, <expression>, <color> );
<drawCircle statement> --> drawCircle ( <expression>, <expression>, <expression>, <color> );
<drawString statement> --> drawString ( <expression>, <expression>, <identifier> | <string literal>, <color> );
<expression> --> <integer literal>
| <string literal>
| <identifier>
| ( <expression> )
| <expression> <arithmetic operation> <expression>
| <expression> <relational operation> <expression>
| <expression> <logical operation> <expression>
| <unary operation> <expression>
<arithmetic operation> --> + | - | * | / | %
<relational operation> --> < | > | <= | >= | == | !=
<logical operation> --> && | ||
<unary operation> --> - | !
<integer literal> --> 0 | {0-9}
<string literal> --> "[{a-Z}]"
<identifier> --> a-Z{a-Z|0-9}
<color> --> black | blue | cyan | gray | green | magenta | orange | pink | red | white | yellow
|
|