About MyHL

MyHL is a simple and abstract high-level computer language. This a project for our Programming Languages course.

MyHL implements static binding and strong typing.

Structure

The basic structure of MyHL is shown below:

begin vars
...
variable declarations
...

end vars
begin statements
...
program statements...

end statements

Variable Block

Every MyHL program must start with a variable block enclosed by the keywords begin vars on the first line and end vars on the last line. This block contains all the variable declarations for the program. This is the only block were variables can be declared and only the variables that are declared can be used. Variable declarations are discussed here.

begin vars
x, y, z use as number;
name use as word;
end vars

Statement Block

The statement block comes right after the variable block and is enclosed by the keywords begin statements on the first line and end statements on the last line. This block contains the main logic of the program represented by a combination of program statements. Learn more about statements here.

begin statements
read x;
read y;
z = x + y;
print z;
print "MyHL";
end statements

Keywords

Keywords are reserved words that have special purposes in the program and cannot be used in any other context (except in comments). These are the keywords used in MyHL:

  1. begin vars

  2. end vars

  3. begin statements

  4. end statements

  5. use as

  6. word

  7. number

  8. read

  9. print

Data Types

MyHL implements only two data types: number and word.

Number

Numbers are non-negative integers. The program will throw an Overflow Error when it encounters a negative number.

Word

Words are sequences of alphanumeric and special characters enclosed in quotes.

begin statements
word1 = "this is a word";
word2 = 'this is a word';
end statements

Variables

Variables are storage locations associated with a name or identifier. They are used to store values within your program.

Declaration

To use a variable, it must be declared inside the variable block. A variable is declared by defining a name followed by the key word use as, the data type for that variable and a semicolon, ;, as punctuation.

identifier use as data type;

You can also declare multiple variables at once if they are of the same data type.

identifier1, indentifer2 use as data type;

Example variable declarations:

begin vars
x, y, z use as number;
name use as word;
end vars

Identifier rules

MyHL's naming rules on identifiers are the same from that of C. Identifiers must conform with the following rules in order to deemed valid.

  1. Identifiers must not have other characters other than letters, digits and underscores.

  2. The first character must not be a digit.

  3. The identifier must not be a keyword in MyHL.

It is also important to note that identifiers are case-sensitive. Identifiers with the same name but of different case are treated differently, so seaside, SeaSide and SEASIDE are distinct identifiers.

Expressions

An expression is a piece of code that represents a value. An expression could be a:

  1. Number

  2. Word

  3. Variable

  4. Operations between expressions

The validity of an expression inherents the validity of these program elements.

Operations

MyHL supports basic arithmetic operations. Note that since MyHL supports strong typing only, all expressions involved in the operations must be of the data type number. Otherwise, an Operation Error will be raised.

Addition

expression + expression

1 + 2
OUT: 3

Subtraction

expression - expression

4 + 2
OUT: 2

Multiplication

expression * expression

4 * 2
OUT: 8

Division

expression / expression

6 / 2
OUT: 3

Modulo

expression % expression

3 % 2
OUT: 1

Parenthesis and Precedence

MyHL supports PEMDAS precedence on operations.

(3 + 2) * 2
OUT: 10
3 + 2 * 2
OUT: 7

Word Concatenation

MyHL also supports concatenation of words by using the + operator on words

"a "+ "string"
OUT: "a string"

Other operations mentioned above are not supported for words and will raise an Operation Error if used.

Statements

Program statements represent the main logic of the program. They control how values and variables are manipulated in the program for whatever purpose the program serves. They must be used within the statement block of the program. Essentially there are three types of program statements in MyHL: read, print and assignment statements.

Read

The read statement asks the user for input and stores it in the variable specified. It is called by using the read keyword followed by the identifier of the variable in which the input will be stored and semicolon, ;, for punctuation.

read identifier;

begin statements
read x;
read y;
end statements

The read statement will throw an error if:

  1. the variable specified is not declared in the variable block: Null Error; and if

  2. the variable specified is of data type number and the input does not represent a number: Type Error

Print

The print statement displays the value of an expression to the output window. It is called by using the print keyword followed by either a variable or an expression and a semicolon, ;, for punctuation.

print expression;

begin statements
print x;
print "MyHL";
print (4 + 2) * 2;
end statements

The print statement will throw an error if:

  1. the variable used in the statement is not declared in the variable block: Null Error; and if

  2. the expression is invalid. see Expressions

Assignment

An assignment statement stores an expression's value into a variable. An assignement statement is made up of the identifier of a variable followed by the = operator and an the expression on the right side.

identifier = expression;

begin statements
x = 1;
y = 5;
z = x + y - 20;
end statements

An assignment statement will throw an error if:

  1. the variable used in the statement is not declared in the variable block: Null Error; if

  2. the expression is invalid; see Expressions, and if

  3. the value of the expression and the data type of the variable is incompatible (strong typing); Type Error.

Comments

Comments are constructs ignored by the program. They are not executed and serve only as annotations. Comments in MyHL are preceded by a double backslash: //. Everything after the double backslash until the end of the line is considered a comment.

//this is a comment
begin vars//variable block here
x, y, z use as number;//another comment
name use as word;
end vars
begin statements
x = 1;
y = 5;
z = x + y - 20;
end statements

Error List

This is the list of errors that may be thrown by the program.

Parse Error

This is the general error thrown for syntax error encountered by the parser. This is a native support by Jison.

Null Error

This is the error thrown when a variable that is not declared is used in a statement.

Operation Error

This is the error thrown for an operation among incompatible data types.

Type Error

This is the error thrown when storing an expression into an incompatible data type (strong typing).

Overflow Error

This is the error thrown for negative values.