@effekt-lang/effekt 0.62.0 → 0.64.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.
@@ -2,21 +2,15 @@ module scanner
2
2
 
3
3
  import stream
4
4
 
5
- interface Scan[A] {
6
- /// Return the next character, not advancing.
7
- /// That is, this does not change what any future calls on the Scanner return.
8
- def peek(): A / stop
9
- /// Advance the Scanner to the next character.
10
- def skip(): Unit / stop
11
- }
12
5
 
6
+ /// Return the next character, not advancing.
7
+ /// That is, this does not change what any future calls on the Scanner return.
8
+ effect peek[A](): A / stop
13
9
 
14
- /// Advance the Scanner to the next token, returning it.
15
- def read[A](): A / { Scan[A], stop } = {
16
- val t = do peek()
17
- do skip()
18
- return t
19
- }
10
+ effect Scan[A] = {read[A], peek[A]}
11
+
12
+ /// Advance the pull stream to the next element, discarding the current element.
13
+ def skip[A](): Unit / {read[A], stop} = { do read(); () }
20
14
 
21
15
 
22
16
  /// Run a scanner by reading from an input stream, discarding its output.
@@ -29,7 +23,7 @@ def expect(message: String) { scanner: () => Unit / fail }: Unit / Exception[Wro
29
23
  /// If conversion is successful read the next token, otherwise stop.
30
24
  def readWhen[A, B] { convert: A => B / fail }: B / { Scan[A], stop } = {
31
25
  val t = attempt { convert(do peek()) } { do stop() };
32
- do skip();
26
+ do read()
33
27
  return t
34
28
  }
35
29
 
@@ -37,7 +31,7 @@ def readWhen[A, B] { convert: A => B / fail }: B / { Scan[A], stop } = {
37
31
  def readIf[A] { predicate: A => Bool }: A / { Scan[A], stop } = {
38
32
  val t = do peek()
39
33
  if (predicate(t)) {
40
- do skip()
34
+ do read()
41
35
  return t
42
36
  } else {
43
37
  do stop()
@@ -51,7 +45,7 @@ def skipIf[A] { predicate: A => Bool }: Unit / { Scan[A], stop } = {
51
45
 
52
46
  /// Skips the next token but fails instead of stopping.
53
47
  def mustSkip[A](): Unit / { Scan[A], fail } =
54
- must { do skip() }
48
+ must { skip() }
55
49
 
56
50
  /// Skips the next character but fails if it is not the given one.
57
51
  def mustSkip(token: Char): Unit / { Scan[Char], fail } =
@@ -91,19 +85,21 @@ def skipIf(e: Char): Unit / { Scan[Char], stop } =
91
85
  /// Immediately stops when already at the end.
92
86
  def until[A, R] { predicate: A => Bool } { scanner: () => R / Scan[A] }: R / { Scan[A], stop } = {
93
87
  var stopped = false
94
- if (predicate(do peek())) { stopped = true; do skip() }
88
+ if (predicate(do peek())) { stopped = true; skip() }
95
89
  try {
96
90
  scanner()
97
- } with Scan[A] {
98
- def peek() = resume {
91
+ } with peek[A] {
92
+ resume {
99
93
  if (stopped) { do stop() } else { do peek() }
100
94
  }
101
- def skip() = resume {
95
+ } with read[A] {
96
+ resume {
102
97
  if (stopped) { do stop() } else {
103
- do skip()
98
+ val t = do read()
104
99
  boundary {
105
- if (predicate(do peek())) { stopped = true; do skip() }
100
+ if (predicate(do peek())) { stopped = true; skip() }
106
101
  }
102
+ return t
107
103
  }
108
104
  }
109
105
  }
@@ -165,7 +161,7 @@ def readHexadecimal(): Int / { Scan[Char], fail } = {
165
161
  /// Fails whenn there isn't at least one digit.
166
162
  def readInteger(): Int / { Scan[Char], fail } =
167
163
  must { do peek() } match {
168
- case '-' => must { do skip() }; neg(readDecimal())
164
+ case '-' => must { skip() }; neg(readDecimal())
169
165
  case c and isDigit(c) => readDecimal()
170
166
  case _ => do fail()
171
167
  }
@@ -174,28 +170,29 @@ namespace returning {
174
170
 
175
171
  /// Run a scanner by reading from an input stream.
176
172
  def scanner[A, R] { scanner: () => R / Scan[A] }: R / read[A] = {
173
+ def upstream(): A / stop = do read()
177
174
  var last = None()
178
175
  try {
179
176
  scanner()
180
- } with Scan[A] {
181
- def peek() = resume {
177
+ } with peek[A] { () =>
178
+ resume {
182
179
  last match {
183
180
  case None() =>
184
- val t = do read()
181
+ val t = upstream()
185
182
  last = Some(t)
186
183
  return t
187
184
  case Some(t) =>
188
185
  return t
189
186
  }
190
187
  }
191
- def skip() = resume {
188
+ } with read[A] {
189
+ resume {
192
190
  last match {
193
191
  case None() =>
194
- val _ = do read()
195
- return ()
192
+ upstream()
196
193
  case Some(t) =>
197
194
  last = None()
198
- return ()
195
+ return t
199
196
  }
200
197
  }
201
198
  }
@@ -243,10 +243,14 @@ def intersection[A](s1: Set[A], s2: Set[A]): Set[A] =
243
243
  def each[A](set: Set[A]): Unit / emit[A] =
244
244
  set.foreach { x => do emit(x) }
245
245
 
246
+ /// Collects elements emitted by a push stream producer `stream` into a new set
247
+ /// using provided `compare` function, discarding the result of the producer.
246
248
  def collect[A](compare: (A, A) => Ordering at {}) { stream: () => Unit / emit[A] }: Set[A] =
247
249
  returning::collect[A, Unit](compare){stream}.second
248
250
 
249
251
  namespace returning {
252
+ /// Collects elements emitted by a push stream producer `stream` into a new set
253
+ /// using provided `compare` function, returning the result of the producer as well as the collected set.
250
254
  def collect[A, R](compare: (A, A) => Ordering at {}) { stream: () => R / emit[A] }: (R, Set[A]) =
251
255
  try {
252
256
  (stream(), empty(compare))
@@ -314,8 +314,8 @@ def tee[A]{ cons1: { => Unit / emit[A] } => Unit }{ cons2: { => Unit / emit[A] }
314
314
  }
315
315
  }
316
316
 
317
- /// Return exactly the very first element of the given stream
318
- /// or raise MissingValue if no elements are emitted.
317
+ /// Extract exactly the very first element of the given stream
318
+ /// or fails if no elements are emitted.
319
319
  ///
320
320
  /// Examples:
321
321
  /// ```effekt
@@ -333,6 +333,50 @@ def first[A] { stream: () => Unit / emit[A] }: A / fail = {
333
333
  } with emit[A] { a => a }
334
334
  }
335
335
 
336
+ /// Extract the nth element of the given stream.
337
+ /// Fails if not enough elements are emitted
338
+ def nth[A](n: Int){ stream: () => Unit / emit[A] }: A / fail = {
339
+ var m = n
340
+ try {
341
+ def body(): A = { stream(); do fail() }
342
+ body()
343
+ } with emit[A] { a =>
344
+ if (m == 0) {
345
+ return a
346
+ } else {
347
+ m = m - 1
348
+ resume(())
349
+ }
350
+ }
351
+ }
352
+
353
+ // Counting and padding
354
+ // --------------------
355
+
356
+ /// get current position in stream
357
+ effect getPos(): Int
358
+
359
+ /// Track position in stream, starting with init
360
+ /// Handles getPos
361
+ def tracking[A](init: Int){ body: => Unit / { emit[A], getPos } }: Unit / emit[A] = {
362
+ var n = init
363
+ try body()
364
+ with emit[A] { b => n = n + 1; resume(do emit[A](b)) }
365
+ with getPos{ resume(n) }
366
+ }
367
+
368
+ /// Pad to a multiple of fac emitted values, by emitting gen repeatedly
369
+ def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
370
+ while(mod(do getPos(), fac) != 0) {
371
+ do emit[A](gen())
372
+ }
373
+ }
374
+
375
+ /// Track how many bytes were emitted (in the body)
376
+ /// Handles getPos
377
+ def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
378
+ tracking[A](0){body}
379
+
336
380
  namespace returning {
337
381
 
338
382
  /// Canonical handler of push streams that performs `action` for every
@@ -33,6 +33,9 @@ def assert(obtained: String, expected: String, msg: String): Unit / Assertion =
33
33
  def assert(obtained: Int, expected: Int): Unit / { Assertion, Formatted } =
34
34
  assertEqual(obtained, expected) { (x, y) => x == y } { x => show(x) }
35
35
 
36
+ def assert(obtained: Byte, expected: Byte): Unit / { Assertion, Formatted } =
37
+ assertEqual(obtained, expected) { (x, y) => x == y } { x => show(x) }
38
+
36
39
  def assert(obtained: Bool, expected: Bool): Unit / { Assertion, Formatted } =
37
40
  assertEqual(obtained, expected) { (x, y) => x == y } { x => show(x) }
38
41
 
@@ -68,6 +71,13 @@ def assertThrown[E](ex: on[E]) { body: => Unit / Exception[E] }: Unit / Assertio
68
71
  def assertThrows[E] { body: => Unit / Exception[E] }: Unit / Assertion =
69
72
  do assert(on[E].default { true } { body(); false }, "Expected Exception, but exited normally")
70
73
 
74
+ def assertFail { body: => Unit / fail }: Unit / Assertion =
75
+ do assert(succeeds {body}, "Expected 'fail', but exited normally")
76
+
77
+ def assertNoFail { body: => Unit / fail }: Unit / Assertion =
78
+ onFail { do assert(false, "Unexpected 'fail'") } {body}
79
+
80
+
71
81
  interface Test {
72
82
  def success(name: String, duration: Int): Unit
73
83
  def failure(name: String, msg: String, duration: Int): Unit
@@ -826,4 +826,43 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Pos options, Stack stack) {
826
826
  }
827
827
  }
828
828
 
829
+ // Returns the required buffer size if the variable exists, or -1 if it does not.
830
+ // This avoids allocating when the variable is not set.
831
+ Int c_getenv_size(struct Pos name) {
832
+ char* name_str = c_bytearray_into_nullterminated_string(name);
833
+ erasePositive(name);
834
+
835
+ char dummy;
836
+ size_t size = 1; // Note: size = 1 is required, otherwise libuv always returns EINVAL.
837
+ int result = uv_os_getenv(name_str, &dummy, &size);
838
+ free(name_str);
839
+
840
+ if (result == UV_ENOENT) return -1;
841
+ if (result == 0 || result == UV_ENOBUFS) return (Int)size;
842
+ return -1;
843
+ }
844
+
845
+ // Returns the value, given the caller already knows it exists with
846
+ // the given buffer size. Caller passes the size from c_getenv_size.
847
+ struct Pos c_getenv_value(struct Pos name, Int size) {
848
+ char* name_str = c_bytearray_into_nullterminated_string(name);
849
+ erasePositive(name);
850
+
851
+ char* buffer = malloc((size_t)size);
852
+ size_t buf_size = (size_t)size;
853
+ int result = uv_os_getenv(name_str, buffer, &buf_size);
854
+ free(name_str);
855
+
856
+ if (result != 0) {
857
+ // This should not happen, since we already checked with `c_getenv_size`
858
+ // ...but in case it does, we return an empty string instead.
859
+ free(buffer);
860
+ return c_bytearray_new(0);
861
+ }
862
+
863
+ struct Pos value = c_bytearray_from_nullterminated_string(buffer);
864
+ free(buffer);
865
+ return value;
866
+ }
867
+
829
868
  #endif
@@ -1,5 +1,5 @@
1
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>
2
- <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/og-image.jpg"><meta name=twitter:image content="https://www.eclipse.org/images/og-image.jpg"><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.144.2"><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=1772392798"><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 href=#main class=skip-link>Skip to content</a><div class=eclipsefdn-featured-story-banner data-publish-target=eclipse_org></div><header class="header-wrapper header-wrapper" id=header><div class="navigation-bar dark"><div class=container><div class=navigation-bar-inner><div class=navigation-bar-left><a class="focus-highlight focus-inverse" href=/><img class=navigation-bar-brand src=https://www.eclipse.org/eclipse.org-common/themes/solstice/public/images/logo/eclipse-foundation-white-orange.svg alt="Eclipse Foundation"></a></div><nav class="navigation-bar-nav hidden-xs hidden-sm"><ul><li><button type=button class="btn btn-link" data-target=join-us-menu>Join us</button></li><li><button type=button class="btn btn-link" data-target=what-we-do-menu>What we do</button></li><li><button type=button class="btn btn-link" data-target=resources-menu>Resources</button></li><li><button type=button class="btn btn-link" data-target=about-us-menu>About us</button></li></ul></nav><div class=navigation-bar-right><div class="navigation-bar-controls hidden-xs hidden-sm"><ul><li class=dropdown><a class="dropdown-toggle link-unstyled focus-highlight" tabindex=0 id=search-dropdown-toggle data-toggle=dropdown href=# aria-label=Search><i class="fa-solid fa-magnifying-glass" aria-hidden=true></i></a><form class="search-bar dropdown-menu dropdown-menu-right" aria-labelledby=search-dropdown-toggle action=https://www.eclipse.org/home/search/ method=get><input class=search-bar-input name=q placeholder=Search>
2
+ <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/og-image.jpg"><meta name=twitter:image content="https://www.eclipse.org/images/og-image.jpg"><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.144.2"><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=1773598796"><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 href=#main class=skip-link>Skip to content</a><div class=eclipsefdn-featured-story-banner data-publish-target=eclipse_org></div><header class="header-wrapper header-wrapper" id=header><div class="navigation-bar dark"><div class=container><div class=navigation-bar-inner><div class=navigation-bar-left><a class="focus-highlight focus-inverse" href=/><img class=navigation-bar-brand src=https://www.eclipse.org/eclipse.org-common/themes/solstice/public/images/logo/eclipse-foundation-white-orange.svg alt="Eclipse Foundation"></a></div><nav class="navigation-bar-nav hidden-xs hidden-sm"><ul><li><button type=button class="btn btn-link" data-target=join-us-menu>Join us</button></li><li><button type=button class="btn btn-link" data-target=what-we-do-menu>What we do</button></li><li><button type=button class="btn btn-link" data-target=resources-menu>Resources</button></li><li><button type=button class="btn btn-link" data-target=about-us-menu>About us</button></li></ul></nav><div class=navigation-bar-right><div class="navigation-bar-controls hidden-xs hidden-sm"><ul><li class=dropdown><a class="dropdown-toggle link-unstyled focus-highlight" tabindex=0 id=search-dropdown-toggle data-toggle=dropdown href=# aria-label=Search><i class="fa-solid fa-magnifying-glass" aria-hidden=true></i></a><form class="search-bar dropdown-menu dropdown-menu-right" aria-labelledby=search-dropdown-toggle action=https://www.eclipse.org/home/search/ method=get><input class=search-bar-input name=q placeholder=Search>
3
3
  <button class="btn btn-link" type=submit>
4
4
  <i class="fa-solid fa-arrow-right"></i></button></form></li><li class=dropdown><a class="dropdown-toggle focus-highlight" id=user-menu href=# data-toggle=dropdown aria-haspopup=true aria-expanded=false tabindex=0><i class="fa-regular fa-user" aria-hidden=true></i></a><ul class="dropdown-menu dropdown-menu-right" aria-labelledby=user-menu><li><a href=https://accounts.eclipse.org/user><i class="fa-solid fa-user margin-right-10" aria-hidden=true></i>
5
5
  View my account</a></li><li><a href=https://accounts.eclipse.org/user/edit><i class="fa-solid fa-edit margin-right-10" aria-hidden=true></i>
@@ -43,4 +43,4 @@ Manage cookies</a></li></ul></li></ul></div><div class="navigation-bar-ctas hidd
43
43
  <i class="fa-brands fa-youtube fa-stack-1x fa-inverse" aria-hidden=true></i></span></a></li><li><a href=https://www.linkedin.com/company/eclipse-foundation aria-label=LinkedIn><span class=fa-stack><i class="fa-solid fa-circle fa-stack-2x" aria-hidden=true></i>
44
44
  <i class="fa-brands fa-linkedin-in fa-stack-1x fa-inverse" aria-hidden=true></i></span></a></li></ul></section><section class=newsletter-section><p class="subheading text-tertiary">Stay up to date</p><h2>Subscribe to our newsletter</h2><form class="newsletter-form margin-top-40" id=mc-embedded-subscribe-form action="https://eclipse.us6.list-manage.com/subscribe/post?u=eaf9e1f06f194eadc66788a85&amp;id=e7538485cd&amp;f_id=00f9c2e1f0" method=post novalidate target=_blank><div class=text-field-outlined><input name=EMAIL type=email placeholder="Your email">
45
45
  <button class=newsletter-form-submit type=submit name=subscribe id=mc-embedded-subscribe aria-label=Submit>
46
- <i class="fa-solid fa-arrow-right" aria-hidden=true></i></button></div></form></section><section class=footer-bottom-section><img class="img-responsive logo" src=https://www.eclipse.org/eclipse.org-common/themes/solstice/public/images/logo/eclipse-foundation-white.svg alt="Eclipse Foundation"><div class=legal-information><p>Copyright &copy; Eclipse Foundation AISBL. All rights reserved.</p><ul class=legal-links><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/legal/>Legal</a></li></ul></div></section></div></div></footer><div id=footer-js><script src="/public/js/main.js?v1.0?v=1772392798"></script><script>(function(){})(jQuery,document)</script></div></body></html>
46
+ <i class="fa-solid fa-arrow-right" aria-hidden=true></i></button></div></form></section><section class=footer-bottom-section><img class="img-responsive logo" src=https://www.eclipse.org/eclipse.org-common/themes/solstice/public/images/logo/eclipse-foundation-white.svg alt="Eclipse Foundation"><div class=legal-information><p>Copyright &copy; Eclipse Foundation AISBL. All rights reserved.</p><ul class=legal-links><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/legal/>Legal</a></li></ul></div></section></div></div></footer><div id=footer-js><script src="/public/js/main.js?v1.0?v=1773598796"></script><script>(function(){})(jQuery,document)</script></div></body></html>