about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--mu.arc35
-rw-r--r--mu.arc.t15
-rw-r--r--tangle.mu50
3 files changed, 87 insertions, 13 deletions
diff --git a/mu.arc b/mu.arc
index 1e34249a..6e37918e 100644
--- a/mu.arc
+++ b/mu.arc
@@ -859,19 +859,26 @@
 ; see add-code below for adding to before* and after*
 
 (def insert-code (instrs)
-  (accum yield
-    (each instr instrs
-      (if (acons instr)
-        (yield instr)
-        ; label
-        (do
-          (each fragment (as cons before*.instr)
-            (each instr fragment
-              (yield instr)))
-          (yield instr)
-          (each fragment after*.instr
-            (each instr fragment
-              (yield instr))))))))
+  (loop (instrs instrs)
+    (accum yield
+      (each instr instrs
+        (if (and (acons instr) (~is 'begin car.instr))
+              ; simple instruction
+              (yield instr)
+            (and (acons instr) (is 'begin car.instr))
+              ; block
+              (yield `{begin ,@(recur cdr.instr)})
+            (atom instr)
+              ; label
+              (do
+;?                 (prn "tangling " instr)
+                (each fragment (as cons before*.instr)
+                  (each instr fragment
+                    (yield instr)))
+                (yield instr)
+                (each fragment after*.instr
+                  (each instr fragment
+                    (yield instr)))))))))
 
 ;; system software
 
@@ -1066,6 +1073,8 @@
       def
         (let (name (_make-br-fn body))  rest
           (assert (is 'make-br-fn _make-br-fn))
+;?           (prn keys.before* " -- " keys.after*)
+;?           (= function*.name (convert-names:convert-braces:prn:insert-code body)))
           (= function*.name (convert-names:convert-braces:insert-code body)))
 
       ; syntax: before <label> [ <instructions> ]
diff --git a/mu.arc.t b/mu.arc.t
index 069677c2..a1f0c90e 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -2775,4 +2775,19 @@
             (list before*!label1 after*!label1)))
   (prn "F - order matters within 'before' and 'after' fragments, but not *between* 'before' and 'after' fragments"))
 
+(reset)
+;? (new-trace "before-after-braces")
+(add-code '((after label1 [
+               ((1 integer) <- copy (0 literal))
+             ])
+            (def f1 [
+              { begin
+                label1
+              }
+             ])))
+(if (~iso function*!f1
+          '(label1
+            ((1 integer) <- copy (0 literal))))
+  (prn "F - before/after works inside blocks"))
+
 (reset)  ; end file with this to persist the trace for the final test
diff --git a/tangle.mu b/tangle.mu
new file mode 100644
index 00000000..fae99791
--- /dev/null
+++ b/tangle.mu
@@ -0,0 +1,50 @@
+; To demonstrate tangle directives, we'll construct a factorial function with
+; separate base and recursive cases. Compare factorial.mu.
+; This isn't a very realistic example, just a simple demonstration of
+; possibilities.
+
+(after base-case [
+  ; if n=0 return 1
+  ((zero? boolean) <- eq (n integer) (0 literal))
+  (break-unless (zero? boolean))
+  (reply (1 literal))
+])
+
+(after recursive-case [
+  ; return n*factorial(n-1)
+  ((x integer) <- sub (n integer) (1 literal))
+  ((subresult integer) <- factorial (x integer))
+  ((result integer) <- mul (subresult integer) (n integer))
+  (reply (result integer))
+])
+
+(def factorial [
+  ((default-scope scope-address) <- new (scope literal) (30 literal))
+  ((n integer) <- arg)
+  { begin
+    base-case
+  }
+  recursive-case
+])
+
+(def main [
+  ((1 integer) <- factorial (5 literal))
+  (print-primitive ("result: " literal))
+  (print-primitive (1 integer))
+  (print-primitive ("\n" literal))
+])
+
+;? (((default-scope scope-address) <- new (scope literal) (30 literal))
+;?  ((n integer) <- arg)
+;?  { begin
+;?    base-case
+;?  }
+;?  recursive-case
+;?  ((x integer) <- sub (n integer) (1 literal))
+;?  ((subresult integer) <- factorial (x integer))
+;?  ((result integer) <- mul (subresult integer) (n integer))
+;?  (reply (result integer)))
+;? 
+;? (((1 integer) <- factorial (5 literal)) (print-primitive (result:  literal)) (print-primitive (1 integer)) (print-primitive (
+;?  literal)))
+;?