@effekt-lang/effekt 0.85.0 → 0.87.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.
@@ -22,19 +22,19 @@ effect next[A](): A / stop
22
22
  ///
23
23
  /// e.g. for[Int] { prog() } { el => println(el) }
24
24
  def for[A] { stream: () => Unit / emit[A] } { action: A => Unit }: Unit =
25
- returning::for[A, Unit]{stream}{action}
25
+ returning::for{stream}{action}
26
26
 
27
27
  /// Applies the given function on each emitted element and reemits the result.
28
28
  ///
29
29
  /// e.g. map[Int,String]{ x => x.show } { range(0, 5) }
30
30
  def map[A, B] { function: A => B } { stream: () => Unit / emit[A] }: Unit / emit[B] =
31
- returning::map[A, B, Unit]{function}{stream}
31
+ returning::map{function}{stream}
32
32
 
33
33
  /// Runs the given action whenever the given driver reads an element.
34
34
  ///
35
35
  /// e.g. var i = -1; with tap { i = i + 1; i }; driver()
36
36
  def tap[A] { action: () => A / stop } { driver: () => Unit / next[A] }: Unit =
37
- returning::tap[A, Unit]{action}{driver}
37
+ returning::tap{action}{driver}
38
38
 
39
39
  /// Runs the stream as a message queue,
40
40
  /// handling each of its elements that can produce new elements ("messages"),
@@ -75,12 +75,12 @@ def rangeFrom(lower: Int): Unit / emit[Int] = {
75
75
  }
76
76
 
77
77
  def index[A] { stream: () => Unit / emit[A] }: Unit / emit[Indexed[A]] =
78
- returning::index[A, Unit]{stream}
78
+ returning::index{stream}
79
79
 
80
80
 
81
81
  /// If `number` is zero or negative it does nothing
82
82
  def limit[A](number: Int) { stream: () => Unit / emit[A] }: Unit / emit[A] =
83
- boundary { returning::limit[A, Unit](number){stream} }
83
+ boundary { returning::limit(number){stream} }
84
84
 
85
85
 
86
86
  /// If `number` is zero or negative it does nothing
@@ -122,10 +122,10 @@ def takeWhile[A] { predicate: A => Bool } { stream: () => Unit / emit[A] }: Unit
122
122
 
123
123
 
124
124
  def sum { stream: () => Unit / emit[Int] }: Int =
125
- returning::sum[Unit]{stream}.second
125
+ returning::sum{stream}.second
126
126
 
127
127
  def product { stream: () => Unit / emit[Int] }: Int =
128
- returning::product[Unit]{stream}.second
128
+ returning::product{stream}.second
129
129
 
130
130
  /// Returns true if all elements in the stream satisfy the given predicate,
131
131
  /// false otherwise. Short-circuits as soon as a non-matching element is found.
@@ -290,7 +290,7 @@ def manyTee[A]{ body: { { { => Unit / emit[A] } => Unit }{ => Unit / emit[A] } =
290
290
  try {
291
291
  body{ {hnd}{prod} =>
292
292
  running = running + 1
293
- teeing[A]{ {b} => hnd{b}; running = running - 1 }{prod}
293
+ teeing{ {b} => hnd{b}; running = running - 1 }{prod}
294
294
  }
295
295
  } with emit[A] { _ =>
296
296
  if(running > 0) resume(())
@@ -307,7 +307,7 @@ def manyTee[A]{ body: { { { => Unit / emit[A] } => Unit }{ => Unit / emit[A] } =
307
307
  /// assertEquals(product{ range(0, 10) }, productRes)
308
308
  ///
309
309
  def tee[A]{ cons1: { => Unit / emit[A] } => Unit }{ cons2: { => Unit / emit[A] } => Unit }{ prod: => Unit / emit[A] }: Unit = {
310
- manyTee[A]{ {tee} =>
310
+ manyTee{ {tee} =>
311
311
  with tee{cons1}
312
312
  with tee{cons2}
313
313
  prod()
@@ -358,7 +358,7 @@ effect peek[A](): A / stop
358
358
 
359
359
  /// Provide the ability to peek when consuming a stream.
360
360
  def peeking[A] { peeker: () => Unit / {next[A], peek[A]} }: Unit / next[A] =
361
- returning::peeking[A, Unit]{peeker}
361
+ returning::peeking{peeker}
362
362
 
363
363
  // Counting and padding
364
364
  // --------------------
@@ -371,21 +371,21 @@ effect getPos(): Int
371
371
  def tracking[A](init: Int){ body: => Unit / { emit[A], getPos } }: Unit / emit[A] = {
372
372
  var n = init
373
373
  try body()
374
- with emit[A] { b => n = n + 1; resume(do emit[A](b)) }
374
+ with emit[A] { b => n = n + 1; resume(do emit(b)) }
375
375
  with getPos{ resume(n) }
376
376
  }
377
377
 
378
378
  /// Pad to a multiple of fac emitted values, by emitting gen repeatedly
379
379
  def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
380
380
  while(mod(do getPos(), fac) != 0) {
381
- do emit[A](gen())
381
+ do emit(gen())
382
382
  }
383
383
  }
384
384
 
385
385
  /// Track how many bytes were emitted (in the body)
386
386
  /// Handles getPos
387
387
  def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
388
- tracking[A](0){body}
388
+ tracking(0){body}
389
389
 
390
390
  namespace returning {
391
391
 
@@ -39,7 +39,7 @@ def substring(str: String, from: Int): String =
39
39
 
40
40
  /// Checks whether str starts with the given prefix at `from`
41
41
  def isSubstringAt(str: String, prefix: String, from: Int): Bool = {
42
- with default[OutOfBounds, Bool] { false };
42
+ with default { false };
43
43
 
44
44
  val stringLength = str.length
45
45
  val prefixLength = prefix.length
@@ -71,15 +71,37 @@ def repeat(str: String, n: Int): String = {
71
71
  go(n, "")
72
72
  }
73
73
 
74
+ /// Left-pad the given string with `fill` to length `n`
75
+ def padLeft(str: String, n: Int, fill: Char): String =
76
+ if (str.length >= n) str else fill.toString.repeat(n - str.length) ++ str
77
+
78
+ /// Right-pad the given string with `fill` to length `n`
79
+ def padRight(str: String, n: Int, fill: Char): String =
80
+ if (str.length >= n) str else str ++ fill.toString.repeat(n - str.length)
81
+
74
82
  /// Left-pad the given string with spaces
75
- def padLeft(str: String, n: Int): String = {
76
- " ".repeat(n - str.length) ++ str
77
- }
83
+ def padLeft(str: String, n: Int): String = str.padLeft(n, ' ')
78
84
 
79
85
  /// Right-pad the given string with spaces
80
- def padRight(str: String, n: Int): String = {
81
- str ++ " ".repeat(n - str.length)
82
- }
86
+ def padRight(str: String, n: Int): String = str.padRight(n, ' ')
87
+
88
+ /// Show the given `unscaled` number with 10^-`scale`.
89
+ ///
90
+ /// Examples:
91
+ /// ```
92
+ /// > -105.showScaled(2)
93
+ /// -1.05
94
+ /// > 7.showScaled(3)
95
+ /// 0.007
96
+ /// ```
97
+ def showScaled(unscaled: Int, scale: Int): String =
98
+ if (scale <= 0) (unscaled * pow(10, 0 - scale)).show
99
+ else {
100
+ val factor = pow(10, scale)
101
+ val magnitude = abs(unscaled)
102
+ val sign = if (unscaled < 0) "-" else ""
103
+ sign ++ (magnitude / factor).show ++ "." ++ magnitude.rem(factor).show.padLeft(scale, '0')
104
+ }
83
105
 
84
106
  // TODO use .split() in JS
85
107
  def split(str: String, sep: String): List[String] = {
@@ -214,7 +236,7 @@ namespace internal {
214
236
  """
215
237
 
216
238
  def utf8ByteLength(str: String): Int = stream::sum {
217
- for {str.each} { (c: Char) =>
239
+ for {str.each} { (c) =>
218
240
  do emit(c.utf8ByteCount)
219
241
  }
220
242
  }
@@ -281,7 +303,8 @@ extern def unsafeCharAt(str: String, n: Int) at {}: Char =
281
303
  js "${str}.codePointAt(${n})"
282
304
  chez "(char->integer (string-ref ${str} ${n}))"
283
305
  llvm """
284
- %x = call %Int @c_bytearray_character_at(%Pos ${str}, %Int ${n})
306
+ %c = call i32 @c_bytearray_character_at(%Pos ${str}, %Int ${n})
307
+ %x = zext i32 %c to %Int
285
308
  ret %Int %x
286
309
  """
287
310
 
@@ -300,7 +323,7 @@ def each(string: String): Unit / emit[Char] =
300
323
  }
301
324
 
302
325
  def collect(capacity: Int) { stream: () => Unit / emit[Char] }: String =
303
- returning::collect[Unit](capacity){stream}.second
326
+ returning::collect(capacity){stream}.second
304
327
 
305
328
  def collect { stream: () => Unit / emit[Char] }: String =
306
329
  collect(4){stream}
@@ -308,7 +331,7 @@ def collect { stream: () => Unit / emit[Char] }: String =
308
331
  namespace returning {
309
332
 
310
333
  def collect[R](capacity: Int) { stream: () => R / emit[Char] }: (R, String) = {
311
- val (result, bytes) = bytearray::returning::collect[R](capacity) { encodeUTF8 { stream } }
334
+ val (result, bytes) = bytearray::returning::collect(capacity) { encodeUTF8 { stream } }
312
335
  (result, bytes.toString)
313
336
  }
314
337
 
@@ -1,7 +1,7 @@
1
1
  ; forward-declared from primitives.c
2
2
 
3
3
  declare i64 @c_get_argc()
4
- declare %Pos @c_get_arg(i64)
4
+ declare %Pos @c_get_arg(i64 zeroext)
5
5
 
6
6
  declare void @c_io_print(%Pos)
7
7
  declare void @c_io_println(%Pos)
@@ -12,38 +12,29 @@ declare void @hole(i8*) cold noreturn
12
12
  declare void @duplicated_prompt() cold noreturn
13
13
  declare void @unmatched_tag() cold noreturn
14
14
 
15
- declare %Pos @c_ref_fresh(%Pos)
16
- declare %Pos @c_ref_get(%Pos)
17
- declare %Pos @c_ref_set(%Pos, %Pos)
18
-
19
- declare %Pos @c_array_new(%Int)
20
- declare %Int @c_array_size(%Pos)
21
- declare %Pos @c_array_get(%Pos, %Int)
22
- declare %Pos @c_array_set(%Pos, %Int, %Pos)
23
-
24
- declare %Pos @c_bytearray_new(%Int)
15
+ declare %Pos @c_bytearray_new(%Int signext)
25
16
  declare %Int @c_bytearray_size(%Pos)
26
- declare %Byte @c_bytearray_get(%Pos, %Int)
27
- declare %Pos @c_bytearray_set(%Pos, %Int, %Byte)
17
+ declare %Byte @c_bytearray_get(%Pos, %Int signext)
18
+ declare %Pos @c_bytearray_set(%Pos, %Int signext, %Byte zeroext)
28
19
 
29
- declare %Pos @c_bytearray_copy(%Pos, %Int, %Pos, %Int, %Int)
20
+ declare %Pos @c_bytearray_copy(%Pos, %Int signext, %Pos, %Int signext, %Int signext)
30
21
  declare ptr @c_bytearray_data(%Pos)
31
- declare %Pos @c_bytearray_construct(i64, ptr)
22
+ declare %Pos @c_bytearray_construct(i64 zeroext, ptr)
32
23
  declare void @c_bytearray_erase_noop(ptr)
33
24
  declare %Pos @c_bytearray_copy_shared(%Pos)
34
25
 
35
26
  declare %Pos @c_bytearray_from_nullterminated_string(ptr)
36
27
  declare ptr @c_bytearray_into_nullterminated_string(%Pos)
37
28
 
38
- declare %Pos @c_bytearray_show_Int(i64)
39
- declare %Pos @c_bytearray_show_Char(i32)
40
- declare %Pos @c_bytearray_show_Byte(i8)
29
+ declare %Pos @c_bytearray_show_Int(i64 signext)
30
+ declare %Pos @c_bytearray_show_Char(i64 zeroext)
31
+ declare %Pos @c_bytearray_show_Byte(i8 zeroext)
41
32
  declare %Pos @c_bytearray_show_Double(double)
42
33
 
43
34
  declare %Pos @c_bytearray_concatenate(%Pos, %Pos)
44
35
  declare %Pos @c_bytearray_equal(%Pos, %Pos)
45
36
  declare %Int @c_bytearray_compare(%Pos, %Pos)
46
37
 
47
- declare %Pos @c_bytearray_substring(%Pos, i64, i64)
48
- declare %Int @c_bytearray_character_at(%Pos, i64)
38
+ declare %Pos @c_bytearray_substring(%Pos, i64 zeroext, i64 zeroext)
39
+ declare i32 @c_bytearray_character_at(%Pos, i64 zeroext)
49
40
 
@@ -99,11 +99,11 @@
99
99
 
100
100
  ; Foreign imports
101
101
 
102
- declare ptr @malloc(i64)
102
+ declare ptr @malloc(i64 zeroext)
103
103
  declare void @free(ptr)
104
- declare ptr @realloc(ptr, i64)
105
- declare noalias ptr @calloc(i64, i64)
106
- declare ptr @memcpy(ptr, ptr, i64)
104
+ declare ptr @realloc(ptr, i64 zeroext)
105
+ declare noalias ptr @calloc(i64 zeroext, i64 zeroext)
106
+ declare ptr @memcpy(ptr, ptr, i64 zeroext)
107
107
  declare i64 @llvm.ctlz.i64 (i64 , i1)
108
108
  declare i64 @llvm.cttz.i64 (i64 , i1)
109
109
  declare i64 @llvm.fshr.i64(i64, i64, i64)
@@ -120,7 +120,7 @@ declare double @log1p(double)
120
120
  ; Intrinsic versions of the following two only added in LLVM 19
121
121
  declare double @atan(double)
122
122
  declare double @tan(double)
123
- declare void @exit(i32)
123
+ declare void @exit(i32 signext)
124
124
  declare void @llvm.assume(i1)
125
125
 
126
126
 
@@ -808,7 +808,7 @@ define private ptr @frameDescriptorEntry(%StackPointer %header, i64 %index) alwa
808
808
  %returnAddressPointer = getelementptr %FrameHeader, %StackPointer %header, i64 0, i32 0
809
809
  %returnAddress = load %ReturnAddress, ptr %returnAddressPointer, !alias.scope !12, !noalias !22
810
810
  %offsetPointer = getelementptr i64, ptr %returnAddress, i64 %index
811
- %offset = load i64, ptr %offsetPointer
811
+ %offset = load i64, ptr %offsetPointer, align 1
812
812
  %entry = getelementptr i8, ptr %returnAddress, i64 %offset
813
813
  ret ptr %entry
814
814
  }