@effekt-lang/effekt 0.2.2 → 0.3.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.
Files changed (96) hide show
  1. package/bin/effekt +0 -0
  2. package/bin/effekt.sh +1 -1
  3. package/libraries/chez/common/effekt_primitives.ss +10 -2
  4. package/libraries/chez/common/immutable/cslist.effekt +13 -7
  5. package/libraries/chez/common/mutable/dict.effekt +8 -10
  6. package/libraries/chez/common/text/regex.effekt +7 -9
  7. package/libraries/common/args.effekt +95 -0
  8. package/libraries/common/array.effekt +225 -0
  9. package/libraries/common/bench.effekt +60 -0
  10. package/libraries/common/buffer.effekt +123 -0
  11. package/libraries/common/bytes.effekt +125 -0
  12. package/libraries/{chez/common/immutable → common}/dequeue.effekt +18 -4
  13. package/libraries/common/effekt.effekt +764 -0
  14. package/libraries/common/exception.effekt +122 -0
  15. package/libraries/common/io/console.effekt +66 -0
  16. package/libraries/common/io/error.effekt +365 -0
  17. package/libraries/common/io/files.effekt +282 -0
  18. package/libraries/common/io/network.effekt +105 -0
  19. package/libraries/common/io/time.effekt +15 -0
  20. package/libraries/common/io.effekt +158 -0
  21. package/libraries/{llvm/immutable → common}/list.effekt +83 -74
  22. package/libraries/common/option.effekt +72 -0
  23. package/libraries/common/process.effekt +10 -0
  24. package/libraries/common/queue.effekt +140 -0
  25. package/libraries/common/ref.effekt +63 -0
  26. package/libraries/common/result.effekt +45 -0
  27. package/libraries/{ml/immutable → common}/seq.effekt +30 -19
  28. package/libraries/common/string.effekt +395 -0
  29. package/libraries/common/test.effekt +98 -0
  30. package/libraries/js/effekt_builtins.js +37 -6
  31. package/libraries/js/io.js +143 -0
  32. package/libraries/js/mutable/map.effekt +8 -9
  33. package/libraries/js/text/regex.effekt +7 -8
  34. package/libraries/js/unsafe/cont.effekt +2 -2
  35. package/libraries/js/web/dom.effekt +13 -13
  36. package/libraries/llvm/array.c +53 -0
  37. package/libraries/llvm/buffer.c +181 -76
  38. package/libraries/llvm/forward-declare-c.ll +27 -8
  39. package/libraries/llvm/io.c +543 -1
  40. package/libraries/llvm/io.ll +19 -0
  41. package/libraries/llvm/main.c +3 -1
  42. package/libraries/llvm/ref.c +45 -0
  43. package/libraries/llvm/rts.ll +496 -354
  44. package/libraries/llvm/types.c +23 -0
  45. package/libraries/ml/effekt.sml +2 -0
  46. package/libraries/ml/internal/mllist.effekt +9 -0
  47. package/libraries/ml/internal/mloption.effekt +18 -0
  48. package/libraries/ml/random.effekt +9 -9
  49. package/libraries/ml/text/regex.effekt +14 -19
  50. package/licenses/eclipse public license, version 2.0 - epl-2.0.html +2 -2
  51. package/licenses/mit license - mit-license.html +527 -57
  52. package/licenses/the bsd license - bsd-license.html +530 -55
  53. package/licenses/the mit license - mit.html +528 -58
  54. package/package.json +2 -2
  55. package/libraries/chez/callcc/effekt.effekt +0 -221
  56. package/libraries/chez/common/effekt_matching.ss +0 -100
  57. package/libraries/chez/common/immutable/list.effekt +0 -643
  58. package/libraries/chez/common/immutable/option.effekt +0 -39
  59. package/libraries/chez/common/immutable/result.effekt +0 -29
  60. package/libraries/chez/common/io/args.effekt +0 -20
  61. package/libraries/chez/common/mutable/array.effekt +0 -27
  62. package/libraries/chez/common/mutable/heap.effekt +0 -12
  63. package/libraries/chez/common/test.effekt +0 -59
  64. package/libraries/chez/common/text/string.effekt +0 -46
  65. package/libraries/chez/lift/effekt.effekt +0 -217
  66. package/libraries/chez/monadic/effekt.effekt +0 -218
  67. package/libraries/js/effekt.effekt +0 -284
  68. package/libraries/js/effekt_matching.js +0 -79
  69. package/libraries/js/immutable/dequeue.effekt +0 -78
  70. package/libraries/js/immutable/list.effekt +0 -643
  71. package/libraries/js/immutable/option.effekt +0 -49
  72. package/libraries/js/immutable/result.effekt +0 -29
  73. package/libraries/js/io/args.effekt +0 -9
  74. package/libraries/js/io/async.effekt +0 -28
  75. package/libraries/js/io/async.js +0 -2
  76. package/libraries/js/io/file.effekt +0 -50
  77. package/libraries/js/io/file_include.js +0 -13
  78. package/libraries/js/mutable/array.effekt +0 -98
  79. package/libraries/js/mutable/heap.effekt +0 -32
  80. package/libraries/js/test.effekt +0 -59
  81. package/libraries/js/text/string.effekt +0 -64
  82. package/libraries/llvm/effekt.effekt +0 -243
  83. package/libraries/llvm/immutable/option.effekt +0 -53
  84. package/libraries/llvm/immutable/result.effekt +0 -29
  85. package/libraries/llvm/io/args.effekt +0 -24
  86. package/libraries/llvm/text/string.effekt +0 -179
  87. package/libraries/ml/effekt.effekt +0 -267
  88. package/libraries/ml/immutable/dequeue.effekt +0 -81
  89. package/libraries/ml/immutable/list.effekt +0 -674
  90. package/libraries/ml/immutable/option.effekt +0 -53
  91. package/libraries/ml/immutable/result.effekt +0 -29
  92. package/libraries/ml/internal/option.effekt +0 -18
  93. package/libraries/ml/io/args.effekt +0 -41
  94. package/libraries/ml/mutable/array.effekt +0 -43
  95. package/libraries/ml/test.effekt +0 -60
  96. package/libraries/ml/text/string.effekt +0 -81
package/bin/effekt CHANGED
Binary file
package/bin/effekt.sh CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/bin/bash
2
- java -jar "$(npm root -g)/effekt/bin/effekt" $@
2
+ java -jar "$(npm root -g)/@effekt-lang/effekt/bin/effekt" $@
@@ -3,6 +3,7 @@
3
3
  [(number? obj) (show-number obj)]
4
4
  [(string? obj) obj]
5
5
  [(boolean? obj) (if obj "true" "false")]
6
+ [(char? obj) (show-char obj)]
6
7
  ; [(record? obj)
7
8
  ; (let* ([rtd (record-rtd obj)]
8
9
  ; [name (symbol->string (record-type-name rtd))])
@@ -21,6 +22,9 @@
21
22
  (define (show-number n)
22
23
  (if (integer? n) (number->string (exact n)) (number->string n)))
23
24
 
25
+ (define (show-char c)
26
+ (string c))
27
+
24
28
  ; here we use eval to find the show function defined with the record...
25
29
  ; (define (show-record rec)
26
30
  ; (let* ([rtd (record-rtd rec)]
@@ -56,8 +60,8 @@
56
60
 
57
61
 
58
62
 
59
- (define (println_impl obj)
60
- (display (show_impl obj))
63
+ (define (println_impl str)
64
+ (display str)
61
65
  (newline))
62
66
 
63
67
  (define (equal_impl obj1 obj2)
@@ -79,6 +83,10 @@
79
83
  (define (seconds diff)
80
84
  (+ (time-second diff) (/ (time-nanosecond diff) 1000000000.0)))
81
85
 
86
+ (define (timestamp)
87
+ (let ([t (current-time)])
88
+ (+ (* (time-second t) 1000000000) (time-nanosecond t))))
89
+
82
90
  (define (measure block warmup iterations)
83
91
  (define (run n)
84
92
  (if (<= n 0)
@@ -1,25 +1,26 @@
1
1
  module immutable/cslist
2
2
 
3
- import immutable/list
3
+ import list
4
+ import array
4
5
 
5
6
  // a chez scheme cons list
6
7
  extern type CSList[A]
7
8
  extern pure def cons[A](el: A, rest: CSList[A]): CSList[A] =
8
- "(cons ${el} ${rest})"
9
+ chez "(cons ${el} ${rest})"
9
10
 
10
11
  extern pure def nil[A](): CSList[A] =
11
- "(list)"
12
+ chez "(list)"
12
13
 
13
- extern pure def isEmpty[A](l: CSList[A]): Boolean =
14
- "(null? ${l})"
14
+ extern pure def isEmpty[A](l: CSList[A]): Bool =
15
+ chez "(null? ${l})"
15
16
 
16
17
  // unsafe!
17
18
  extern pure def head[A](l: CSList[A]): A =
18
- "(car ${l})"
19
+ chez "(car ${l})"
19
20
 
20
21
  // unsafe!
21
22
  extern pure def tail[A](l: CSList[A]): CSList[A] =
22
- "(cdr ${l})"
23
+ chez "(cdr ${l})"
23
24
 
24
25
 
25
26
  def toChez[A](l: List[A]): CSList[A] = l match {
@@ -29,3 +30,8 @@ def toChez[A](l: List[A]): CSList[A] = l match {
29
30
 
30
31
  def fromChez[A](l: CSList[A]): List[A] =
31
32
  if (l.isEmpty) Nil() else Cons(l.head, l.tail.fromChez)
33
+
34
+ // Old From Chez
35
+ // -------------
36
+ extern pure def toArray[A](l: CSList[A]): Array[A] =
37
+ chez "(list->vector ${l})"
@@ -1,30 +1,28 @@
1
1
  module mutable/dict
2
2
 
3
- import immutable/option
4
-
5
3
  // https://cisco.github.io/ChezScheme/csug9.5/compat.html#g148
6
4
  extern type Dict[K, V]
7
5
 
8
6
  extern io def emptyDict[K, V](): Dict[K, V] =
9
- "(make-eq-hashtable)"
7
+ chez "(make-eq-hashtable)"
10
8
 
11
9
  extern io def emptyWeakDict[K, V](): Dict[K, V] =
12
- "(make-hash-table #t)"
10
+ chez "(make-hash-table #t)"
13
11
 
14
12
  extern io def put[K, V](d: Dict[K, V], key: K, value: V): Unit =
15
- "(put-hash-table! ${d} ${key} ${value})"
13
+ chez "(put-hash-table! ${d} ${key} ${value})"
16
14
 
17
15
  extern io def get[K, V](d: Dict[K, V], key: K, default: V): V =
18
- "(hashtable-ref ${d} ${key} ${default})"
16
+ chez "(hashtable-ref ${d} ${key} ${default})"
19
17
 
20
18
  extern io def unsafeGet[K, V](d: Dict[K, V], key: K): V =
21
- "(hashtable-ref ${d} ${key} #f)"
19
+ chez "(hashtable-ref ${d} ${key} #f)"
22
20
 
23
- extern io def contains[K, V](d: Dict[K, V], key: K): Boolean =
24
- "(hashtable-contains? ${d} ${key})"
21
+ extern io def contains[K, V](d: Dict[K, V], key: K): Bool =
22
+ chez "(hashtable-contains? ${d} ${key})"
25
23
 
26
24
  extern io def remove[K, V](d: Dict[K, V], key: K): Unit =
27
- "(remove-hash-table! ${d} ${key})"
25
+ chez "(remove-hash-table! ${d} ${key})"
28
26
 
29
27
  def get[K, V](d: Dict[K, V], key: K): Option[V] =
30
28
  undefinedToOption(unsafeGet(d, key))
@@ -1,17 +1,15 @@
1
1
  module text/regex
2
2
 
3
- import immutable/option
4
3
  import immutable/cslist
5
- import mutable/array
6
4
 
7
- extern include "pregexp.scm"
5
+ extern include chez "pregexp.scm"
8
6
 
9
7
  extern type Regex
10
8
 
11
9
  record Match(matched: String, index: Int)
12
10
 
13
11
  extern pure def regex(str: String): Regex =
14
- "(pregexp ${str})"
12
+ chez "(pregexp ${str})"
15
13
 
16
14
  def exec(reg: Regex, str: String): Option[Match] = {
17
15
  val matched = reg.unsafeMatchString(str)
@@ -20,14 +18,14 @@ def exec(reg: Regex, str: String): Option[Match] = {
20
18
  }
21
19
 
22
20
  extern pure def unsafeMatchIndex(reg: Regex, str: String): Int =
23
- "(let ([m (pregexp-match-positions ${reg} ${str})]) (if m (car (car m)) #f))"
21
+ chez "(let ([m (pregexp-match-positions ${reg} ${str})]) (if m (car (car m)) #f))"
24
22
 
25
23
  // we ignore the captures for now and only return the whole match
26
24
  extern pure def unsafeMatchString(reg: Regex, str: String): String =
27
- "(let ([m (pregexp-match ${reg} ${str})]) (if m (car m) #f))"
25
+ chez "(let ([m (pregexp-match ${reg} ${str})]) (if m (car m) #f))"
28
26
 
29
27
  extern pure def split(reg: Regex, str: String): CSList[String] =
30
- "(pregexp-split ${reg} ${str})"
28
+ chez "(pregexp-split ${reg} ${str})"
31
29
 
32
- def split(str: String, sep: String): Array[String] =
33
- sep.regex.split(str).toArray
30
+ // def split(str: String, sep: String): Array[String] =
31
+ // toArray(split(sep.regex, str))
@@ -0,0 +1,95 @@
1
+ module args
2
+
3
+ extern io def commandLineArgs(): List[String] =
4
+ js { js::commandLineArgs() }
5
+ chez { chez::commandLineArgs() }
6
+ ml { ml::commandLineArgs() }
7
+ llvm { llvm::commandLineArgs() }
8
+
9
+ namespace js {
10
+ extern type Args // = Array[String]
11
+
12
+ extern io def nativeArgs(): Args = js "process.argv.slice(1)"
13
+
14
+ extern io def argCount(args: Args): Int = js "${args}.length"
15
+
16
+ extern io def argument(args: Args, i: Int): String = js "${args}[${i}]"
17
+
18
+ def commandLineArgs(): List[String] = {
19
+ def toList(args: Args, start: Int, acc: List[String]): List[String] =
20
+ if (start < 1) { acc }
21
+ else args.toList(start - 1, Cons(args.argument(start), acc))
22
+
23
+ val args = nativeArgs()
24
+ args.toList(args.argCount - 1, Nil())
25
+ }
26
+ }
27
+
28
+ namespace chez {
29
+ extern type Args // = CSList[String]
30
+
31
+ extern io def nativeArgs(): Args =
32
+ chez "(cdr (command-line))"
33
+
34
+ extern pure def isEmpty(l: Args): Bool =
35
+ chez "(null? ${l})"
36
+
37
+ extern pure def head(l: Args): String =
38
+ chez "(car ${l})"
39
+
40
+ extern pure def tail(l: Args): Args =
41
+ chez "(cdr ${l})"
42
+
43
+ def toList(l: Args): List[String] =
44
+ if (l.isEmpty) Nil() else Cons(l.head, l.tail.toList)
45
+
46
+ def commandLineArgs(): List[String] = nativeArgs().toList
47
+ }
48
+
49
+ namespace ml {
50
+
51
+ // MLList[String]
52
+ // we inline these from mllist to not have a dependency
53
+ extern type Args
54
+
55
+ extern io def nativeArgs(): Args =
56
+ ml "CommandLine.arguments ()"
57
+
58
+ extern pure def isEmpty(args: Args): Bool =
59
+ ml "null ${args}"
60
+
61
+ extern io def first(a: Args): String =
62
+ ml "hd ${a}"
63
+
64
+ extern io def rest(a: Args): Args =
65
+ ml "tl ${a}"
66
+
67
+
68
+ def commandLineArgs(): List[String] = {
69
+ def toList(args: Args): List[String] =
70
+ if (args.isEmpty) Nil() else Cons(args.first, args.rest.toList)
71
+ nativeArgs().toList
72
+ }
73
+ }
74
+
75
+ namespace llvm {
76
+ extern io def argCount(): Int =
77
+ llvm """
78
+ %c = call %Int @c_get_argc()
79
+ ret %Int %c
80
+ """
81
+
82
+ extern io def argument(i: Int): String =
83
+ llvm """
84
+ %s = call %Pos @c_get_arg(%Int ${i})
85
+ ret %Pos %s
86
+ """
87
+
88
+ def commandLineArgs(): List[String] = {
89
+ def toList(start: Int, acc: List[String]): List[String] =
90
+ if (start < 1) { acc }
91
+ else toList(start - 1, Cons(argument(start), acc))
92
+
93
+ toList(argCount() - 1, Nil())
94
+ }
95
+ }
@@ -0,0 +1,225 @@
1
+ module array
2
+
3
+ import effekt
4
+ import exception
5
+ import list
6
+
7
+ /**
8
+ * A mutable 0-indexed fixed-sized array.
9
+ */
10
+ extern type Array[T]
11
+
12
+ /**
13
+ * Allocates a new array of size `size`, keeping its values _undefined_.
14
+ * Prefer using `array` constructor instead to ensure that values are defined.
15
+ */
16
+ extern global def allocate[T](size: Int): Array[T] =
17
+ js "(new Array(${size}))"
18
+ chez "(make-vector ${size})" // creates an array filled with 0s on CS
19
+ llvm """
20
+ %z = call %Pos @c_array_new(%Int ${size})
21
+ ret %Pos %z
22
+ """
23
+
24
+ /**
25
+ * Creates a new Array of size `size` filled with the value `init`
26
+ */
27
+ extern global def array[T](size: Int, init: T): Array[T] =
28
+ ml "Array.array (${size}, ${init})"
29
+ default {
30
+ val arr = allocate[T](size);
31
+ each(0, size) { i =>
32
+ unsafeSet(arr, i, init)
33
+ };
34
+ arr
35
+ }
36
+
37
+ /**
38
+ * Converts a List `list` to an Array
39
+ */
40
+ def fromList[T](list: List[T]): Array[T] = {
41
+ val listSize = list.size();
42
+ val arr = allocate(listSize);
43
+
44
+ foreachIndex(list) { (i, head) =>
45
+ arr.unsafeSet(i, head)
46
+ }
47
+ return arr;
48
+ }
49
+
50
+ /**
51
+ * Gets the length of the array in constant time.
52
+ */
53
+ extern pure def size[T](arr: Array[T]): Int =
54
+ js "${arr}.length"
55
+ chez "(vector-length ${arr})"
56
+ ml "Array.length ${arr}"
57
+ llvm """
58
+ %z = call %Int @c_array_size(%Pos ${arr})
59
+ ret %Int %z
60
+ """
61
+
62
+ /**
63
+ * Gets the element of the `arr` at given `index` in constant time.
64
+ * Unchecked Precondition: `index` is in bounds (0 ≤ index < arr.size)
65
+ *
66
+ * Prefer using `get` instead.
67
+ */
68
+ extern global def unsafeGet[T](arr: Array[T], index: Int): T =
69
+ js "${arr}[${index}]"
70
+ chez "(vector-ref ${arr} ${index})"
71
+ ml "Array.sub (${arr}, ${index})"
72
+ llvm """
73
+ %z = call %Pos @c_array_get(%Pos ${arr}, %Int ${index})
74
+ ret %Pos %z
75
+ """
76
+
77
+ extern js """
78
+ function array$set(arr, index, value) {
79
+ arr[index] = value;
80
+ return $effekt.unit
81
+ }
82
+ """
83
+
84
+ /**
85
+ * Sets the element of the `arr` at given `index` to `value` in constant time.
86
+ * Unchecked Precondition: `index` is in bounds (0 ≤ index < arr.size)
87
+ *
88
+ * Prefer using `set` instead.
89
+ */
90
+ extern global def unsafeSet[T](arr: Array[T], index: Int, value: T): Unit =
91
+ js "array$set(${arr}, ${index}, ${value})"
92
+ chez "(begin (vector-set! ${arr} ${index} ${value}) #f)"
93
+ ml "Array.update (${arr}, ${index}, ${value})"
94
+ llvm """
95
+ %z = call %Pos @c_array_set(%Pos ${arr}, %Int ${index}, %Pos ${value})
96
+ ret %Pos %z
97
+ """
98
+
99
+ /**
100
+ * Creates a copy of `arr`
101
+ */
102
+ def copy[T](arr: Array[T]): Array[T] = {
103
+ with on[OutOfBounds].default { <> }; // should not happen
104
+ val len = arr.size;
105
+ val newArray = allocate[T](len);
106
+ copy[T](arr, 0, newArray, 0, len);
107
+ newArray
108
+ }
109
+
110
+ /**
111
+ * Copies `length`-many elements from `from` to `to`
112
+ * starting at `start` (in `from`) and `offset` (in `to`)
113
+ */
114
+ def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
115
+ val startValid = start >= 0 && start + length <= from.size
116
+ val offsetValid = offset >= 0 && offset + length <= to.size
117
+
118
+ def go(i: Int, j: Int, length: Int): Unit =
119
+ if (length > 0) {
120
+ to.unsafeSet(j, from.unsafeGet(i))
121
+ go(i + 1, j + 1, length - 1)
122
+ }
123
+
124
+ if (startValid && offsetValid) go(start, offset, length)
125
+ else do raise(OutOfBounds(), "Array index out of bounds, when copying")
126
+ }
127
+
128
+ // Derived operations:
129
+
130
+ /**
131
+ * Gets the element of the `arr` at given `index` in constant time,
132
+ * throwing an `Exception[OutOfBounds]` unless `0 ≤ index < arr.size`.
133
+ */
134
+ def get[T](arr: Array[T], index: Int): T / Exception[OutOfBounds] =
135
+ if (index >= 0 && index < arr.size) arr.unsafeGet(index)
136
+ else do raise(OutOfBounds(), "Array index out of bounds: " ++ show(index))
137
+
138
+ /**
139
+ * Sets the element of the `arr` at given `index` to `value` in constant time,
140
+ * throwing an `Exception[OutOfBounds]` unless `0 ≤ index < arr.size`.
141
+ */
142
+ def set[T](arr: Array[T], index: Int, value: T): Unit / Exception[OutOfBounds] =
143
+ if (index >= 0 && index < arr.size) unsafeSet(arr, index, value)
144
+ else do raise(OutOfBounds(), "Array index out of bounds: " ++ show(index))
145
+
146
+ /**
147
+ * Builds a new Array of size `size` from a computation `index` which gets an index
148
+ * and returns a value that will be on that position in the resulting array
149
+ */
150
+ def build[T](size: Int) { index: Int => T }: Array[T] = {
151
+ val arr = allocate[T](size);
152
+ each(0, size) { i =>
153
+ unsafeSet(arr, i, index(i))
154
+ };
155
+ arr
156
+ }
157
+
158
+ // Utility functions:
159
+
160
+ def toList[T](arr: Array[T]): List[T] = {
161
+ var i = arr.size - 1;
162
+ var l = Nil[T]()
163
+ while (i >= 0) {
164
+ l = Cons(arr.unsafeGet(i), l)
165
+ i = i - 1
166
+ }
167
+ l
168
+ }
169
+
170
+ def foreach[T](arr: Array[T]){ action: T => Unit }: Unit =
171
+ each(0, arr.size) { i =>
172
+ val x: T = arr.unsafeGet(i)
173
+ action(x)
174
+ }
175
+
176
+ def foreach[T](arr: Array[T]){ action: (T) {Control} => Unit }: Unit =
177
+ each(0, arr.size) { (i) {label} =>
178
+ val x: T = arr.unsafeGet(i)
179
+ action(x) {label}
180
+ }
181
+
182
+ def foreachIndex[T](arr: Array[T]){ action: (Int, T) => Unit }: Unit =
183
+ each(0, arr.size) { i =>
184
+ val x: T = arr.unsafeGet(i)
185
+ action(i, x)
186
+ }
187
+
188
+ def foreachIndex[T](arr: Array[T]){ action: (Int, T) {Control} => Unit }: Unit =
189
+ each(0, arr.size) { (i) {label} =>
190
+ val x: T = arr.unsafeGet(i)
191
+ action(i, x) {label}
192
+ }
193
+
194
+ def sum(list: Array[Int]): Int = {
195
+ var acc = 0
196
+ list.foreach { x =>
197
+ acc = acc + x
198
+ }
199
+ acc
200
+ }
201
+
202
+ // Show Instances
203
+ // --------------
204
+
205
+ def show[A](arr: Array[A]) { showA: A => String }: String = {
206
+ var output = "Array("
207
+ val lastIndex = arr.size - 1
208
+
209
+ arr.foreachIndex { (index, a) =>
210
+ if (index == lastIndex) output = output ++ showA(a)
211
+ else output = output ++ showA(a) ++ ", "
212
+ }
213
+ output = output ++ ")"
214
+
215
+ output
216
+ }
217
+ def show(l: Array[Int]): String = show(l) { e => show(e) }
218
+ def show(l: Array[Double]): String = show(l) { e => show(e) }
219
+ def show(l: Array[Bool]): String = show(l) { e => show(e) }
220
+ def show(l: Array[String]): String = show(l) { e => e }
221
+
222
+ def println(l: Array[Int]): Unit = println(show(l))
223
+ def println(l: Array[Double]): Unit = println(show(l))
224
+ def println(l: Array[Bool]): Unit = println(show(l))
225
+ def println(l: Array[String]): Unit = println(show(l))
@@ -0,0 +1,60 @@
1
+ module bench
2
+
3
+ /**
4
+ * The current time (since UNIX Epoch) in nanoseconds.
5
+ *
6
+ * The actual precision varies across the different backends.
7
+ * - js: Milliseconds
8
+ * - chez: Microseconds
9
+ * - ml: Microseconds
10
+ */
11
+ extern io def timestamp(): Int =
12
+ js "Date.now() * 1000000"
13
+ chez "(timestamp)"
14
+ ml "IntInf.toInt (Time.toNanoseconds (Time.now ()))"
15
+ llvm """
16
+ %time_ptr = alloca { i64, i64 }
17
+ call i32 @clock_gettime(i32 0, ptr %time_ptr)
18
+ %time = load {i64, i64}, ptr %time_ptr
19
+ %time_seconds = extractvalue {i64, i64} %time, 0
20
+ %time_nanoseconds = extractvalue {i64, i64} %time, 1
21
+ %time_seconds_nanoseconds = mul nsw i64 %time_seconds, 1000000000
22
+ %result = add nsw i64 %time_seconds_nanoseconds, %time_nanoseconds
23
+ ret %Int %result
24
+ """
25
+
26
+ extern llvm """
27
+ declare i32 @clock_gettime(i32, ptr)
28
+ """
29
+
30
+ /**
31
+ * High-precision timestamp in nanoseconds that should be for measurements.
32
+ *
33
+ * This timestamp should only be used for **relative** measurements,
34
+ * as gives no guarantees on the absolute time (unlike a UNIX timestamp).
35
+ */
36
+ extern io def relativeTimestamp(): Int =
37
+ js "Math.round(performance.now() * 1000000)"
38
+ default { timestamp() }
39
+
40
+ /**
41
+ * Runs the block and returns the time in nanoseconds
42
+ */
43
+ def timed { block: => Unit }: Int = {
44
+ val before = relativeTimestamp()
45
+ block()
46
+ val after = relativeTimestamp()
47
+ after - before
48
+ }
49
+
50
+ def measure(warmup: Int, iterations: Int) { block: => Unit }: Unit = {
51
+ def run(n: Int, report: Bool): Unit = {
52
+ if (n <= 0) { () } else {
53
+ val time = timed { block() };
54
+ if (report) { println(time) } else { () };
55
+ run(n - 1, report)
56
+ }
57
+ }
58
+ run(warmup, false)
59
+ run(iterations, true)
60
+ }
@@ -0,0 +1,123 @@
1
+ module buffer
2
+
3
+ import array
4
+
5
+ // TODO (in Effekt compiler)
6
+ // - [ ] fix allocating into actually global region
7
+ // - [ ] fix exceptions on objects
8
+ // - [X] allow omitting braces after `at` for singleton regions
9
+
10
+ record BufferOverflow()
11
+
12
+ /**
13
+ * Fixed-size ring buffers (queues) where one can write
14
+ * elements to the head and read from the tail.
15
+ */
16
+ interface Buffer[T] {
17
+ /**
18
+ * The remaining capacity
19
+ */
20
+ def capacity(): Int
21
+
22
+ /**
23
+ * Is this buffer full?
24
+ */
25
+ def full?(): Bool
26
+
27
+ /**
28
+ * Is this buffer empty?
29
+ */
30
+ def empty?(): Bool
31
+
32
+ /**
33
+ * Read an element from the tail of the buffer
34
+ */
35
+ def read(): Option[T]
36
+
37
+ /**
38
+ * Write an element to the head of the buffer
39
+ */
40
+ def write(el: T): Unit / Exception[BufferOverflow]
41
+ }
42
+
43
+ def emptyBuffer[T](capacity: Int): Buffer[T] at {global} = {
44
+ if (capacity == 1) refBuffer()
45
+ else arrayBuffer(capacity)
46
+ }
47
+ def arrayBuffer[T](initialCapacity: Int): Buffer[T] at {global} = {
48
+ // TODO allocate buffer (and array) into a region r.
49
+ val contents = emptyArray[T](initialCapacity)
50
+ var head in global = 0
51
+ var tail in global = 0
52
+
53
+ def size(): Int =
54
+ if (tail >= head) { tail - head }
55
+ else { initialCapacity - head + tail }
56
+
57
+ def capacity(): Int = initialCapacity - size()
58
+
59
+ def buffer = new Buffer[T] {
60
+ def capacity() = capacity()
61
+ def full?() = capacity() <= 0
62
+ def empty?() = size() <= 0
63
+ def read() = {
64
+ if (buffer.empty?) None()
65
+ else {
66
+ val result: T = contents.remove(head).getOrElse { <> };
67
+ head = mod(head + 1, initialCapacity)
68
+ Some(result)
69
+ }
70
+ }
71
+ def write(el: T) = {
72
+ if (buffer.full?) <> // raise(BufferOverflow())
73
+
74
+ contents.put(tail, el)
75
+ tail = mod(tail + 1, initialCapacity)
76
+ }
77
+ }
78
+ buffer
79
+ }
80
+ def refBuffer[T](): Buffer[T] at {global} = {
81
+ var content: Option[T] in global = None()
82
+ new Buffer[T] {
83
+ def capacity() = if (content.isEmpty) 1 else 0
84
+ def full?() = content.isDefined
85
+ def empty?() = isEmpty(content)
86
+ def read() = {
87
+ val res = content
88
+ content = None()
89
+ res
90
+ }
91
+ def write(el: T) = content match {
92
+ case Some(v) =>
93
+ <> // do raise(BufferOverflow(), "Cannot read element from buffer")
94
+ case None() =>
95
+ content = Some(el)
96
+ }
97
+ }
98
+ }
99
+
100
+ namespace examples {
101
+ def main() = ignore[BufferOverflow] {
102
+ // Buffer with capacity 1
103
+ def b = emptyBuffer[Int](1);
104
+ println(b.capacity);
105
+ println(b.full?);
106
+
107
+ b.write(17);
108
+ println(b.read());
109
+
110
+ // buffer with capacity 3
111
+ def ringbuffer = emptyBuffer[Int](3);
112
+ ringbuffer.write(1);
113
+ ringbuffer.write(2);
114
+ println(ringbuffer.read());
115
+ ringbuffer.write(3);
116
+ println(ringbuffer.read());
117
+ println(ringbuffer.read());
118
+ ringbuffer.write(4);
119
+ ringbuffer.write(5);
120
+ println(ringbuffer.read());
121
+ println(ringbuffer.read());
122
+ }
123
+ }