Package edu.rit.pal6640.cc.bpl

Back to BPL Home

See:
          Description

Interface Summary
Parser.yyInput must be implemented by a scanner object to supply input to the parser.
 

Class Summary
AbstractVisitor Contains the base functionality for traversing ASTs build using the Node class.
BPL Program driver.
Env<T> Represents a stack of envorinment layers (frames).
EnvLayer<T> Contains symbols names and values.
Interpreter Interprets a BPL tree.
Parser  
TypeChecker Tree visitor which does type checking.
ValueStack A stack implementation based off of LinkedList.
 

Exception Summary
Parser.yyException thrown for irrecoverable syntax errors and stack overflow.
PositionException Exception which has information about where in the input the error was that caused the exception
TypeException A PositionException caused by a type error.
 

Package edu.rit.pal6640.cc.bpl Description

Back to BPL Home

BPL User Manaul

  1. Introduction
  2. Whitespace
  3. Statements and Blocks
  4. Expressions
  5. Variables
    1. Declaration
    2. Assignment
  6. Control structures
    1. Selection
    2. Iteration
  7. Functions
    1. Parameters
    2. Scoping
  8. Aggregates
  9. Todo/BPL shortcomings

Introduction

This is the user manual for BPL, a language with C-like syntax with limited functionality. It has global and local variables.. It has simple selection (if) and iteration (while) control structures. It also allows nested blocks of statements with local variables. It allows for functions and aggregates.

Whitespace

Whitespace including newlines is not significant and does not affect program execution, except where noted. Note that BPL keywords, identifiers and operators may not contain whitespace.

Statements and Blocks

A BPL program is made up of statements. Types of statements include variable declaration and assignment as well as control structures. Statements which are not control structures may span lines and are semicolon terminated. Statements may be enclosed in a block. A block is defined by a starting and ending braces as follows.
statement1; statement2; { blockStatement1; . . . blockStatementn; }

Expressions

BPL statements may contain expressions. Expressions are program elements which evaluate to integer values. When expressions are being evaluated, the precedence will be used to determine in what order sub-expressions will be evaluated. The higher the precedence, the sooner the sub expression will be evaulated.
Expression Type Expression Description Precedence Associativity
Numeric Constant This evaluates to the given numeric constant - -
Boolean Constant This evaluates to the given boolean constant - -
String Constant This evaluates to the given string constant - -
Variable This evaluates to value of the variable - -
expr1 && expr2 This evaluates to true if expr1 and expr2 both evaluate to true value and to false otherwise.
expr1 and expr2 must be boolean expressions
1 Left
expr1 || expr2 This evaluates to false if both expr1 and expr2 evaluate to false, and to true otherwise;
expr1 and expr2 must be boolean expressions.
1 Left
expr1 > expr2 This evaluates to true if expr1 is numerically greater than expr2 and false otherwise.
expr1 and expr2 must be integer expressions.
2 Left
expr1 < expr2 This evaluates to true if expr1 is numerically less than expr2 and false otherwise.
expr1 and expr2 must be integer expressions.
2 Left
expr1 >= expr2 This evaluates to true if expr1 is numerically greater than or equal to expr2 and false otherwise.
expr1 and expr2 must be integer expressions.
2 Left
expr1 <= expr2 This evaluates to true if expr1 is numerically less than or equal to expr2 and false otherwise.
expr1 and expr2 must be integer expressions.
2 Left
expr1 == expr2 This evaluates to true if expr1 is equal to expr2 and false otherwise. The expressions may be of any type. Both expressions must be of the same type for the expression to evaluate to true. Integers will be compared numerically. Booleans will be compared to see if they contain the same truth value. String values will compared to each other lexographically to see if they contain the same sequence of characters. Comparisons of objects and functions will only evaluate to true if the object or function is compared to itself. 2 Left
expr1 != expr2 This evaluates to true if expr1 is NOT equal to expr2 and false otherwise. Will return false where an equality test would return true and vice-versa. 2 Left
expr1 + expr2 This evaluates to the sum of expr1 and expr2 3 Left
expr1 - expr2 This evaluates to expr2 subtracted from expr1.
expr1 and expr2 must be integer expressions.
3 Left
expr1 * expr2 This evaluates to the product of expr1 and expr2. expr1 and expr2 must be integer expressions. 4 Left
expr1 / expr2 This evaluates to the integer quotient of expr1 divided by expr2. expr1 and expr2 must be integer expressions. 4 Left
expr1 % expr2 This evaluates to the remainder of expr1 divided by expr2. expr1 and expr2 must be integer expressions. 4 Left
- expr This evaluates to the negation of expr
expr must be be an integer expression.
5 Right
+ expr This evaluates to expr
expr must be be an integer expression.
5 Right
( expr ) This evaluates to expr 6 None

Variables

BPL allows the definition of global and local variables. Global variables are those defined at the top level. Local variables are those defined in the scope of a block, including blocks in control statements.

Declaration

All BPL variables may be of type integer, boolean, string, function or aggregate. Variables are declared using the keyword var. Variables must be named using a valid identifier. A valid identifier begins with a letter and may contain letters, nummbers and the underscore '_'. Variable names may not include whitespace. Variables may optionally be declared with an initial value. Variables may be initialized to an expression, which includes constant values. If a variable is not declared, it will be assigned the default value of 0.
var ValidIdentifier; var ValidIdentifier = <expression>;
Variables may be declared at any point in the program, not just at the beginning of the program or of blocks. Variables must be declared before they can be referenced.

Assignment

Variable assignment is similar to variable declaration with an initializing value, except that the var keyword is dropped. Variables must be declared before they can be assigned to.
ValidIdentifier = <expression>;

Control Structures

BPL includes two flow control structres, one for selection, the other for iteration.

Selection

For selection BPL uses an if-else control structure of the form
if ( <expression> ) statementOrBlock [ else statementOrBlock ]
The if clause is controlled by an expression. If the expression evaluates to to a boolean true the code associated with the if the expression with be evaluated. An else clause may optionally be appended. If the if evaluates to boolean false, the code associated with the else will be executed.
Valid forms of the if-else
if ( <expression> ) statement; if ( <expression> ) { statement1; . . . statementn; } if ( <expression> ) statement; else statement; if ( <expression> ) { statement1; . . . statementn; } else statement; if ( <expression> ) statement; else { statement1; . . . statementn; } if ( <expression> ) { statement1; . . . statementn; } else { statement1; . . . statementn; }

Iteration

For iteration BPL uses a while construct of the form
while ( <expression> ) statementOrBlock
While the expression specified evaluates to a non-zero value, the statement or block following the while will execute. The while loop may take the following forms
while ( <expression> ) statement; while ( <expression> ) { statement1; . . . statementn; }

Functions

BPL support functions. Functions are defined using the function keyword. Functions may take 0 or more arguments and return a value, or may choose to return nothing. Method return may be effected using the return keyword. A return statement is not required at the end of a function definition. However, if the last statement in a function is not a return, then the compiler/interpreter will insert a void return. Since all returns from a function must return values of the same type, this may cause problems if you are returning a value from elsewhere in the function.

Functions are generally declared as anonymous, then assigned to a variable.

var functionName = function ( arg1, arg2, ..., argn ) { . . // function code goes here . . }
For example
var sum = function( num1, num2 ) { return num1 + num2; };
There is a shortcut syntax, which is equivilent.
function sum ( num1, num2 ) { return num1 + num2; }
Note that when a function is declared anonymously and assigned to a variable, the declaration must be ended with a semi-colon. However, the shortcut syntax requires no semi-colon.

Given a function stored in a variable f, it can be invoked

f( arg1, arg2, ..., argn );
Some examples of invocation, using the sum method defined above ...
sum( 1, 2 ); print sum( sum( 10, 2 ), 10 );

Parameters

Parameter passing is done using pass-by-value. Note, however, that aggregates variables are references. This means that when an aggregate is passed, the reference is copied, not the entire aggregate. When a method is invoked, literals, variables and expressions maybe be passed to the function. They will be evaluated before the function is invoked.

Scoping

All functions are closures. They share the scope of the environement in which they are declared. Local variables will shadow variables in the enclosing scope. For example, the following code
var a = 1; var b = 2; var c = function() { var b = 4; print a + " " + b; }; c();
will result in 1 4 being outputted.

Aggregates

BPL supportes aggregates (similar to C structures). These allow variables and functions to be grouped. The syntax is as follows
var ValidIdentifier = { ValidIdentifier1 : expression1, ValidIdentifier2 : expression2, . . . ValidIdentifiern : expressionn };
Example
var employee1 = { first : "John", second : "Smith", empId : nextId(), age : 30, currentlyEmployed : true, rightSize : function() { currentlyEmployed = false; } };
Note that you can not use the shorthand function definition notation in this context. Functions defined in an aggregate have all other variables defined in the scope of the aggregate.

To do/Shortcomings