-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd.kr
73 lines (60 loc) · 1.21 KB
/
std.kr
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
def throw() {
#c printf("Error")
#c exit(1)
}
def |x: i32| as_i32() { x }
def |x: i32| memsize() {
#c x = sizeof(i32);
x
}
def alloc(size: i32) {
let x: ptr = 0
#c x = malloc(size);
x
}
def drop(address: ptr) {
#c free(address);
}
def print(x: i32) {
#c printf("%d", x)
}
def println(x: i32) {
#c printf("%d\n", x)
}
def array(length: i32) {
let pointer: ptr = alloc((length) * length:memsize() + length:memsize())
#c pointer[0] = length
pointer
}
def |ary: ptr| at(idx: i32) {
let el: i32 = 0
#c el = (i32) ary[idx+1];
el
}
def |ary: ptr| length() {
let len: i32 = 0
#c if (ary == NULL) { printf("\nnull pointer exception\n");exit(1); }
#c len = ary[0]
len
}
def |ary: ptr| set(idx: i32, el: i32) {
if (idx - 1 > ary:length()) {
throw()
#
}
#c ary[idx+1] = el;
}
def |ary: ptr| fill(value: i32, start_idx: i32) {
if (start_idx < ary:length()) {
ary:set(start_idx, value)
ary:fill(value, start_idx+1)
}
ary
}
def |ary: ptr| clone() {
let cloned = array(ary:length())
let len = ary:length()
#c cloned[0] = len
#c for (int i=1; i < len; i++) { cloned[i] = ary[i]; };
cloned
}