-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys.n
126 lines (105 loc) · 2.66 KB
/
sys.n
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
126
import boot.loader
class sys {
priv:
static fn native loadLibrary0(n)
pub:
static fn loadLibrary(name) {
loadLibrary0(name)
}
}
class range {
priv:
from, to, step
pub:
new(f, t, s) {
from = f
to = t
step = s
}
fn next() {
if(from != to) {
from += step
}
ret from
}
}
// Mirrors a native array
class list {
priv:
elements
fn native arr_new0(size)
fn native arr_at0(arr, idx)
fn native arr_assign0(arr, idx, val)
fn native arr_length0(arr)
pub:
static { // only static variables, methods
// and classes are accessible
sys.loadLibrary("nextarr.so")
}
// constructor
new(s) {
elements = arr_new0(s)
}
len() {
ret arr_length0(elements)
}
op [](x) { // element access -> x = list[i]
ret arr_at0(elements, x)
}
op [](x, val) { // element assign -> list[i] = x
ret arr_assign0(elements, idx, val)
}
// typical getter
// can be used as member reference directly
// e = list.size
// Moreover, if it has the same name
// as a member of the class, an empty body
// will automatically return the value
// of the member.
// Ideally, the function call should be optimized
// in that case.
get size {}
// typical setter
// can be used as member reference directly
// list.name = "s"
// Moreover, if it has the same name
// as a member of the class, an empty body
// will automatically set the value to the member
set name(x) {}
}
// all members are static
static class io {
priv:
fn native print0(x)
fn native readline0()
fn native read0()
fn native readString0()
pub:
static { // executed when the class is loaded to the vm
sys.loadLibrary("nextio.so")
}
fn print(expr...) {
for(exp in expr) {
print0(exp)
}
// it will be desugared like
// num = expr.len()
// for(i = 0;i < num;i++) {
// exp = expr[i]
// print0(exp)
// }
}
fn readline() {
ret readline0()
}
fn read() {
ret read0()
}
fn readString() {
ret readString0()
}
op <<(x) {
print(x)
ret this
}
}