-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource-language-description
103 lines (57 loc) · 3.3 KB
/
source-language-description
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Source Language Documentation
Source language of uni-compiler is in fact fragment of C language.
It is an imperative, statically typed, lexically scoped and memory-unsafe language. It has simple control-flow constructs, recursion, one-dimensional arrays syntax, pointers and so on.
Main function is needed when compiling project (Place it where to start computation).
## Types
### Basic Data Types
4 basic data types: void, signed integer int, char, floating-point numbers float (single precision). Integers are written in hexadecimal format. Strings are represented by array of chars.
### Relational Operators
Numbers can be compared using <, <=, >, >=, == and !=. Bools can be compared with == and !=. Relational operators has always two operands, we cannot chain them in longer chains, only with parentheses.
### Unary and Binary Operators
Unary - negates the sign of a number. ! negates a boolean and ~ does bitwise negation of a number.
Language supports classical numbers binary operators -, +, *, /, % and also bitshifts >> and << and bitwise operators &, ^, |.
There are also boolean binary operators && and ||.
In the end we will also use assignement operator =.
There is also special unary operator sizeof(Expression) which looks like a function. It can be used for variables or data types to return its size.
### Arrays
Same as in classical C language we can define arrays both on stack and on heap.
One-dimensional array on stack:
uint8 array_name[3] = {3, 10, 49};
Indexing:
uint8 var = array_name[2]; // == 49
Multi-dimensional array on stack:
uint8 array_name[2][3] = {
{2, 4, 9},
{3, 1, 7}
};
### Pointers
Same as in C we have pointer type for every type: int *pointer_to_int. Name of a defined array is also pointer automatically. * operator is used for dereferencing, while & is used for getting address (which is exactly what pointer stores) of a variable. Language provides pointer aritmetics so you can traverse memory based on type of pointer.
### Conversions
Implicit conversions are happening only when there is no data loss, for example from char to int, but that is all. Arrays cannot be implicitly converted and other conversions can do explicitly a programmer:
int first = 159;
char second = (char) first;
## Control-Flow
Our language has for loops and while loops, if statements and classical function calls.
### Loops
For loop:
for (int i = 0; i < 10; i = i + 1) { ... }
While loop:
while ([bool condition]) { ... }
### If Statements
if ([bool condition]) { ... }
or also with else:
if ([bool condition]) { ... } else { ... }
### Functions
Our functions cannot be declared forwardly, they need some return type and arguments and explicit return value if they are not of type void. That means that mutual recursion is not possible. Which is safer and it makes code more readable.
int add(int, int);
int add(int a, int b) {
return a + b;
}
Most important is main function which has int retunr type (1 when error, 0 when success) and zero positional arguments, when there is return statement in main, then whole program ends.
## Other implemented functions
void* malloc(int size); (classical memory allocation)
void* calloc(int size_of_elem, int count); (allocation with zeros in memory)
void* realloc(int new_size);
bool free(void *memory); (frees memory)
int putchar(char letter);
int getchar();