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.
The basic structure of MyHL is shown below:
begin vars
...
variable declarations
...
end vars
begin statements
...
program statements...
end statements
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
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 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:
begin vars
end vars
begin statements
end statements
use as
word
number
read
MyHL implements only two data types: number and word.
Numbers are non-negative integers. The program will throw an Overflow Error when it encounters a negative number.
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 are storage locations associated with a name or identifier. They are used to store values within your program.
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
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.
Identifiers must not have other characters other than letters, digits and underscores.
The first character must not be a digit.
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.
An expression is a piece of code that represents a value. An expression could be a:
Number
Word
Variable
Operations between expressions
The validity of an expression inherents the validity of these program elements.
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.
expression + expression
1 + 2
OUT: 3
expression - expression
4 + 2
OUT: 2
expression * expression
4 * 2
OUT: 8
expression / expression
6 / 2
OUT: 3
expression % expression
3 % 2
OUT: 1
MyHL supports PEMDAS precedence on operations.
(3 + 2) * 2
OUT: 10
3 + 2 * 2
OUT: 7
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.
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.
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:
the variable specified is not declared in the variable block: Null Error; and if
the variable specified is of data type number and the input does not represent a number: Type Error
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:
the variable used in the statement is not declared in the variable block: Null Error; and if
the expression is invalid. see Expressions
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:
the variable used in the statement is not declared in the variable block: Null Error; if
the expression is invalid; see Expressions, and if
the value of the expression and the data type of the variable is incompatible (strong typing); Type Error.
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
This is the list of errors that may be thrown by the program.
This is the general error thrown for syntax error encountered by the parser. This is a native support by Jison.
This is the error thrown when a variable that is not declared is used in a statement.
This is the error thrown for an operation among incompatible data types.
This is the error thrown when storing an expression into an incompatible data type (strong typing).
This is the error thrown for negative values.