about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2017-09-23 18:31:26 -0700
committerKartik K. Agaram <vc@akkartik.com>2017-09-23 18:41:47 -0700
commit72cf994869e19f6bdc4678e1122f0082d07d4a11 (patch)
treee7a39da677884476a3088a96b1dab125d310316d
parent50685c29bdafaa3fab19c832b421707b0442fdc6 (diff)
downloadmu-72cf994869e19f6bdc4678e1122f0082d07d4a11.tar.gz
4002
-rw-r--r--061text.mu41
-rw-r--r--chessboard.mu37
-rw-r--r--factorial.mu8
-rw-r--r--tangle.mu25
4 files changed, 39 insertions, 72 deletions
diff --git a/061text.mu b/061text.mu
index 913bc53a..1ef8c393 100644
--- a/061text.mu
+++ b/061text.mu
@@ -11,12 +11,9 @@ def equal a:text, b:text -> result:bool [
   a-len:num <- length *a
   b-len:num <- length *b
   # compare lengths
-  {
-    trace 99, [text-equal], [comparing lengths]
-    length-equal?:bool <- equal a-len, b-len
-    break-if length-equal?
-    return 0
-  }
+  trace 99, [text-equal], [comparing lengths]
+  length-equal?:bool <- equal a-len, b-len
+  return-unless length-equal?, 0/false
   # compare each corresponding character
   trace 99, [text-equal], [comparing characters]
   i:num <- copy 0
@@ -25,11 +22,8 @@ def equal a:text, b:text -> result:bool [
     break-if done?
     a2:char <- index *a, i
     b2:char <- index *b, i
-    {
-      chars-match?:bool <- equal a2, b2
-      break-if chars-match?
-      return 0
-    }
+    chars-match?:bool <- equal a2, b2
+    return-unless chars-match?, 0/false
     i <- add i, 1
     loop
   }
@@ -344,11 +338,8 @@ scenario append-to-buffer-of-non-characters [
 def buffer-to-array in:&:buffer:_elem -> result:&:@:_elem [
   local-scope
   load-ingredients
-  {
-    # propagate null buffer
-    break-if in
-    return 0
-  }
+  # propagate null buffer
+  return-unless in, 0
   len:num <- get *in, length:offset
   s:&:@:_elem <- get *in, data:offset
   # we can't just return s because it is usually the wrong length
@@ -995,13 +986,10 @@ def match-at text:text, pattern:text, idx:num -> result:bool [
   load-ingredients
   pattern-len:num <- length *pattern
   # check that there's space left for the pattern
-  {
-    x:num <- length *text
-    x <- subtract x, pattern-len
-    enough-room?:bool <- lesser-or-equal idx, x
-    break-if enough-room?
-    return 0/not-found
-  }
+  x:num <- length *text
+  x <- subtract x, pattern-len
+  enough-room?:bool <- lesser-or-equal idx, x
+  return-unless enough-room?, 0/not-found
   # check each character of pattern
   pattern-idx:num <- copy 0
   {
@@ -1009,11 +997,8 @@ def match-at text:text, pattern:text, idx:num -> result:bool [
     break-if done?
     c:char <- index *text, idx
     exp:char <- index *pattern, pattern-idx
-    {
-      match?:bool <- equal c, exp
-      break-if match?
-      return 0/not-found
-    }
+    match?:bool <- equal c, exp
+    return-unless match?, 0/not-found
     idx <- add idx, 1
     pattern-idx <- add pattern-idx, 1
     loop
diff --git a/chessboard.mu b/chessboard.mu
index 7351c5ce..27e16fd8 100644
--- a/chessboard.mu
+++ b/chessboard.mu
@@ -268,21 +268,12 @@ def read-file stdin:&:source:char, screen:&:screen -> file:num, quit:bool, error
   load-ingredients
   c:char, eof?:bool, stdin <- read stdin
   return-if eof?, 0/dummy, 1/quit, 0/error
-  {
-    q-pressed?:bool <- equal c, 81/Q
-    break-unless q-pressed?
-    return 0/dummy, 1/quit, 0/error
-  }
-  {
-    q-pressed? <- equal c, 113/q
-    break-unless q-pressed?
-    return 0/dummy, 1/quit, 0/error
-  }
-  {
-    empty-fake-keyboard?:bool <- equal c, 0/eof
-    break-unless empty-fake-keyboard?
-    return 0/dummy, 1/quit, 0/error
-  }
+  q-pressed?:bool <- equal c, 81/Q
+  return-if q-pressed?, 0/dummy, 1/quit, 0/error
+  q-pressed? <- equal c, 113/q
+  return-if q-pressed?, 0/dummy, 1/quit, 0/error
+  empty-fake-keyboard?:bool <- equal c, 0/eof
+  return-if empty-fake-keyboard?, 0/dummy, 1/quit, 0/error
   {
     newline?:bool <- equal c, 10/newline
     break-unless newline?
@@ -315,16 +306,12 @@ def read-rank stdin:&:source:char, screen:&:screen -> rank:num, quit?:bool, erro
   load-ingredients
   c:char, eof?:bool, stdin <- read stdin
   return-if eof?, 0/dummy, 1/quit, 0/error
-  {
-    q-pressed?:bool <- equal c, 8/Q
-    break-unless q-pressed?
-    return 0/dummy, 1/quit, 0/error
-  }
-  {
-    q-pressed? <- equal c, 113/q
-    break-unless q-pressed?
-    return 0/dummy, 1/quit, 0/error
-  }
+  q-pressed?:bool <- equal c, 81/Q
+  return-if q-pressed?, 0/dummy, 1/quit, 0/error
+  q-pressed? <- equal c, 113/q
+  return-if q-pressed?, 0/dummy, 1/quit, 0/error
+  empty-fake-keyboard?:bool <- equal c, 0/eof
+  return-if empty-fake-keyboard?, 0/dummy, 1/quit, 0/error
   {
     newline?:bool <- equal c, 10  # newline
     break-unless newline?
diff --git a/factorial.mu b/factorial.mu
index 8a261207..5b43d151 100644
--- a/factorial.mu
+++ b/factorial.mu
@@ -10,12 +10,8 @@ def main [
 def factorial n:num -> result:num [
   local-scope
   load-ingredients
-  {
-    # if n=0 return 1
-    zero?:bool <- equal n, 0
-    break-unless zero?
-    return 1
-  }
+  # if n=0 return 1
+  return-unless n, 1
   # return n * factorial(n-1)
   x:num <- subtract n, 1
   subresult:num <- factorial x
diff --git a/tangle.mu b/tangle.mu
index 4b13c910..e8388a72 100644
--- a/tangle.mu
+++ b/tangle.mu
@@ -3,30 +3,29 @@
 # We construct a factorial function with separate base and recursive cases.
 # Compare factorial.mu.
 #
-# This isn't a very tasteful example, just a simple demonstration of
+# This isn't a very tasteful example, just a basic demonstration of
 # possibilities.
 
 def factorial n:num -> result:num [
   local-scope
   load-ingredients
-  {
-    <base-case>
-  }
-  <recursive-case>
+  <factorial-cases>
 ]
 
-after <base-case> [
+after <factorial-cases> [
   # if n=0 return 1
-  zero?:bool <- equal n, 0
-  break-unless zero?
-  return 1
+  return-unless n, 1
 ]
 
-after <recursive-case> [
+after <factorial-cases> [
   # return n * factorial(n - 1)
-  x:num <- subtract n, 1
-  subresult:num <- factorial x
-  result <- multiply subresult, n
+  {
+    break-unless n
+    x:num <- subtract n, 1
+    subresult:num <- factorial x
+    result <- multiply subresult, n
+    return result
+  }
 ]
 
 def main [