about summary refs log tree commit diff stats
path: root/html/088file.mu.html
blob: 6a19422753352fc9afb64d3d7d4d876b62ccc960 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!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 - 088file.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; }
.muData { color: #ffff00; }
.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"># Wrappers around file-system primitives that take a 'filesystem' object and</span>
<span class="Comment"># are thus easier to test.</span>

<span class="muData">container</span> filesystem [
  data:address:array:file-mapping
]

<span class="muData">container</span> file-mapping [
  name:address:array:character
  contents:address:array:character
]

<span class="muRecipe">def</span> start-reading fs:address:filesystem, filename:address:array:character<span class="muRecipe"> -&gt; </span>contents:address:source:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> fs
    <span class="Comment"># real file-system</span>
    file:number<span class="Special"> &lt;- </span>$open-file-for-reading filename
    assert file, <span class="Constant">[file not found]</span>
    contents:address:source:character, sink:address:sink:character<span class="Special"> &lt;- </span>new-channel <span class="Constant">30</span>
    start-running transmit-from-file file, sink
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># fake file system</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  data:address:array:file-mapping<span class="Special"> &lt;- </span>get *fs, <span class="Constant">data:offset</span>
  len:number<span class="Special"> &lt;- </span>length *data
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    tmp:file-mapping<span class="Special"> &lt;- </span>index *data, i
    curr-filename:address:array:character<span class="Special"> &lt;- </span>get tmp, <span class="Constant">name:offset</span>
    found?:boolean<span class="Special"> &lt;- </span>equal filename, curr-filename
    <span class="muControl">loop-unless</span> found?
    contents:address:source:character, sink:address:sink:character<span class="Special"> &lt;- </span>new-channel <span class="Constant">30</span>
    curr-contents:address:array:character<span class="Special"> &lt;- </span>get tmp, <span class="Constant">contents:offset</span>
    start-running transmit-from-text curr-contents, sink
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="muControl">return</span> <span class="Constant">0/not-found</span>
]

<span class="muRecipe">def</span> transmit-from-file file:number, sink:address:sink:character<span class="muRecipe"> -&gt; </span>sink:address:sink:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    c:character<span class="Special"> &lt;- </span>$read-from-file file
    <span class="muControl">break-unless</span> c
    sink<span class="Special"> &lt;- </span>write sink, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  sink<span class="Special"> &lt;- </span>close sink
  $close-file file
]

<span class="muRecipe">def</span> transmit-from-text contents:address:array:character, sink:address:sink:character<span class="muRecipe"> -&gt; </span>sink:address:sink:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  len:number<span class="Special"> &lt;- </span>length *contents
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    c:character<span class="Special"> &lt;- </span>index *contents, i
    sink<span class="Special"> &lt;- </span>write sink, c
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  sink<span class="Special"> &lt;- </span>close sink
]

<span class="muRecipe">def</span> start-writing fs:address:filesystem, filename:address:array:character<span class="muRecipe"> -&gt; </span>sink:address:sink:character, routine-id:number [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  file:number<span class="Special"> &lt;- </span>$open-file-for-writing filename
  source:address:source:character, sink:address:sink:character<span class="Special"> &lt;- </span>new-channel <span class="Constant">30</span>
  routine-id<span class="Special"> &lt;- </span>start-running transmit-to-file file, source
]

<span class="muRecipe">def</span> transmit-to-file file:number, source:address:source:character<span class="muRecipe"> -&gt; </span>file:number, source:address:source:character [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    c:character, done?:boolean, source<span class="Special"> &lt;- </span>read source
    <span class="muControl">break-if</span> done?
    $write-to-file file, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  $close-file file
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->