Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da55ab48bf81c74ce64e930e8fc4b2685e455d8e (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
h1. Tutorial Remove C-Comment ( C )

h2. Scope

In this tutorial you will create a more complex model. The model implements a simple parser that removes comments (block comments and line comments) from a C source file. Therefore we will create two actors. One actor is responsible to perform the file operations, whether the second actor implements the parser.

You will perform the following steps:

# create a new model from scratch for C
# define a protocol
# define your own data type
# create the structure and the behavior by yourself
# generate, build and run the model

Make sure that you have set up the workspace as described in _Setting up the Workspace for C Projects_.

h2. Create a new model from scratch

Remember the following steps from the previous tutorials:
- select the _C/C++_ perspective
- From the main menue select _File->New->C Project_
- Name the project _RemoveComment_
- Project type is _Executable / Empty C Project_
- Toolchain is _MinGW_
- Add the folder _model_
- Add the model file and name it _RemoveComment.room_
- Add the Xtext nature.

The workspace should look like this:

!images/036-RemoveCommentC01.png!

Create a launch configuration for the C generator and add the include path and library as described in _HelloWorldC_.

The workspace should look like this:

!images/036-RemoveCommentC02.png!

Now the model is created and all settings for the code generator, compiler and linker are done.


h2. Create your own data type

The planed application should read a C source file and remove the comments. Therefore we need a file descriptor which is not part of the basic C types. The type for the file descriptor for MinGW is _FILE_. To make this type available on the model level, you have to declare the type in CTypes.room. To not interfere with other models, make a copy of _CTypes.room_ from _org.eclipse.modellib.c_ to your model folder.

!images/036-RemoveCommentC03.png!

Open _Ctypes.room_ and take a look at the declaration of _string_ (last line) which is not a basic C type.

_PrimitiveType string:ptCharacter -> charPtr default "0"_

With this declaration, you make the _string_ keyword available on model level as a primitive type. This type will be translated to _charPtr_ in your C sources. _charPtr_ is defined in _etDatatypes.h_. This header file is platform specific (_generic_). With this mechanism you can define your own type system on model level and map the model types to specific target/platform types. 

Add the following line in _CTypes.room_:

bc.. 
PrimitiveType file:ptInteger -> FILE default "0"
bq. 

_FILE_ is the native type for MinGW. Therefore you don´t need a mapping within _etDatatypes.h_. If your model should be portable across different platforms you should not take this shortcut.
 
h2. Create the model

Due to the former tutorials you should be familiar with the steps to create the model with protocols, actors and state machines.

The basic idea of the exercise is to create a file reader actor, which is responsible to open, close and read characters from the source file. Another actor receives the characters and filters the comments (parser). The remaining characters (pure source code) should be print out. 

Remember the logical steps: 
- create the model by the help of content assist (CTRL Space)
- name the model, subsystem and top level actor
- define the protocol (in this case it should be able to send a char, and to request the next char from the file reader)
- create the structure (file reader and parser with an appropriate port, create the references and connect the ports)
- create the state machines

Try to create the model by yourself and take the following solution as an example.

Structure:

!images/036-RemoveCommentC04.png!

File reader FSM:

!images/036-RemoveCommentC05.png!

Parser FSM:

!images/036-RemoveCommentC06.png!

Complete model:

bc..  
RoomModel RemoveComment {
	import room.basic.types.c.* from "CTypes.room"
	SubSystemClass RemoveCommentSubSys {
		ActorRef Topref:ATop 
	}
	ActorClass ATop {
		Structure {
			ActorRef reader: AFileReader
			ActorRef cParser: ACParser
			Binding reader.outPort and cParser.readerPort
		}
		Behavior { }
	}
	ActorClass AFileReader{
		Interface {
			Port outPort: FileReaderProtocol
		}
		Structure {
			usercode3{"#include <stdio.h>"
			}
			external Port outPort
			Attribute f:file ref
		}
		Behavior {
			StateMachine {
				Transition init: initial -> reading {
					action {
						"if ((f = fopen(\"test.txt\",\"r\")) != 0) {"
						"\tprintf(\"file open ok !\\r\\n\");"
						"\t}"
						"\telse {"
						"\tprintf(\"file not found !\\r\\n\");"
						"\tSubSysClass_shutdown();"
						"\t}"
					}
				}
				Transition tr0: reading -> reading {
					triggers {
						<getNextChar: outPort>
					}
					action {
						"int8 c;"
						"if ((c=fgetc(f)) != EOF) {"
						"\toutPort.nextChar(c);"
						"\t}"
						"\telse {"
						"\tfclose(f);"
						"\tprintf(\"file closed !\\r\\n\");"
						"\tSubSysClass_shutdown();"
						"\t}"
					}
				}
				State reading
			}
		}
	}
	ActorClass ACParser{
		Interface {
			conjugated Port readerPort: FileReaderProtocol
		}
		Structure {
			external Port readerPort
		}
		Behavior {
			StateMachine {
				Transition init: initial -> code {
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr0: code -> cp cp0 {
					triggers {
						<nextChar: readerPort>
					}
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr1: cp cp0 -> code {
					action {
						"printf(\"%c\",c);"
					}
				}
				Transition tr2: cp cp0 -> firstSlash {
					cond {
						"c==\'/\'"
					}
				}
				Transition tr3: firstSlash -> cp cp1 {
					triggers {
						<nextChar: readerPort>
					}
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr4: cp cp1 -> cp cp4
				Transition tr5: cp cp1 -> blockComment {
					cond {
						"c==\'*\'"
					}
				}
				Transition tr6: blockComment -> cp cp2 {
					triggers {
						<nextChar: readerPort>
					}
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr7: cp cp2 -> blockComment
				Transition tr8: cp cp2 -> firstStar {
					cond {
						"c==\'*\'"
					}
				}
				Transition tr9: firstStar -> cp cp3 {
					triggers {
						<nextChar: readerPort>
					}
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr10: cp cp3 -> blockComment
				Transition tr11: cp cp3 -> code {
					cond {
						"c==\'/\'"
					}
				}
				Transition tr12: cp cp4 -> code {
					action {
						"printf(\"%c%c\",\'/\',c);"
					}
				}
				Transition tr13: cp cp4 -> lineComment {
					cond {
						"c==\'/\'"
					}
				}
				Transition tr14: cp cp5 -> lineComment
				Transition tr15: lineComment -> cp cp5 {
					triggers {
						<nextChar: readerPort>
					}
					action {
						"readerPort.getNextChar();"
					}
				}
				Transition tr16: cp cp5 -> code {
					cond {
						"c==\'\\n\'"
					}
				}
				ChoicePoint cp0
				ChoicePoint cp1
				ChoicePoint cp2
				ChoicePoint cp3
				ChoicePoint cp4
				ChoicePoint cp5
				State code
				State firstSlash
				State blockComment
				State firstStar
				State lineComment
			}
		}
	}

	ProtocolClass FileReaderProtocol {
		incoming {
			Message getNextChar()
		}
		outgoing {
			Message nextChar(c:char)
		}
	}
}
bq. 

Take a look at the file attribute of the file reader. 

bc.. 
Attribute f:file ref
bq. 

_fopen_ expects a _FILE *_. _f:file ref_ declares a variable _f_ from type reference to _file_, which is a pointer to _FILE_.


h2. Generate, build and run the model

Before you can run the model you should copy one of the generated C source files into the project folder and name it _test.txt_. 

!images/036-RemoveCommentC07.png!

Generate, build and run the model.

Your output should start like this:

!images/036-RemoveCommentC08.png!


h2. Summary

This tutorial should help you to train the necessary steps to create a C model. By the way you have seen how to create your own type system for a real embedded project. An additional aspect was to show how simple it is to separate different aspects of the required functionality by the use of actors and protocols and make them reusable.

Back to the top