Skip to content
MefAldemisov edited this page Oct 9, 2020 · 8 revisions

Welcome to the riddle wiki!

The following is the specification of our language.

Basic examples

Variable initialization and assignmaent

var length : integer is 3       // [example 1]
var a                is true;   // [example 2]
var b                is 4.3     // [example 3]
  • semicolons at the end of line are not mandatory
  • the type is not mandatory if an initial value is specified
  • variable name could start only with latin letters (upper or lower), the next chars could me latine letters, digits or '_' symbol

Comments

  • only single line comments available in the language e.g
// comment
var a                is true;   // [example 2]

Type declaration

Type is one of the following list or identifier([TBD])

Primitive type:

  1. integer - both positive and negative
  2. real - both positive and negative
  3. boolean - true and false

Integers and real numbers are stored using 64 bits, akin to the long long and double in C++.

User Type

  1. Record
  2. Array

The assignment in case of user-defined variable works like assignment by reference

Record

Is a kind of structure, where record can be of any type.

type MyRecordType is record
    var x0      :   real
    var y0      :   real
    var selected:   boolean
end

Members are accessed with the dot notation

var dot: MyRecordType;
dot.x0 := 0.3;
dot.y0 := 6.4;
dot.selected := false;
Array

Arrays in our language are indexed from 1

The length parameter (specified between brackets) is required except in routine parameters. It must be a compile-time constant. It can be read using the dot notation with the property length.

Simple array:

type MyArray is array [ 4 ] integer;
var myArray : MyArray;
myArray[2] := 3;
myArray[1] := myArray.length

2D array:

type Array1D is array [ 4 ] integer;
type Array2D is array [ 4 ] Array1D;

var myArray : Array2D;
myArray[3][2] := 3;

[TBD] The element index is specified dynamically (using the syntax of expression). This allows organizing array processing in loops.

Statements

  1. Assignment
  2. Routine Call
  3. While loop
  4. For loop
  5. If statement

Assignment

Assignment evaluates the value of the expression specified in the right part of the statement and copies it to the variable whose name is specified in the left part.

var a is 3;
a := a + 4;

Type of the variable from the left side should conform the type of the expression.

Conformance table

Only in case of user-defined types works like assignment by reference

Routine

Remember that a routine call makes an assignment of arguments

routine myFunction(a: integer, b:integer) : integer
is
    return a + b;
end

While

var a is 1;
while a < 100 loop
    a := a + 1;
end

For

The looping varibale should always be an integer and should be declared in the loop

Assignments to the loop variable are forbidden

var length : integer is 100;
type myArray is array [ length ] integer;
var array : myArray;

for a in reverse 1..100 loop
    array[a] := a
end

for b in 1..100 loop
    array[b] := 50 - b
end

1..100 notation is named Range

If

var a is true
var b is false
var c : integer
if b then
    c := 3
else
    c := 5
    if a then
        c := 4
    end
end
// c is 4

Tables and important notes:

List of all keywords:

integer real boolean true false is
var record end type array while
loop reverse for in if then
else routine and or xor return
not

Conformance table

Left part Right part Semantics
integer integer direct value copying
integer real copying with rounding to nearest integer
integer boolean true is converted to integer 1
false is converted to integer 0
real real direct value copying
real integer integer is generalized to real
real boolean true is converted to real 1.0,
false – to real 0.0
boolean boolean direct value copying
boolean integer if integer is 1, it is converted to true,
if it is 0, it’s converted to false;
otherwise, assignment is treated as erroneous.
boolean real assignment is illegal

Example:

// initialization
var a : integer is 2;
var b : real is 0.6;
var c : boolean is false;
// integer
a := b // a is now 1
a := c // a is now 0
a := 2 // a is now 2
// real
b := a // b is now 2.0
b := c // b is now false
b := 1.2 // b is nw 1.2
// boolean
c := a // error
c := 1 // c is now true
c := b // error
c := true // c is now true

Terminology

User-defined types are considered as “reference types”. This means that a variable of a user-defined type is actually the reference to an object of the type. So, the variable serves as a “representative” of the object. Therefore, any assignment to a variable of an array or record type means copying the reference to the aggregate but not copying of the aggregate itself.