-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.malin
More file actions
125 lines (98 loc) · 1.74 KB
/
Copy pathplotter.malin
File metadata and controls
125 lines (98 loc) · 1.74 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
fun main(): i32 {
// start
printNextLine();
printLine();
printNextLine();
// plot x^2 from -11 to 11
plotFunction(start=-11, end=11);
// end
printNextLine();
printLine();
printNextLine();
return 0;
}
/**
* Plot function x^2
*/
fun plotFunction(start: i32, end: i32) {
// for each x from start to end
let x = start;
while x <= end {
let funcVal = x * x;
// print counter value and funcVal
// counter : funcVal -------
printNumbersDigits(x);
printColon();
printTab();
printTab();
printBar(funcVal);
printNextLine();
// next
x = x+1;
}
}
fun printBar(value: i32) {
if (value != 0 && !(value < 0)) {
printMinus();
printBar(value - 1);
}
}
/**
* Print a integer to the console.
*/
fun printNumber(number: i32) {
printNumbersDigits(number);
printNextLine();
}
/**
* Print digits of an integer to the console.
*/
fun printNumbersDigits(number: i32) {
// tread negative
if number < 0 {
number = number * -1;
printMinus();
}
// print lower digit
if number >= 10 {
printNumbersDigits(number / 10);
}
// get lowest digit
let digit = modulo(number, by= 10);
// print one digit 0..9
if (0 <= digit && digit <= 9) {
putChar(c = digit + 48);
}
}
fun printNextLine() {
putChar(10);
}
fun printTab() {
putChar(9);
}
fun printSpace() {
putChar(32);
}
fun printColon() {
putChar(58);
}
fun printMinus() {
putChar(45);
}
fun printLine(length: i32 = 20) {
printMinus();
if length > 1 {
printLine(length - 1);
}
else {
printNextLine();
}
}
fun modulo(number: i32, by: i32): i32 {
return number - (number / by) * by;
}
/**
* Print a char in the console.
* Uses extern c putChar.
*/
fun extern putChar(c: i32)