From d2244a2f117618df541c52a1f7ec5d0fed8bcb7a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 11 May 2015 12:00:50 -0700 Subject: 1345 --- Readme.md | 10 +++++++--- channel.mu | 12 ++++++++---- counters.mu | 9 ++++++--- display.mu | 3 ++- fork.mu | 2 ++ keyboard.mu | 3 ++- screen.mu | 6 +++++- tangle.mu | 9 ++++++--- x.mu | 9 ++++++--- 9 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Readme.md b/Readme.md index 7630318b..08cdf7a5 100644 --- a/Readme.md +++ b/Readme.md @@ -94,9 +94,9 @@ returns some *results*. [results] <- instruction [ingredients] ``` -Result and ingredient *reagents* have to be simple; you can't nest operations. -But you can have any number of them. In particular you can have any number of -results. For example, you can perform integer division as follows: +Result and ingredient *reagents* have to be variables. But you can have any +number of them. In particular you can have any number of results. For example, +you can perform integer division as follows: ``` quotient:integer, remainder:integer <- divide-with-remainder 11:literal, 3:literal @@ -247,6 +247,10 @@ inserting code at them. (You'll find this version in `tangle.mu`.) +Any instruction without ingredients or products that starts with a +non-alphanumeric character is a label. By convention we use '+' to indicate +label names. + This is a good time to point out that `{` and `}` are also just labels in Mu syntax, and that `break` and `loop` get rewritten as jumps to just after the enclosing `}` and `{` respectively. This gives us a simple sort of structured diff --git a/channel.mu b/channel.mu index a0c2b6dd..0206c2f9 100644 --- a/channel.mu +++ b/channel.mu @@ -1,3 +1,5 @@ +# example program: communicating between routines using channels + recipe producer [ # produce numbers 1 to 5 on a channel default-space:address:array:location <- new location:type, 30:literal @@ -8,7 +10,7 @@ recipe producer [ done?:boolean <- lesser-than n:integer, 5:literal break-unless done?:boolean # other threads might get between these prints - $print [produce: ], n:integer, [ + $print [produce: ], n:integer, [ ] chan:address:channel <- write chan:address:channel, n:integer n:integer <- add n:integer, 1:literal @@ -24,7 +26,7 @@ recipe consumer [ # read an integer from the channel n:integer, chan:address:channel <- read chan:address:channel # other threads might get between these prints - $print [consume: ], n:integer, [ + $print [consume: ], n:integer, [ ] loop } @@ -34,6 +36,8 @@ recipe main [ default-space:address:array:location <- new location:type, 30:literal chan:address:channel <- init-channel 3:literal # create two background 'routines' that communicate by a channel - routine1:integer <- start-running consumer:recipe, chan:address:channel - routine2:integer <- start-running producer:recipe, chan:address:channel + routine1:integer <- start-running producer:recipe, chan:address:channel + routine2:integer <- start-running consumer:recipe, chan:address:channel + wait-for-routine routine1:integer + wait-for-routine routine2:integer ] diff --git a/counters.mu b/counters.mu index 98199072..ef841400 100644 --- a/counters.mu +++ b/counters.mu @@ -23,9 +23,12 @@ recipe main [ b:address:space <- init-counter 23:literal # increment both by 2 but in different ways increment-counter a:address:space, 1:literal - bres:integer <- increment-counter b:address:space, 2:literal - ares:integer <- increment-counter a:address:space, 1:literal + b-value:integer <- increment-counter b:address:space, 2:literal + a-value:integer <- increment-counter a:address:space, 1:literal # check results - $print [Contents of counters a: ], ares:integer, [ b: ], bres:integer, [ + $print [Contents of counters +] + # trailing space in next line is to help with syntax highlighting + $print [a: ], a-value:integer, [ b: ], b-value:integer, [ ] ] diff --git a/display.mu b/display.mu index 5d2cb17d..bff74a5d 100644 --- a/display.mu +++ b/display.mu @@ -1,4 +1,5 @@ -# example of raw primitives operating on display +# example program: managing the display + recipe main [ switch-to-display print-character-to-display 97:literal diff --git a/fork.mu b/fork.mu index 3917120f..c3be8310 100644 --- a/fork.mu +++ b/fork.mu @@ -1,3 +1,5 @@ +# example program: running multiple routines + recipe main [ start-running thread2:recipe { diff --git a/keyboard.mu b/keyboard.mu index d6f728f6..530275ca 100644 --- a/keyboard.mu +++ b/keyboard.mu @@ -1,7 +1,8 @@ -# example reading keys from keyboard +# example program: reading keys from keyboard # # Keeps printing 'a' until you press a key. Then prints the key you pressed # and exits. + recipe main [ switch-to-display { diff --git a/screen.mu b/screen.mu index fc971e11..fedffa5d 100644 --- a/screen.mu +++ b/screen.mu @@ -1,4 +1,8 @@ -# example of recipes for working with screen objects +# example program: managing the display using 'screen' objects +# +# The zero screen below means 'use the real screen'. Tests can also use fake +# screens. + recipe main [ switch-to-display print-character 0:literal/screen, 97:literal diff --git a/tangle.mu b/tangle.mu index 10829ca5..cb322e46 100644 --- a/tangle.mu +++ b/tangle.mu @@ -1,6 +1,9 @@ -# 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 +# example program: constructing recipes out of order +# +# 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 # possibilities. recipe factorial [ diff --git a/x.mu b/x.mu index 0d40a4a7..7f80f845 100644 --- a/x.mu +++ b/x.mu @@ -1,5 +1,8 @@ +# example program: add two numbers + recipe main [ - 12:integer <- copy 1:literal - 13:integer <- copy 3:literal - 11:integer <- add 12:integer, 13:integer + 11:integer <- copy 1:literal + 12:integer <- copy 3:literal + 13:integer <- add 11:integer, 12:integer + $dump-memory ] -- cgit 1.4.1-2-gfad0