Specifications - SylvainRamseyer/pascalcompiler GitHub Wiki

Specifications

The current section presents each point more in detail, but first, let's take a look at its quick summary :

  • The basics

    • Variable types
    • Arithmetic operators
    • Logical operators
  • Flow control

    • Conditional
    • Loops
  • Functions

    • Pre-written functions
    • "Hand-made" functions

If you are familiar with the Pascal language or not, this is how the program skeleton is written basically:

{PROGRAM}
PROGRAM program_name;
{VARIABLES: global scope}
{standard variable declaration}
VAR
  var1, var2 : variable_type;
{constant variable declaration and initialization}
CONST
  varC_1 = something;
  varC_2 = something_else;
{functions + procedures}
{PROGRAM START}
BEGIN
  {code}
END.

The basics

Variable types

The following types will be handled by the compiler :

  • Integer
  • Real
  • Bool
  • Char

If you know how Pascal does work, you're probably familiar with the following : we must precise when a variable is a constant at the beginning of our program.

VAR variable_name : variable_type;
{constant variable declaration + initialization -> using = }
CONST variable_name = something;

Arithmetic operators

The following operation will be handled by the compiler :

  • Arithmetic operation
{sum affectation -> using := }
sum := a + b;

Logical operators

The following operators will be handled by the compiler :

  • AND
  • OR
  • NOT
(a = true) AND (b = false)

Comments section

The compiler will be able to recognize and ignore the comments written throughout the code.

{I will be ignored by the compiler *sob*}

Flow Control

Conditional operators

The following operators will be handled by the compiler :

  • IF .. THEN
IF <condition> THEN
  BEGIN
    {do this code}
  END.
  • IF .. THEN .. ELSE
IF <condition> THEN
  {do this code}
ELSE
  {do this code}
IF <condition> THEN
BEGIN
  IF <condition> THEN
    {do this}
  IF <condition> THEN
    {do this}
  ELSE
    {do this}
END.

Loops

The following loops will be handled by the compiler :

  • Repeat .. until (also known as 'while' in other languages)
REPEAT
  {code}
UNTIL <some conditional statement is true>;
  • While .. do
WHILE <condition is true>
DO
  BEGIN
    {code}
  END.

Functions

There are two types of functions, those we create and those that are already in the language. We decided to manage both, as it is shown below.

Pre-written functions

The following function will be handled by the compiler :

  • Write
  • Writeln
Writeln('Hello world!');
Writeln('My name is ', name);

"Hand-made" functions

The following function types will be handled by the compiler :

  • Procedure
PROCEDURE procedure_name;
VAR variable_name : variable_type;
BEGIN
  {code}
END.
PROCEDURE procedure_name(variable_name : variable_type);
VAR variable_name_inside_function : variable_type;
BEGIN
  {code}
END.
  • Function
FUNCTION function_name(variable_name : variable type) : return_type;
VAR variable_name_inside_function : variable_type;
BEGIN
  {code}
END.

Reserved words

The following reserved words will be taken care of:

{variable types}
INTEGER
REAL
BOOL
CHAR

{arithmetic operators}
+
-
*
/

{logical operator}
AND
OR
NOT


{program}
PROGRAM

{variables}
VAR
CONST

{functions}
FUNCTION
PROCEDURE
WRITE
WRITELN

{flow control}
IF
THEN
ELSE
REPEAT
UNTIL
WHILE
DO

{start + end of section}
BEGIN
END.
⚠️ **GitHub.com Fallback** ⚠️