@effekt-lang/effekt 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/effekt +0 -0
- package/libraries/common/array.effekt +42 -31
- package/libraries/common/bytearray.effekt +22 -2
- package/libraries/common/effekt.effekt +5 -0
- package/libraries/common/io/filesystem.effekt +2 -2
- package/libraries/common/stream.effekt +419 -0
- package/libraries/common/string.effekt +0 -13
- package/libraries/js/effekt_runtime.js +18 -18
- package/libraries/llvm/bytearray.c +4 -4
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +2 -2
- package/package.json +1 -1
package/bin/effekt
CHANGED
|
Binary file
|
|
@@ -77,30 +77,7 @@ extern global def unsafeSet[T](arr: Array[T], index: Int, value: T): Unit =
|
|
|
77
77
|
ret %Pos %z
|
|
78
78
|
"""
|
|
79
79
|
|
|
80
|
-
/// Creates a copy of `arr`
|
|
81
|
-
def copy[T](arr: Array[T]): Array[T] = {
|
|
82
|
-
with on[OutOfBounds].default { <> }; // should not happen
|
|
83
|
-
val len = arr.size;
|
|
84
|
-
val newArray = allocate[T](len);
|
|
85
|
-
copy[T](arr, 0, newArray, 0, len);
|
|
86
|
-
newArray
|
|
87
|
-
}
|
|
88
80
|
|
|
89
|
-
/// Copies `length`-many elements from `from` to `to`
|
|
90
|
-
/// starting at `start` (in `from`) and `offset` (in `to`)
|
|
91
|
-
def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
|
|
92
|
-
val startValid = start >= 0 && start + length <= from.size
|
|
93
|
-
val offsetValid = offset >= 0 && offset + length <= to.size
|
|
94
|
-
|
|
95
|
-
def go(i: Int, j: Int, length: Int): Unit =
|
|
96
|
-
if (length > 0) {
|
|
97
|
-
to.unsafeSet(j, from.unsafeGet(i))
|
|
98
|
-
go(i + 1, j + 1, length - 1)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (startValid && offsetValid) go(start, offset, length)
|
|
102
|
-
else do raise(OutOfBounds(), "Array index out of bounds, when copying")
|
|
103
|
-
}
|
|
104
81
|
|
|
105
82
|
// Derived operations:
|
|
106
83
|
|
|
@@ -126,6 +103,44 @@ def build[T](size: Int) { index: Int => T }: Array[T] = {
|
|
|
126
103
|
arr
|
|
127
104
|
}
|
|
128
105
|
|
|
106
|
+
def resize[T](source: Array[T], size: Int): Array[T] = {
|
|
107
|
+
val target = allocate(size)
|
|
108
|
+
val n = min(source.size, target.size)
|
|
109
|
+
def go(i: Int): Array[T] =
|
|
110
|
+
if (i < n) {
|
|
111
|
+
target.unsafeSet(i, source.unsafeGet(i))
|
|
112
|
+
go(i + 1)
|
|
113
|
+
} else {
|
|
114
|
+
target
|
|
115
|
+
}
|
|
116
|
+
go(0)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Creates a copy of `arr`
|
|
121
|
+
*/
|
|
122
|
+
def copy[T](array: Array[T]): Array[T] =
|
|
123
|
+
array.resize(array.size)
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Copies `length`-many elements from `from` to `to`
|
|
127
|
+
* starting at `start` (in `from`) and `offset` (in `to`)
|
|
128
|
+
*/
|
|
129
|
+
def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
|
|
130
|
+
val startValid = start >= 0 && start + length <= from.size
|
|
131
|
+
val offsetValid = offset >= 0 && offset + length <= to.size
|
|
132
|
+
|
|
133
|
+
def go(i: Int, j: Int, length: Int): Unit =
|
|
134
|
+
if (length > 0) {
|
|
135
|
+
to.unsafeSet(j, from.unsafeGet(i))
|
|
136
|
+
go(i + 1, j + 1, length - 1)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (startValid && offsetValid) go(start, offset, length)
|
|
140
|
+
else do raise(OutOfBounds(), "Array index out of bounds, when copying")
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
129
144
|
// Utility functions:
|
|
130
145
|
|
|
131
146
|
def toList[T](arr: Array[T]): List[T] = {
|
|
@@ -140,26 +155,22 @@ def toList[T](arr: Array[T]): List[T] = {
|
|
|
140
155
|
|
|
141
156
|
def foreach[T](arr: Array[T]){ action: T => Unit }: Unit =
|
|
142
157
|
each(0, arr.size) { i =>
|
|
143
|
-
|
|
144
|
-
action(x)
|
|
158
|
+
action(arr.unsafeGet(i))
|
|
145
159
|
}
|
|
146
160
|
|
|
147
161
|
def foreach[T](arr: Array[T]){ action: (T) {Control} => Unit }: Unit =
|
|
148
162
|
each(0, arr.size) { (i) {label} =>
|
|
149
|
-
|
|
150
|
-
action(x) {label}
|
|
163
|
+
action(arr.unsafeGet(i)) {label}
|
|
151
164
|
}
|
|
152
165
|
|
|
153
166
|
def foreachIndex[T](arr: Array[T]){ action: (Int, T) => Unit }: Unit =
|
|
154
167
|
each(0, arr.size) { i =>
|
|
155
|
-
|
|
156
|
-
action(i, x)
|
|
168
|
+
action(i, arr.unsafeGet(i))
|
|
157
169
|
}
|
|
158
170
|
|
|
159
171
|
def foreachIndex[T](arr: Array[T]){ action: (Int, T) {Control} => Unit }: Unit =
|
|
160
172
|
each(0, arr.size) { (i) {label} =>
|
|
161
|
-
|
|
162
|
-
action(i, x) {label}
|
|
173
|
+
action(i, arr.unsafeGet(i)) {label}
|
|
163
174
|
}
|
|
164
175
|
|
|
165
176
|
def sum(list: Array[Int]): Int = {
|
|
@@ -50,13 +50,33 @@ def resize(source: ByteArray, size: Int): ByteArray = {
|
|
|
50
50
|
go(0)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
def foreach(arr: ByteArray){ action: Byte => Unit }: Unit =
|
|
54
|
+
each(0, arr.size) { i =>
|
|
55
|
+
action(arr.unsafeGet(i))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
def foreach(arr: ByteArray){ action: (Byte) {Control} => Unit }: Unit =
|
|
59
|
+
each(0, arr.size) { (i) {label} =>
|
|
60
|
+
action(arr.unsafeGet(i)) {label}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def foreachIndex(arr: ByteArray){ action: (Int, Byte) => Unit }: Unit =
|
|
64
|
+
each(0, arr.size) { i =>
|
|
65
|
+
action(i, arr.unsafeGet(i))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
def foreachIndex(arr: ByteArray){ action: (Int, Byte) {Control} => Unit }: Unit =
|
|
69
|
+
each(0, arr.size) { (i) {label} =>
|
|
70
|
+
action(i, arr.unsafeGet(i)) {label}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
extern pure def fromString(str: String): ByteArray =
|
|
54
74
|
js "(new TextEncoder().encode(${str}))"
|
|
55
75
|
llvm """
|
|
56
76
|
ret %Pos ${str}
|
|
57
77
|
"""
|
|
58
78
|
|
|
59
|
-
extern pure def
|
|
79
|
+
extern pure def toString(arr: ByteArray): String =
|
|
60
80
|
js "(new TextDecoder('utf-8').decode(${arr}))"
|
|
61
81
|
// assuming the buffer is already in UTF-8
|
|
62
82
|
llvm """
|
|
@@ -566,6 +566,11 @@ extern pure def bitwiseAnd(x: Int, y: Int): Int =
|
|
|
566
566
|
chez "(logand ${x} ${y})"
|
|
567
567
|
llvm "%z = and %Int ${x}, ${y} ret %Int %z"
|
|
568
568
|
|
|
569
|
+
extern pure def bitwiseOr(x: Int, y: Int): Int =
|
|
570
|
+
js "(${x} | ${y})"
|
|
571
|
+
chez "(logior ${x} ${y})"
|
|
572
|
+
llvm "%z = or %Int ${x}, ${y} ret %Int %z"
|
|
573
|
+
|
|
569
574
|
extern pure def bitwiseXor(x: Int, y: Int): Int =
|
|
570
575
|
js "(${x} ^ ${y})"
|
|
571
576
|
chez "(logxor ${x} ${y})"
|
|
@@ -44,7 +44,7 @@ def readFile(path: String): String / Exception[IOError] = {
|
|
|
44
44
|
def go(): String = {
|
|
45
45
|
read(file, buffer, offset, chunkSize, -1) match {
|
|
46
46
|
case 0 =>
|
|
47
|
-
buffer.resize(offset).
|
|
47
|
+
buffer.resize(offset).toString
|
|
48
48
|
case n =>
|
|
49
49
|
offset = offset + n
|
|
50
50
|
if (offset + chunkSize > buffer.size) {
|
|
@@ -63,7 +63,7 @@ def writeFile(path: String, contents: String): Unit / Exception[IOError] = {
|
|
|
63
63
|
with on[IOError].finalize { close(file) }
|
|
64
64
|
|
|
65
65
|
val chunkSize = 1048576 // 1MB
|
|
66
|
-
val buffer = contents.
|
|
66
|
+
val buffer = contents.fromString
|
|
67
67
|
var offset = 0;
|
|
68
68
|
|
|
69
69
|
def go(): Unit = {
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
module stream
|
|
2
|
+
|
|
3
|
+
import array
|
|
4
|
+
import bytearray
|
|
5
|
+
import io/filesystem
|
|
6
|
+
import io/error
|
|
7
|
+
|
|
8
|
+
// Effects
|
|
9
|
+
// -------
|
|
10
|
+
|
|
11
|
+
/// Describes push streams by emitting values of type `A`.
|
|
12
|
+
effect emit[A](value: A): Unit
|
|
13
|
+
|
|
14
|
+
/// Describes pull streams by requiring values of type `A`.
|
|
15
|
+
///
|
|
16
|
+
/// The producer can decide to `stop` emitting values.
|
|
17
|
+
effect read[A](): A / stop
|
|
18
|
+
|
|
19
|
+
/// Signal from a producer to a consumer that there are no further values.
|
|
20
|
+
effect stop(): Nothing
|
|
21
|
+
|
|
22
|
+
/// Canonical handler of push streams that performs `action` for every
|
|
23
|
+
/// value emitted by `stream`.
|
|
24
|
+
def for[A, R] { stream: () => R / emit[A] } { action: A => Unit }: R =
|
|
25
|
+
try {
|
|
26
|
+
stream()
|
|
27
|
+
} with emit[A] { value =>
|
|
28
|
+
resume(action(value))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Like `for[A, R]`, but fixes the result of producing computation to the
|
|
32
|
+
/// common type `Unit`. This simplifies annotating the type of stream
|
|
33
|
+
/// elements `A` without having to also annotate `R`.
|
|
34
|
+
///
|
|
35
|
+
/// e.g. for[Int] { prog() } { el => println(el) }
|
|
36
|
+
def for[A] { stream: () => Unit / emit[A] } { action: A => Unit }: Unit =
|
|
37
|
+
for[A, Unit]{stream}{action}
|
|
38
|
+
|
|
39
|
+
def continually[A] { action: () => A }: Nothing / emit[A] = {
|
|
40
|
+
def go(): Nothing = {
|
|
41
|
+
do emit(action())
|
|
42
|
+
go()
|
|
43
|
+
}
|
|
44
|
+
go()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Turns a `list` into a producer of a push stream
|
|
48
|
+
/// by emitting each contained value left-to-right.
|
|
49
|
+
def each[A](list: List[A]): Unit / emit[A] =
|
|
50
|
+
list match {
|
|
51
|
+
case Nil() => ()
|
|
52
|
+
case Cons(head, tail) =>
|
|
53
|
+
do emit(head)
|
|
54
|
+
each(tail)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// Turns an `array` into a producer of a push stream
|
|
58
|
+
/// by emitting each contained value from 0 to length - 1.
|
|
59
|
+
def each[T](array: Array[T]): Unit / emit[T] = {
|
|
60
|
+
val n = array.size
|
|
61
|
+
def go(i: Int): Unit = {
|
|
62
|
+
if (i < n) {
|
|
63
|
+
do emit(array.unsafeGet(i))
|
|
64
|
+
go(i + 1)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
go(0)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Turns `bytes` into a producer of a push stream
|
|
71
|
+
/// by emitting each contained value from 0 to length - 1.
|
|
72
|
+
def each(bytes: ByteArray): Unit / emit[Byte] = {
|
|
73
|
+
val n = bytes.size
|
|
74
|
+
def go(i: Int): Unit = {
|
|
75
|
+
if (i < n) {
|
|
76
|
+
do emit(bytes.unsafeGet(i))
|
|
77
|
+
go(i + 1)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
go(0)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// not option
|
|
84
|
+
// not dequeue
|
|
85
|
+
// not queue
|
|
86
|
+
// not result
|
|
87
|
+
// not seq
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def boundary[R] { program: () => R / stop }: Option[R] =
|
|
91
|
+
try {
|
|
92
|
+
Some(program())
|
|
93
|
+
} with stop {
|
|
94
|
+
def stop() = None()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
def exhaustively[A] { program: () => A / stop } { action: A => Unit }: Unit =
|
|
98
|
+
try {
|
|
99
|
+
def go(): Unit = {
|
|
100
|
+
action(program())
|
|
101
|
+
go()
|
|
102
|
+
}
|
|
103
|
+
go()
|
|
104
|
+
} with stop {
|
|
105
|
+
def stop() = ()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
// In Effekt lower bounds are inclusive and upper bounds are exclusive
|
|
110
|
+
|
|
111
|
+
record Indexed[A](index: Int, value: A)
|
|
112
|
+
|
|
113
|
+
def range(lower: Int, upper: Int): Unit / emit[Int] =
|
|
114
|
+
if (lower < upper) {
|
|
115
|
+
do emit(lower)
|
|
116
|
+
range(lower + 1, upper)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
def rangeTo(upper: Int): Unit / emit[Int] =
|
|
120
|
+
range(0, upper)
|
|
121
|
+
|
|
122
|
+
def rangeFrom(lower: Int): Unit / emit[Int] = {
|
|
123
|
+
do emit(lower)
|
|
124
|
+
rangeFrom(lower + 1)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def index[A, R] { stream: () => R / emit[A] }: R / emit[Indexed[A]] = {
|
|
129
|
+
var i = 0;
|
|
130
|
+
try {
|
|
131
|
+
stream()
|
|
132
|
+
} with emit[A] { v =>
|
|
133
|
+
val c = i;
|
|
134
|
+
i = c + 1;
|
|
135
|
+
resume(do emit(Indexed(c, v)))
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/// If `number` is zero or negative it does nothing
|
|
140
|
+
def take[A, R](number: Int) { stream: () => R / emit[A] }: R / { emit[A], stop } = {
|
|
141
|
+
if (number <= 0) do stop();
|
|
142
|
+
var i = 1;
|
|
143
|
+
try {
|
|
144
|
+
stream()
|
|
145
|
+
} with emit[A] { v =>
|
|
146
|
+
if (i < number) {
|
|
147
|
+
i = i + 1;
|
|
148
|
+
resume(do emit(v))
|
|
149
|
+
} else {
|
|
150
|
+
do stop()
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// If `number` is zero or negative it does nothing
|
|
156
|
+
def replicate[A](number: Int) { action: () => A }: Unit / emit[A] =
|
|
157
|
+
if (number > 0) {
|
|
158
|
+
do emit(action())
|
|
159
|
+
replicate(number - 1) {action}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def sum[R] { stream : () => R / emit[Int] }: (R, Int) = {
|
|
164
|
+
var s = 0;
|
|
165
|
+
try {
|
|
166
|
+
(stream(), s)
|
|
167
|
+
} with emit[Int] { v =>
|
|
168
|
+
s = s + v;
|
|
169
|
+
resume(())
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
def sum { stream : () => Unit / emit[Int] }: Int =
|
|
174
|
+
sum[Unit]{stream}.second
|
|
175
|
+
|
|
176
|
+
def collectList[A, R] { stream: () => R / emit[A] }: (R, List[A]) =
|
|
177
|
+
try {
|
|
178
|
+
(stream(), Nil())
|
|
179
|
+
} with emit[A] { (v) =>
|
|
180
|
+
val (r, vs) = resume(());
|
|
181
|
+
(r, Cons(v, vs))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
def collectList[A] { stream: () => Unit / emit[A] }: List[A] =
|
|
185
|
+
collectList[A, Unit]{stream}.second
|
|
186
|
+
|
|
187
|
+
def collectArray[A, R] { stream: () => R / emit[A] }: (R, Array[A]) = {
|
|
188
|
+
var i = 0
|
|
189
|
+
var a = array::allocate(1)
|
|
190
|
+
try {
|
|
191
|
+
(stream(), a.resize(i))
|
|
192
|
+
} with emit[A] { (v) =>
|
|
193
|
+
if (i >= a.size) { a = a.resize(2 * a.size) }
|
|
194
|
+
a.unsafeSet(i, v)
|
|
195
|
+
i = i + 1
|
|
196
|
+
resume(())
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
def collectArray[A] { stream: () => Unit / emit[A] }: Array[A] =
|
|
201
|
+
collectArray[A, Unit]{stream}.second
|
|
202
|
+
|
|
203
|
+
def collectBytes[R] { stream: () => R / emit[Byte] }: (R, ByteArray) = {
|
|
204
|
+
var i = 0
|
|
205
|
+
var a = bytearray::allocate(1)
|
|
206
|
+
try {
|
|
207
|
+
(stream(), a.resize(i))
|
|
208
|
+
} with emit[Byte] { (v) =>
|
|
209
|
+
if (i >= a.size) { a = a.resize(2 * a.size) }
|
|
210
|
+
a.unsafeSet(i, v)
|
|
211
|
+
i = i + 1
|
|
212
|
+
resume(())
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
def collectBytes { stream: () => Unit / emit[Byte] }: ByteArray =
|
|
217
|
+
collectBytes[Unit]{stream}.second
|
|
218
|
+
|
|
219
|
+
def feed[T](list: List[T]) { reader: () => Unit / read[T] } = {
|
|
220
|
+
var l = list
|
|
221
|
+
try {
|
|
222
|
+
reader()
|
|
223
|
+
} with read[T] {
|
|
224
|
+
resume {
|
|
225
|
+
l match {
|
|
226
|
+
case Nil() => do stop()
|
|
227
|
+
case Cons(value, rest) =>
|
|
228
|
+
l = rest
|
|
229
|
+
return value
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
def feed[T](array: Array[T]) { reader: () => Unit / read[T] } = {
|
|
236
|
+
var i = 0
|
|
237
|
+
try {
|
|
238
|
+
reader()
|
|
239
|
+
} with read[T] {
|
|
240
|
+
resume {
|
|
241
|
+
if (i < array.size) {
|
|
242
|
+
array.unsafeGet(i)
|
|
243
|
+
} else {
|
|
244
|
+
do stop()
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
def feed(bytes: ByteArray) { reader: () => Unit / read[Byte] } = {
|
|
251
|
+
var i = 0
|
|
252
|
+
try {
|
|
253
|
+
reader()
|
|
254
|
+
} with read[Byte] {
|
|
255
|
+
resume {
|
|
256
|
+
if (i < bytes.size) {
|
|
257
|
+
bytes.unsafeGet(i)
|
|
258
|
+
} else {
|
|
259
|
+
do stop()
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
def source[A, R] { stream: () => Unit / emit[A] } { reader: () => R / read[A] }: R = {
|
|
266
|
+
var next = box { None() }
|
|
267
|
+
next = box {
|
|
268
|
+
try {
|
|
269
|
+
stream()
|
|
270
|
+
None()
|
|
271
|
+
} with emit[A] { (v) =>
|
|
272
|
+
next = box { resume(()) }
|
|
273
|
+
Some(v)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
reader()
|
|
278
|
+
} with read[A] {
|
|
279
|
+
resume {
|
|
280
|
+
next().getOrElse { do stop() }
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def writeFile[R](path: String) { stream: () => R / emit[Byte] }: R / Exception[IOError] = {
|
|
287
|
+
|
|
288
|
+
val file = open(path, WriteOnly());
|
|
289
|
+
with on[IOError].finalize { close(file) }
|
|
290
|
+
|
|
291
|
+
val chunkSize = 1048576 // 1MB
|
|
292
|
+
val buffer = bytearray::allocate(chunkSize)
|
|
293
|
+
var offset = 0
|
|
294
|
+
|
|
295
|
+
def push(i: Int, n: Int): Unit = {
|
|
296
|
+
val r = write(file, buffer, i, n, -1)
|
|
297
|
+
if (r < n) {
|
|
298
|
+
push(i + r, n - r)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
val r = stream()
|
|
304
|
+
push(0, offset)
|
|
305
|
+
return r
|
|
306
|
+
} with emit[Byte] { (byte) =>
|
|
307
|
+
if (offset >= buffer.size) { push(0, buffer.size) }
|
|
308
|
+
buffer.unsafeSet(offset, byte)
|
|
309
|
+
offset = offset + 1
|
|
310
|
+
resume(())
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
def readFile[R](path: String) { reader: () => R / read[Byte] }: R / Exception[IOError] = {
|
|
315
|
+
|
|
316
|
+
val file = open(path, ReadOnly());
|
|
317
|
+
with on[IOError].finalize { close(file) }
|
|
318
|
+
|
|
319
|
+
val chunkSize = 1048576 // 1MB
|
|
320
|
+
val buffer = bytearray::allocate(chunkSize)
|
|
321
|
+
var offset = 0
|
|
322
|
+
var length = 0
|
|
323
|
+
|
|
324
|
+
def pull(): Unit / stop = {
|
|
325
|
+
read(file, buffer, 0, chunkSize, -1) match {
|
|
326
|
+
case 0 =>
|
|
327
|
+
do stop()
|
|
328
|
+
case n =>
|
|
329
|
+
offset = 0
|
|
330
|
+
length = n
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
reader()
|
|
336
|
+
} with read[Byte] {
|
|
337
|
+
resume {
|
|
338
|
+
if (offset >= length) { pull() }
|
|
339
|
+
val byte = buffer.unsafeGet(offset)
|
|
340
|
+
offset = offset + 1
|
|
341
|
+
return byte
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
def decodeChar(): Char / {read[Byte], stop} = {
|
|
347
|
+
val b = do read().toInt
|
|
348
|
+
if (b < 128) {
|
|
349
|
+
b.toChar
|
|
350
|
+
} else if (b < 224) {
|
|
351
|
+
val part1 = bitwiseShl(bitwiseAnd(b, 31), 6)
|
|
352
|
+
val part2 = bitwiseAnd(do read().toInt, 63)
|
|
353
|
+
bitwiseOr(part1, part2).toChar
|
|
354
|
+
} else if (b < 240) {
|
|
355
|
+
val part1 = bitwiseShl(bitwiseAnd(b, 15), 12)
|
|
356
|
+
val part2 = bitwiseShl(bitwiseAnd(do read().toInt, 63), 6)
|
|
357
|
+
val part3 = bitwiseAnd(do read().toInt, 63)
|
|
358
|
+
bitwiseOr(bitwiseOr(part1, part2), part3).toChar
|
|
359
|
+
} else {
|
|
360
|
+
val part1 = bitwiseShl(bitwiseAnd(b, 7), 18)
|
|
361
|
+
val part2 = bitwiseShl(bitwiseAnd(do read().toInt, 63), 12)
|
|
362
|
+
val part3 = bitwiseShl(bitwiseAnd(do read().toInt, 63), 6)
|
|
363
|
+
val part4 = bitwiseAnd(do read().toInt, 63)
|
|
364
|
+
bitwiseOr(bitwiseOr(bitwiseOr(part1, part2), part3), part4).toChar
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
def encodeChar(char: Char): Unit / emit[Byte] = {
|
|
369
|
+
val code = char.toInt
|
|
370
|
+
if (code < 128) {
|
|
371
|
+
do emit(code.toByte)
|
|
372
|
+
} else if (code < 2048) {
|
|
373
|
+
do emit(bitwiseOr(192, bitwiseShr(code, 6)).toByte)
|
|
374
|
+
do emit(bitwiseOr(128, bitwiseAnd(code, 63)).toByte)
|
|
375
|
+
} else if (code < 65536) {
|
|
376
|
+
do emit(bitwiseOr(224, bitwiseShr(code, 12)).toByte)
|
|
377
|
+
do emit(bitwiseOr(128, bitwiseAnd(bitwiseShr(code, 6), 63)).toByte)
|
|
378
|
+
do emit(bitwiseOr(128, bitwiseAnd(code, 63)).toByte)
|
|
379
|
+
} else {
|
|
380
|
+
do emit(bitwiseOr(240, bitwiseShr(code, 18)).toByte)
|
|
381
|
+
do emit(bitwiseOr(128, bitwiseAnd(bitwiseShr(code, 12), 63)).toByte)
|
|
382
|
+
do emit(bitwiseOr(128, bitwiseAnd(bitwiseShr(code, 6), 63)).toByte)
|
|
383
|
+
do emit(bitwiseOr(128, bitwiseAnd(code, 63)).toByte)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
def decodeUTF8[R] { reader: () => R / read[Char] }: R / read[Byte] =
|
|
388
|
+
try {
|
|
389
|
+
reader()
|
|
390
|
+
} with read[Char] {
|
|
391
|
+
resume { decodeChar() }
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
def encodeUTF8[R] { stream: () => R / emit[Char] }: R / emit[Byte] =
|
|
395
|
+
try {
|
|
396
|
+
stream()
|
|
397
|
+
} with emit[Char] { char =>
|
|
398
|
+
resume(encodeChar(char))
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
def feed(string: String) { reader: () => Unit / read[Char] } =
|
|
402
|
+
feed(string.fromString) {
|
|
403
|
+
decodeUTF8 {
|
|
404
|
+
reader()
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
def each(string: String): Unit / emit[Char] =
|
|
409
|
+
feed(string) {
|
|
410
|
+
exhaustively[Char] { do read() } { char => do emit(char) }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
def collectString[R] { stream: () => R / emit[Char] }: (R, String) = {
|
|
414
|
+
val (result, bytes) = collectBytes { encodeUTF8 { stream } }
|
|
415
|
+
(result, bytes.toString)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
def collectString { stream: () => Unit / emit[Char] }: String =
|
|
419
|
+
collectString[Unit]{stream}.second
|
|
@@ -222,15 +222,6 @@ def lastIndexOf(str: String, sub: String, from: Int): Option[Int] = {
|
|
|
222
222
|
// extern pure def unsafeIndexOf(str: String, sub: String): Int =
|
|
223
223
|
// js "${str}.indexOf(${sub})"
|
|
224
224
|
|
|
225
|
-
interface Stream {
|
|
226
|
-
def emit(string: Char): Unit
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
def string[T] { prog: => T / Stream }: (T, String) = <>
|
|
230
|
-
|
|
231
|
-
// TODO implement after concurrent IO (we need to stub `process.stdout` in JS for browsers).
|
|
232
|
-
def printing[T] { prog: => T / Stream }: T = <>
|
|
233
|
-
|
|
234
225
|
|
|
235
226
|
// Characters
|
|
236
227
|
// ----------
|
|
@@ -299,10 +290,6 @@ extern pure def infixGte(x: Char, y: Char): Bool =
|
|
|
299
290
|
ret %Pos %adt_boolean
|
|
300
291
|
"""
|
|
301
292
|
|
|
302
|
-
// TODO iterator over codepoints (maybe use the JS primitive feature???)
|
|
303
|
-
|
|
304
|
-
extern type StringIterator
|
|
305
|
-
|
|
306
293
|
|
|
307
294
|
/**
|
|
308
295
|
* Determines the number of bytes needed by a codepoint
|
|
@@ -23,7 +23,7 @@ function Arena(_region) {
|
|
|
23
23
|
fresh: function(init) {
|
|
24
24
|
const cell = Cell(init);
|
|
25
25
|
// region keeps track what to backup, but we do not need to backup unreachable cells
|
|
26
|
-
region.push(new WeakRef(cell))
|
|
26
|
+
region.push(cell) // new WeakRef(cell))
|
|
27
27
|
return cell;
|
|
28
28
|
},
|
|
29
29
|
region: _region,
|
|
@@ -31,15 +31,14 @@ function Arena(_region) {
|
|
|
31
31
|
// a region aggregates weak references
|
|
32
32
|
const nested = Arena([])
|
|
33
33
|
// this doesn't work yet, since Arena.backup doesn't return a thunk
|
|
34
|
-
region.push(new WeakRef(nested))
|
|
34
|
+
region.push(nested) //new WeakRef(nested))
|
|
35
35
|
return nested;
|
|
36
36
|
},
|
|
37
37
|
backup: function() {
|
|
38
38
|
const _backup = []
|
|
39
39
|
let nextIndex = 0;
|
|
40
40
|
for (const ref of region) {
|
|
41
|
-
|
|
42
|
-
const cell = ref.deref()
|
|
41
|
+
const cell = ref //.deref()
|
|
43
42
|
// only backup live cells
|
|
44
43
|
if (cell) {
|
|
45
44
|
_backup[nextIndex] = cell.backup()
|
|
@@ -50,7 +49,7 @@ function Arena(_region) {
|
|
|
50
49
|
const region = []
|
|
51
50
|
let nextIndex = 0;
|
|
52
51
|
for (const restoreCell of _backup) {
|
|
53
|
-
region[nextIndex] = new WeakRef(restoreCell())
|
|
52
|
+
region[nextIndex] = restoreCell() // new WeakRef(restoreCell())
|
|
54
53
|
nextIndex++
|
|
55
54
|
}
|
|
56
55
|
return Arena(region)
|
|
@@ -114,22 +113,23 @@ function SHIFT(p, body, ks, k) {
|
|
|
114
113
|
cont = { stack: meta.stack, prompt: meta.prompt, backup: meta.arena.backup(), rest: cont }
|
|
115
114
|
meta = meta.rest
|
|
116
115
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
meta = { stack: toRewind.stack, prompt: toRewind.prompt, arena: toRewind.backup(), rest: meta }
|
|
122
|
-
toRewind = toRewind.rest
|
|
123
|
-
}
|
|
116
|
+
const k1 = meta.stack
|
|
117
|
+
meta.stack = null
|
|
118
|
+
return body(cont, meta, k1)
|
|
119
|
+
}
|
|
124
120
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
// Rewind stack `cont` back onto `k` :: `ks` and resume with c
|
|
122
|
+
function RESUME(cont, c, ks, k) {
|
|
123
|
+
let meta = { stack: k, prompt: ks.prompt, arena: ks.arena, rest: ks.rest }
|
|
124
|
+
let toRewind = cont
|
|
125
|
+
while (!!toRewind) {
|
|
126
|
+
meta = { stack: toRewind.stack, prompt: toRewind.prompt, arena: toRewind.backup(), rest: meta }
|
|
127
|
+
toRewind = toRewind.rest
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
const k1 = meta.stack // TODO instead copy meta here, like elsewhere?
|
|
131
131
|
meta.stack = null
|
|
132
|
-
return
|
|
132
|
+
return () => c(meta, k1)
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
function RUN_TOPLEVEL(comp) {
|
|
@@ -211,4 +211,4 @@ $effekt.run = RUN
|
|
|
211
211
|
*
|
|
212
212
|
* If a runtime is available, use `$effekt.run`, instead.
|
|
213
213
|
*/
|
|
214
|
-
$effekt.runToplevel = RUN_TOPLEVEL
|
|
214
|
+
$effekt.runToplevel = RUN_TOPLEVEL
|
|
@@ -81,7 +81,7 @@ char* c_bytearray_into_nullterminated_string(const struct Pos arr) {
|
|
|
81
81
|
// TODO do this in Effekt
|
|
82
82
|
struct Pos c_bytearray_show_Int(const Int n) {
|
|
83
83
|
char str[24];
|
|
84
|
-
|
|
84
|
+
snprintf(str, sizeof(str), "%" PRId64, n);
|
|
85
85
|
return c_bytearray_from_nullterminated_string(str);
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -112,14 +112,14 @@ struct Pos c_bytearray_show_Char(const uint64_t n) {
|
|
|
112
112
|
// TODO do this in Effekt
|
|
113
113
|
struct Pos c_bytearray_show_Byte(const Byte n) {
|
|
114
114
|
char str[4]; // Byte values range from 0 to 255, 3 characters + null terminator
|
|
115
|
-
|
|
115
|
+
snprintf(str, sizeof(str), "%" PRIu8, n);
|
|
116
116
|
return c_bytearray_from_nullterminated_string(str);
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
// TODO do this in Effekt
|
|
120
120
|
struct Pos c_bytearray_show_Double(const Double x) {
|
|
121
|
-
char str[64]; // TODO is this large enough?
|
|
122
|
-
|
|
121
|
+
char str[64]; // TODO is this large enough?
|
|
122
|
+
snprintf(str, sizeof(str), "%g", x);
|
|
123
123
|
return c_bytearray_from_nullterminated_string(str);
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!doctype html><html lang=en><head><meta charset=utf-8><meta http-equiv=x-ua-compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>Eclipse Public License 2.0 (EPL) | The Eclipse Foundation</title><meta property="og:title" content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=twitter:title content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta property="og:description" content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=twitter:description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=author content="Wayne Beaton"><link href=/favicon.ico rel=icon type=image/x-icon><meta property="og:image" content="https://www.eclipse.org/images/logos/eclipse-foundation-400x400.png"><meta name=twitter:image content="https://www.eclipse.org/images/logos/eclipse-foundation-400x400.png"><meta name=twitter:card content="summary"><meta name=twitter:site content="@eclipsefdn"><meta name=twitter:creator content="@eclipsefdn"><meta property="og:url" content="https://www.eclipse.org/legal/epl-2.0/"><meta property="og:type" content="website"><meta property="og:site_name" content="Eclipse Foundation"><meta name=keywords content="epl,epl-2.0,legal,foundation,eclipse,eclipse public license,license,licenses"><meta name=generator content="Hugo 0.110.0"><link rel=canonical href=https://www.eclipse.org/legal/epl-2.0/><link rel=alternate href=/legal/epl-2.0/index.xml type=application/rss+xml title><link rel=stylesheet href="/public/css/styles.css?v=
|
|
1
|
+
<!doctype html><html lang=en><head><meta charset=utf-8><meta http-equiv=x-ua-compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>Eclipse Public License 2.0 (EPL) | The Eclipse Foundation</title><meta property="og:title" content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=twitter:title content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta property="og:description" content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=twitter:description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=author content="Wayne Beaton"><link href=/favicon.ico rel=icon type=image/x-icon><meta property="og:image" content="https://www.eclipse.org/images/logos/eclipse-foundation-400x400.png"><meta name=twitter:image content="https://www.eclipse.org/images/logos/eclipse-foundation-400x400.png"><meta name=twitter:card content="summary"><meta name=twitter:site content="@eclipsefdn"><meta name=twitter:creator content="@eclipsefdn"><meta property="og:url" content="https://www.eclipse.org/legal/epl-2.0/"><meta property="og:type" content="website"><meta property="og:site_name" content="Eclipse Foundation"><meta name=keywords content="epl,epl-2.0,legal,foundation,eclipse,eclipse public license,license,licenses"><meta name=generator content="Hugo 0.110.0"><link rel=canonical href=https://www.eclipse.org/legal/epl-2.0/><link rel=alternate href=/legal/epl-2.0/index.xml type=application/rss+xml title><link rel=stylesheet href="/public/css/styles.css?v=1732475737"><script>(function(e,t,n,s,o){e[s]=e[s]||[],e[s].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var a=t.getElementsByTagName(n)[0],i=t.createElement(n),r=s!="dataLayer"?"&l="+s:"";i.async=!0,i.src="https://www.googletagmanager.com/gtm.js?id="+o+r,a.parentNode.insertBefore(i,a)})(window,document,"script","dataLayer","GTM-5WLCZXC")</script><script>var tableClasses="table"</script><link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel=stylesheet type=text/css></head><body><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5WLCZXC" height=0 width=0 style=display:none;visibility:hidden></iframe></noscript><a class=sr-only href=#content>Skip to main content</a><header class="header-wrapper header-wrapper" id=header><div class=header-toolbar><div class=container><div class=header-toolbar-row><div class="toolbar-btn toolbar-search-btn dropdown"><button class=dropdown-toggle id=toolbar-search type=button data-toggle=dropdown tabindex=0 aria-label=Search>
|
|
2
2
|
<i class="fa fa-search fa-lg" aria-role=none></i></button><div class="toolbar-search-bar-wrapper dropdown-menu dropdown-menu-right" aria-labelledby=toolbar-search><form action=https://www.eclipse.org/home/search method=get><div class=search-bar><input class=search-bar-input name=q placeholder=Search>
|
|
3
3
|
<button>
|
|
4
4
|
<i class="fa fa-search" type=submit></i></button></div></form></div></div><div class="toolbar-btn toolbar-user-menu-btn dropdown"><button class=dropdown-toggle id=toolbar-user-menu type=button data-toggle=dropdown tabindex=0 aria-label="User Menu">
|
|
@@ -57,5 +57,5 @@ open source software collaboration and innovation.</p></div><div class=mega-menu
|
|
|
57
57
|
<i class="fa fa-linkedin fa-stack-1x fa-inverse" aria-hidden=true></i>
|
|
58
58
|
<span class=sr-only>Linkedin account</span></span></a></li></ul></div></div><div class="footer-sections row equal-height-md font-bold"><div class=col-md-15><div class=row><section id=footer-eclipse-foundation class="footer-section col-sm-8"><div class=menu-heading>Eclipse Foundation</div><ul class=nav><li><a href=https://www.eclipse.org/org/>About</a></li><li><a href=https://projects.eclipse.org/>Projects</a></li><li><a href=https://www.eclipse.org/collaborations/>Collaborations</a></li><li><a href=https://www.eclipse.org/membership/>Membership</a></li><li><a href=https://www.eclipse.org/sponsor/>Sponsor</a></li></ul></section><section id=footer-legal class="footer-section col-sm-8"><div class=menu-heading>Legal</div><ul class=nav><li><a href=https://www.eclipse.org/legal/privacy/>Privacy Policy</a></li><li><a href=https://www.eclipse.org/legal/terms-of-use/>Terms of Use</a></li><li><a href=https://www.eclipse.org/legal/compliance/>Compliance</a></li><li><a href=https://www.eclipse.org/org/documents/Community_Code_of_Conduct.php>Code of Conduct</a></li><li><a href=https://www.eclipse.org/legal/>Legal Resources</a></li><li><a class=toolbar-manage-cookies href=# onclick=event.preventDefault()>Manage Cookies</a></li></ul></section><section id=footer-more class="footer-section col-sm-8"><div class=menu-heading>More</div><ul class=nav><li><a href=https://www.eclipse.org/security/>Report a Vulnerability</a></li><li><a href=https://status.eclipse.org/>Service Status</a></li><li><a href=https://www.eclipse.org/org/foundation/contact.php>Contact Us</a></li><li><a href=https://www.eclipse.org/projects/support/>Support</a></li></ul></section></div></div><div id=footer-end class="footer-section col-md-8 col-md-offset-1 col-sm-24"><div class=footer-end-newsletter><form id=mc-embedded-subscribe-form action=https://eclipsecon.us6.list-manage.com/subscribe/post method=post novalidate target=_blank><label class=footer-end-newsletter-label for=email>Subscribe to our Newsletter</label><div class=footer-end-newsletter-input-wrapper><input class=footer-end-newsletter-input type=email id=email name=EMAIL autocomplete=email placeholder="Enter your email address"><div><i class="fa fa-solid fa-envelope fa-lg" aria-hidden=true></i></div></div><input type=hidden name=u value=eaf9e1f06f194eadc66788a85>
|
|
59
59
|
<input type=hidden name=id value=46e57eacf1>
|
|
60
|
-
<input id=mc-embedded-subscribe type=submit name=subscribe hidden></form></div></div></div><div class="col-sm-24 margin-top-20"><div class=row><div id=copyright class=col-md-16><p id=copyright-text>Copyright © Eclipse Foundation AISBL. All Rights Reserved.</p></div></div></div><a href=# class=scrollup>Back to the top</a></div></footer><div id=footer-js><script src="/public/js/main.js?v1.0?v=
|
|
60
|
+
<input id=mc-embedded-subscribe type=submit name=subscribe hidden></form></div></div></div><div class="col-sm-24 margin-top-20"><div class=row><div id=copyright class=col-md-16><p id=copyright-text>Copyright © Eclipse Foundation AISBL. All Rights Reserved.</p></div></div></div><a href=# class=scrollup>Back to the top</a></div></footer><div id=footer-js><script src="/public/js/main.js?v1.0?v=1732475737"></script>
|
|
61
61
|
<script>(function(){})(jQuery,document)</script></div></body></html>
|