# Stage 4. Final Design, Imlementation & Presentation
## Project title
Kassym-Jomart
## Project team members and their roles in the project.
* Dias Usenov:
- [x] finish compiler
- [x] type checker
* Makshe Seytkaliev:
- [x] demo application
- [x] documentation
* Kamila Khamidullina:
- [x] language design
- [x] testing
## General description of the target programming language, its core ideas, and list of features.
#### Statically-typed functional programming language like Racket.
* Predefined functions: let, set, define, cond.
* Iterations: for, for\list
* Records: record, record-keywords, record-values, record-field
* Arrays: array, array-rem, array-get, array-add, array-set
* Arithmetic functions: plus, minus, mul, div.
* List operations: head, tail, cons.
* Comparison functions: equal, nonequal, less, lesseq, greater, greatereq.
* Predicates: isint, isreal, isbool, isstring, islist.
* Logical operators: and, or, xor, not
#### Features:
* Base types (mandatory)
* User-defined terms and types (mandatory)
* Standard libraries: arithmetic, logic, list (mandatory)
* First-class functions (lambda) (mandatory)
* Nested functions (mandatory)
* Simple Constraint-Based Type Inference (mandatory)
* Functions with multiple arguments (1)
* Sequencing (1)
* Exceptions (3)
* Tuples (3)
* Built-in lists (4)
* Records (3)
* Wildcard bindings (1)
* General recursion (2)
###### total: `18 points`
## Language design.
* Program is a set of elements. Elements are either atoms, literals or declarations.
* Literals are just values that are written explicitly. There are integer, real, boolean, string, list, array or record literals.
* Atoms can be considered as variables.
* Declaration elements are functions that declare new variables or methods.
* List is a sequence of elements separated by whitespaces and enclosed by parentheses. List can contain several types of literals. So the list in the program is heterogeneous. And since list elements cant be changed it can be considered as tuples. List can build with two ways:
* List build functions
* With ‘ symbol, example: ‘(1 2 3 4)
* Array is a sequence of elements as list, but it is homogeneous and its elements can be changed.
* Record is a collection of name-value mappings (like a dictionary or hash-tables).
* The program execution starts from its very first element. Each element is evaluated in accordance with its semantics. If the current element is an atom, the program(interpreter) simply returns its value.
### Types
The language has
* integer
* real
* boolean
* string
* list
* array
* record
types. Each variable has one of this types, and each function should return one of this types or nothing.
### Predefined functions
* (let Atom:Type Element) - declaration function that creates Atoms.
* (set Atom Element) - set a new value to an already existing atom.
```
> (let v:Integer 5)
> v
5
> (set v 10)
> v
10
```
* (define Name (Atom:Type, Atom:Type)) -> Type {Body} - define a new function.
```
> (define double (a:Integer)) {
> (return (times a a))
> }
> (double 5)
25
```
* (cond [Boolean Any]*) -> Any - the form is the construct for conditional evaluation. It can contain several arguments, each argument has two elements. First element is the condition, the second is the returned element if the condition is true. Program will go through arguments starting from first, If one of the conditions is true, then the program returns its second element, and ends the function.
```
> (let a:Integer 5)
> (let b:Integer 10)
> (cond [(less a b) (b)] [else (a)])
10
```
### Iteration functions
* (for Atom {List\|Integer}) {Body} - the form supports iteration over the sequence. First element is the iterator name, second is either the list or integer. If second is the list, it goes through the list, if integer it goes through all integers starting from 0 to give integer. Element is a body of iteration, at each step it will return some element.
```
> (for i '(1 2 3)) {
> i
> }
1 2 3
> (for i 4) {
> i
> }
0 1 2 3
```
* (for/list (Atom {List\|Integer}) Any) -> List - same as previous but instead of just returning elements, it will construct the list with these elements.
```
> (for/list (i '(1 2 3)) (plus i i))
'(2 4 6)
```
### Arithmetic functions
* ({plus\|minus\|mul\|div} {Integer\|Real} {Integer\|Real}) -> {Integer\|Real} - simple arithmetic functions, have as an argument two elements, both of them are either integer or real, if one the elements is real, the resulting value also be real, in other way it will be integer.
```
> (plus 5 6)
11
> (minus 10 2)
8
> (plus 2.0 3.1)
5.1
> (let a:Real 5.5)
> (let b:Real 3.5)
> (plus a b)
9
> (mul 3 3)
9
> (div 10 2)
5
> (div 10 3)
3
> (div 10.0 3.0)
3.333
```
### List operations
* (list Any*) -> List - Function can have various types, and it will build and return a list with elements in a given order.
* (head List) -> Any - Element should be a list, the function will return the first element of the given list.
* (tail List) -> Any - Element should be a list, the function will return the last element of a given list.
* (rest List) -> List - Element should be a list, the function will return the list without the first Element.
* (empty List) -> Boolean - Element should be a list, the function will return true if list is empty, if not false.
* (append List List) -> List - The function will add a second list to the tail of the first list.
```
> (list “a” “b” “c”)
> ‘(“a” “b” “c”)
> (head '(1 2 3 4 5))
1
> (tail '('abac 5 10 true 1.0))
1.0
> (cons 1 2)
'(1 2)
> (append '(1 2 3 4) '(5 6 7 8))
'(1 2 3 4 5 6 7 8)
```
### Array operators
* (array Any*) -> Array - Array build function can have just one type, and it will build and return an array with elements in a given order.
* (array-rem Array Integer) -> Array - Array function that removes element in a given array and returns it.
* (array-get Array Integer) -> Any - Array function that returns element in a given index.
* (array-add Array Integer Any) -> Array - Array function that inserts element in position.
* (array-set Array Integer Any) -> Array - Array function that changes value of element in some position to another value.
```
> (array 1 2 3 4 5)
'[1 2 3 4 5]
> (array-rem '[1 2 3 4 5] 2)
'[1 2 4 5]
> (array-get '[1 2 3 4 5] 4)
5
> (array-add '["a" "b" "d"] 2 "c")
'["a" "b" "c" "d"]
> (array-set '[1 2 3 4 5] 0 10)
'[10 2 3 4 5]
```
### Record operators
* (record (Keyword Any)*) -> Record - Record build function, can contain several (name-value) fields, each field is a set of keywords and its value.
* (record-keywords Record) -> List - Return a list of keys with a given record.
* (record-values Record) -> List - Return list of values with a given record.
* (record-field Record Keyword) -> Any - Return value of key in a record.
```
> (let rec (record (#name "Dias") (#age 20) (#city Innopolis)))
> rec
(record #age:20 #city:Innopolis #name:"Dias")
> (record-keywords rec)
‘(#age #city #name)
> (record-values rec)
'(20 Innopolis "Dias")
> (record-field rec #name)
"Dias"
```
### Comparison functions
* ({equal \| nonequal \| less \| lesseq \| greater \| greatereq} Element Element) - simple comparison functions, that compares two elements. Elements should be either real or integer. And the resulting value will be true or false.
```
> (less 5 10)
true
> (equal 'abac' 'abac')
true
> (equal 5 5.0)
false
```
### Predicates
* ({isint\|isreal\|isbool\|islist\|isstring} Any) -> Boolean - the functions return a boolean value: true, if the argument is of type that the function expects, and false otherwise.
```
> (isint 5)
true
> (isbool 'true')
false
```
### Logical operators
* ({or\|and\|xor} Boolean Boolean) -> Boolean and (not Boolean) -> Boolean - the functions perform usual logical operators on evaluated arguments and return a boolean value. Elements should be boolean types.
```
> (and true true)
true
> (and (xor true false) (not (or true true)))
false
```
### Input
* (read-line Type) -> Type - user input function, try to read line with given Type, if it can not throw an exception.
```
> (let a (read-line Integer))
# 100
> a
100
```
## A link to the source code of prototype.
The source code can be found [here](https://github.com/DiazzzU/AAC)
## General description of the implementation
* Building the project - to build the project run the *main.cpp* file in the main folder
* Using the prototype - to use the prototype paste your code into *sourcecode.txt* file in the main folder and run the *main.cpp* file
* Valid and invalid programs
Coorect tests checking the features of the language:
```
(define correctTests ()) {
(cond[(checkLetSet) "Let ans set are correct"] [else "Let ans set are incorrect"])
(cond[(checkCond) "Condition is correct"] [else "Condition is incorrect"])
(cond[(checkArithmetic) "Arithmetic is correct"] [else "Arithmetic is incorrect"])
(cond[(checkList) "List is correct"] [else "List is incorrect"])
(cond[(checkFor) "For is correct"] [else "For is incorrect"])
(cond[(checkArray) "Array is correct"] [else "Array is incorrect"])
(cond[(checkRecord) "Record is correct"] [else "Record is incorrect"])
(cond[(checkComp) "Comparison is correct"] [else "Comparison is incorrect"])
(cond[(checkPred) "Predicate is correct"] [else "Predicate is incorrect"])
(cond[(checkLogic) "Logic is correct"] [else "Logic is incorrect"])
}
(define checkLetSet ()) {
(let v:Integer 6)
(let a:Boolean (equal v 6))
(set v 10)
(let b:Boolean (equal v 10))
(return (and a b))
}
(define checkCond ()) {
(return (cond [true true] [else false]))
}
(define checkFor ()) {
(let a:Integer 0)
(for i '(1 2 3)) {
(set a (plus a i))
}
(let f:Boolean (equal a 6))
(set a 0)
(let l:List (for/list (i '(1 2 3)) (plus i i)))
(let t:Boolean (equal (head l) 2))
(return (and f t))
}
(define checkArithmetic ()) {
(let a:Boolean (equal (plus 3 5) 8))
(let b:Boolean (equal (minus 8 10) -2))
(let c:Boolean (equal (mul 3 5) 15))
(let d:Boolean (equal (div 9 3) 3.0))
(let e:Boolean (equal (plus 3.1 5.6) 8.7))
(let f:Boolean (equal (minus 3.8 2.3) 1.5))
(let g:Boolean (equal (mul 1.2 8.2) 9.84))
(return (and a (and b (and c (and d (and e (and f g)))))))
}
(define checkList ()) {
(let l:List (list "a" 1 2 3))
(let a:Boolean (equal (head l) "a"))
(let b:Boolean (equal (tail l) 3))
(let c:Boolean (equal (plus (head (rest l)) (tail (rest l))) 4))
(let d:Boolean (not (empty l)))
(let n:List (append '(1 2 3) '(4 5 6)))
(let e:Boolean (equal (plus (head n) (tail n)) 7))
(return (and a (and b (and c (and d e)))))
}
(define checkArray ()) {
(let ar:Array (array 1 2 3 4 5))
(let a:Boolean (equal (array-get ar 2) 3))
(set ar (array-rem ar 2))
(let b:Boolean (equal (array-get ar 2) 4))
(set ar (array-add ar 2 9))
(let c:Boolean (equal (array-get ar 2) 9))
(set ar (array-set ar 2 0))
(let d:Boolean (equal (array-get ar 2) 0))
(return (and a (and b (and c d))))
}
(define checkRecord()) {
(let rec:Record (record (#name "Dias") (#age "25") (#city "Innopolis")))
(let keys:List (record-keywords rec))
(let val:List (record-values rec))
(let a:Boolean (equal (head val) "25"))
(let b:Boolean (equal (tail val) "Dias"))
(let one:String (record-field rec (head keys)))
(let c:Boolean (equal one "25"))
(return (and a (and b c)))
}
(define checkComp()) {
(let a:Boolean (equal 1 1))
(let b:Boolean (nonequal 1 1))
(let c:Boolean (less 5 1))
(let d:Boolean (lesseq 3 7))
(let e:Boolean (greater 5 2))
(let f:Boolean (greatereq 4 4))
(return (and a (and (not b) (and (not c) (and d (and e f))))))
}
(define checkPred ()) {
(let a:Boolean (isint 5.5))
(let b:Boolean (isint 4))
(let c:Boolean (isreal 5.0))
(let d:Boolean (isbool true))
(let e:Boolean (islist (array 3 4 5)))
(let f:Boolean (isstring "true"))
(return (and (not a) (and b (and c (and d (and (not e) f))))))
}
(define checkLogic ()) {
(let a:Boolean (or true false))
(let b:Boolean (or true true))
(let c:Boolean (or false false))
(let d:Boolean (and true false))
(let e:Boolean (and true true))
(let f:Boolean (and false false))
(let g:Boolean (not true))
(let h:Boolean (not false))
(let i:Boolean (or false true))
(let j:Boolean (and false true))
(return (and a (and b (and (not c) (and (not d) (and e (and (not f) (and (not g) (and h (and i (not j)))))))))))
}
(correctTests)
```
Incorrect tests
* Incorrect type assignmnent
```
(let v:Integer "start")
```
* Wrong number of arguments
```
(mul 1 2 3)
```
* Incorrect argument: trying to get an element from array that does not exist
```
(let ar:Array (array 1 2 3 4 5))
(array-get ar 10)
```
* Incorrect type of passed argument
```
(plus 5 "add")
```
* Incorrect number of paranthesis
```
(let a:Integer (plus 5 3)))
```
## Sketch of a demo program
It is a guess a number game. We have an array of numbers from 0 to 99, user enters some number, we convert it to another number and it will be an index of the hidden number from the array. Then the user has 5 attempts to guess the number, the program will give hints if the hidden number is greater or less than the user guess.
```
define create (field:Array)){
(for i 100){
(set field (array-add field 0 i))
}
(return field)
}
(define guess-number ()){
(let field:Array (array))
(set field (create field))
"Welcome to the game, enter a number from 0 to 10"
(let num:Integer (read-line Integer))
(set num (minus (mul (plus num 9) 5) (plus num 8)))
(let number:Integer (array-get field num))
"Ok, now you have 5 attempts to guess the number"
(let win:Integer 0)
(let check:Integer 0)
(for i 5){
(cond [(equal win check) (set win (play number))])
}
(cond [(equal win check) "You lose"] [else "You win"])
}
(define play(number:Integer)){
"Your guess"
(let guessed:Integer 0)
(let user-number:Integer (read-line Integer))
(cond [(equal user-number number) (set guessed 1)])
(cond [(less user-number number) "Greater"])
(cond [(greater user-number number) "Less"])
(return guessed)
}
(guess-number)
```