From 01b2852b653a81c4d5e197a0c52659c7e0dcaf5f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 18 Feb 2015 14:48:19 -0800 Subject: 782 - promote literate version to canonical C++ version --- cpp/makefile | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'cpp/makefile') diff --git a/cpp/makefile b/cpp/makefile index 0dcdbce5..97187383 100644 --- a/cpp/makefile +++ b/cpp/makefile @@ -1,25 +1,26 @@ -mu: makefile type_list function_list file_list test_file_list test_list - g++ -O3 -Wall -Wextra -fno-strict-aliasing boot.cc -o mu +mu: makefile tangle/tangle mu.cc + g++ -g -Wall -Wextra -fno-strict-aliasing mu.cc -o mu + -@./mu test -type_list: boot.cc [0-9]*.cc - @# assumes struct decl has space before '{' - @grep -h "^struct .* {" [0-9]*.cc |perl -pwe 's/(struct *[^ ]*).*/$$1;/' > type_list - @grep -h typedef [0-9]*.cc >> type_list +# To see what the program looks like after all layers have been applied, read +# mu.cc +mu.cc: 0* + ./tangle/tangle --until 999 > mu.cc + @make autogenerated_lists >/dev/null -function_list: boot.cc [0-9]*.cc - @# assumes function decl has space before '{' - @grep -h "^[^ #].*) {" [0-9]*.cc |perl -pwe 's/ {.*/;/' > function_list - @grep -h "^[[:space:]]*TEST(" [0-9]*.cc |perl -pwe 's/^\s*TEST\((.*)\)$$/void test_$$1();/' >> function_list +tangle/tangle: + cd tangle && make -file_list: boot.cc [0-9]*.cc - @ls [0-9]*.cc |grep -v "\.test\.cc$$" |perl -pwe 's/.*/#include "$$&"/' > file_list +# auto-generated files; by convention they end in '_list'. +.PHONY: autogenerated_lists +autogenerated_lists: mu.cc function_list test_list -test_file_list: [0-9]*.test.cc - @ls [0-9]*.test.cc |perl -pwe 's/.*/#include "$$&"/' > test_file_list +function_list: mu.cc + @grep -h "^[^ #].*) {" mu.cc |perl -pwe 's/ {.*/;/' > function_list -test_list: [0-9]*.cc - @grep -h "^[[:space:]]*void test_" [0-9]*.cc |perl -pwe 's/^\s*void (.*)\(\) {$$/$$1,/' > test_list - @grep -h "^[[:space:]]*TEST(" [0-9]*.cc |perl -pwe 's/^\s*TEST\((.*)\)$$/test_$$1,/' >> test_list +test_list: mu.cc + @grep -h "^[[:space:]]*void test_" mu.cc |perl -pwe 's/^\s*void (.*)\(\) {.*/$$1,/' > test_list clean: - rm -rf mu* *_list + cd tangle && make clean + rm -rf mu.cc mu *_list -- cgit 1.4.1-2-gfad0
path: root/070table.mu
blob: 21184084d8e0fe70bb1ed204df62f72c9ea96335 (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

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
nnel.mu?h=main&id=ce87c19ee42bc52c5ab9a1ee3c431a9423e5a885'>ce87c19e ^
1
2
3
4
5
6
7
8
9

                                                                
                                                                         
                                          
             
                  
         
                       
   

                                     
                                                  
                             
 
                         
                 
        
   
 
 
                                                                                 
                                             
             
                  

                                      

                                                    
                                                  
                                       

        
   
 
 
          
             
                                                                                        
                                                                  

                                                   

                           
 
# example program: communicating between routines using channels

def producer sink:address:sink:character -> sink:address:sink:character [
  # produce characters 1 to 5 on a channel
  local-scope
  load-ingredients
  # n = 0
  n:character <- copy 0
  {
    done?:boolean <- lesser-than n, 5
    break-unless done?
    # other threads might get between these prints
    $print [produce: ], n, [ 
]
    sink <- write sink, n
    n <- add n, 1
    loop
  }
]

def consumer source:address:source:character -> source:address:source:character [
  # consume and print integers from a channel
  local-scope
  load-ingredients
  {
    # read an integer from the channel
    n:character, eof?:boolean, source <- read source
    break-if eof?
    # other threads might get between these prints
    $print [consume: ], n:character, [ 
]
    loop
  }
]

def main [
  local-scope
  source:address:source:character, sink:address:sink:character <- new-channel 3/capacity
  # create two background 'routines' that communicate by a channel
  routine1:number <- start-running producer, sink
  routine2:number <- start-running consumer, source
  wait-for-routine routine1
  wait-for-routine routine2
]
o"><- multiply n, -1 ]