# Chessboard program: you type in moves in algebraic notation, and it'll # display the position after each move. def main [ open-console # take control of screen, keyboard and mouse # The chessboard function takes keyboard and screen objects as 'ingredients'. # # In mu it is good form (though not required) to explicitly show the # hardware you rely on. # # Here the console and screen are both 0, which usually indicates real # hardware rather than a fake for testing as you'll see below. chessboard 0/screen, 0/console close-console # clean up screen, keyboard and mouse ] ## But enough about mu. Here's what it looks like to run the chessboard program. scenario print-board-and-read-move [ trace-until 100/app # we'll make the screen really wide because the program currently prints out a long line assume-screen 120/width, 20/height # initialize keyboard to type in a move assume-console [ type [a2-a4 ] ] run [ local-scope screen:&:screen, console:&:console <- chessboard screen:&:screen, console:&:console # icon for the cursor cursor-icon:char <- copy 9251/␣ screen <- print screen, cursor-icon ] screen-should-contain [ # 1 2 3 4 5 6 7 8 9 10 11 # 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 .Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves. . . . .8 | r n b q k b n r . .7 | p p p p p p p p . .6 | . .5 | . .4 | P . .3 | . .2 | P P P P P P P . .1 | R N B Q K B N R . . +---------------- . . a b c d e f g h . . . .Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>. . . . .Hit 'q' to exit. . . . .move: ␣ . . . . . ] ] ## Here's how 'chessboard' is implemented. type board = address:@:&:@:char def chessboard screen:&:screen, console:&:console -> screen:&:screen, console:&:console [ local-scope load-ingredients board:board <- initial-position # hook up stdin stdin-in:&:source:char, stdin-out:&:sink:char <- new-channel 10/capacity start-running send-keys-to-channel, console, stdin-out, screen # buffer lines in stdin buffered-stdin-in:&:source:char, buffered-stdin-out:&:sink:char <- new-channel 10/capacity start-running buffer-lines, stdin-in, buffered-stdin-out { print screen, [Stupid text-mode chessboard. White pieces in uppercase; black pieces in lowercase. No checking for legal moves. ] cursor-to-next-line screen print-board screen, board cursor-to-next-line screen print screen, [Type in your move as <from square>-<to square>. For example: 'a2-a4'. Then press <enter>. ] cursor-to-next-line screen print screen [Hit 'q' to exit. ] { cursor-to-next-line screen screen <- print screen, [move: ] m:&:move, quit:boolean, error:boolean <- read-move buffered-stdin-in, screen break-if quit, +quit:label buffered-stdin-in <- clear buffered-stdin-in # cleanup after error. todo: test this? loop-if error } board <- make-move board, m screen <- clear-screen screen loop } +quit ] ## a board is an array of files, a file is an array of characters (squares) def new-board initial-position:&:@:char -> board:board [ local-scope load-ingredients # assert(length(initial-position) == 64) len:num <- length *initial-position correct-length?:boolean <- equal len, 64 assert correct-length?, [chessboard had incorrect size] # board is an array of pointers to files; file is an array of characters board <- new {(address array character): type}, 8 col:num <- copy 0 { done?:boolean <- equal col, 8 break-if done? file:&:@:char <- new-file initial-position, col *board <- put-index *board, col, file col <- add col, 1 loop } ] def new-file position:&:@:char, index:num -> result:&:@:char [ local-scope load-ingredients index <- multiply index, 8 result <- new character:type, 8 row:num <- copy 0 { done?:boolean <- equal row, 8 break-if done? square:char <- index *position, index *result <- put-index *result, row, square row <- add row, 1 index <- add index, 1 loop } ] def print-board screen:&:screen, board:board -> screen:&:screen [ local-scope load-ingredients row:num <- copy 7 # start pri<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Mu - filesystem.mu</title> <meta name="Generator" content="Vim/7.4"> <meta name="plugin-version" content="vim7.4_v2"> <meta name="syntax" content="none"> <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy="> <meta name="colorscheme" content="minimal"> <style type="text/css"> <!-- pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 12pt; font-size: 1em; } .muRecipe { color: #ff8700; } .Delimiter { color: #800080; } .Comment { color: #9090ff; } .Constant { color: #00a0a0; } .Special { color: #c00000; } .muControl { color: #c0a020; } --> </style> <script type='text/javascript'> <!-- --> </script> </head> <body> <pre id='vimCodeElement'> <span class="Comment"># example program: copy one file into another, character by character</span> <span class="Comment"># BEWARE: this will modify your file system</span> <span class="Comment"># before running it, put some text into /tmp/mu-x</span> <span class="Comment"># after running it, check /tmp/mu-y</span> <span class="muRecipe">def</span> main [ <span class="Constant">local-scope</span> source-file:&:source:char<span class="Special"> <- </span>start-reading <span class="Constant">0/real-filesystem</span>, <span class="Constant">[/tmp/mu-x]</span> sink-file:&:sink:char, write-routine:num<span class="Special"> <- </span>start-writing <span class="Constant">0/real-filesystem</span>, <span class="Constant">[/tmp/mu-y]</span> <span class="Delimiter">{</span> c:char, done?:boolean, source-file<span class="Special"> <- </span>read source-file <span class="muControl">break-if</span> done? sink-file<span class="Special"> <- </span>write sink-file, c <span class="muControl">loop</span> <span class="Delimiter">}</span> close sink-file <span class="Comment"># make sure to wait for the file to be actually written to disk</span> <span class="Comment"># (Mu practices structured concurrency: <a href="http://250bpm.com/blog:71)">http://250bpm.com/blog:71)</a></span> wait-for-routine write-routine ] </pre> </body> </html> <!-- vim: set foldmethod=manual : -->