Mercurial > hg > CbC > CbC_llvm
comparison docs/tutorial/LangImpl1.rst @ 95:afa8332a0e37 LLVM3.8
LLVM 3.8
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2015 17:48:58 +0900 |
parents | 60c9769439b8 |
children | 7d135dc70f03 |
comparison
equal
deleted
inserted
replaced
84:f3e34b893a5f | 95:afa8332a0e37 |
---|---|
23 | 23 |
24 It is useful to point out ahead of time that this tutorial is really | 24 It is useful to point out ahead of time that this tutorial is really |
25 about teaching compiler techniques and LLVM specifically, *not* about | 25 about teaching compiler techniques and LLVM specifically, *not* about |
26 teaching modern and sane software engineering principles. In practice, | 26 teaching modern and sane software engineering principles. In practice, |
27 this means that we'll take a number of shortcuts to simplify the | 27 this means that we'll take a number of shortcuts to simplify the |
28 exposition. For example, the code leaks memory, uses global variables | 28 exposition. For example, the code uses global variables |
29 all over the place, doesn't use nice design patterns like | 29 all over the place, doesn't use nice design patterns like |
30 `visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but | 30 `visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but |
31 it is very simple. If you dig in and use the code as a basis for future | 31 it is very simple. If you dig in and use the code as a basis for future |
32 projects, fixing these deficiencies shouldn't be hard. | 32 projects, fixing these deficiencies shouldn't be hard. |
33 | 33 |
167 // of these for known things. | 167 // of these for known things. |
168 enum Token { | 168 enum Token { |
169 tok_eof = -1, | 169 tok_eof = -1, |
170 | 170 |
171 // commands | 171 // commands |
172 tok_def = -2, tok_extern = -3, | 172 tok_def = -2, |
173 tok_extern = -3, | |
173 | 174 |
174 // primary | 175 // primary |
175 tok_identifier = -4, tok_number = -5, | 176 tok_identifier = -4, |
177 tok_number = -5, | |
176 }; | 178 }; |
177 | 179 |
178 static std::string IdentifierStr; // Filled in if tok_identifier | 180 static std::string IdentifierStr; // Filled in if tok_identifier |
179 static double NumVal; // Filled in if tok_number | 181 static double NumVal; // Filled in if tok_number |
180 | 182 |
181 Each token returned by our lexer will either be one of the Token enum | 183 Each token returned by our lexer will either be one of the Token enum |
182 values or it will be an 'unknown' character like '+', which is returned | 184 values or it will be an 'unknown' character like '+', which is returned |
183 as its ASCII value. If the current token is an identifier, the | 185 as its ASCII value. If the current token is an identifier, the |
184 ``IdentifierStr`` global variable holds the name of the identifier. If | 186 ``IdentifierStr`` global variable holds the name of the identifier. If |
215 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* | 217 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]* |
216 IdentifierStr = LastChar; | 218 IdentifierStr = LastChar; |
217 while (isalnum((LastChar = getchar()))) | 219 while (isalnum((LastChar = getchar()))) |
218 IdentifierStr += LastChar; | 220 IdentifierStr += LastChar; |
219 | 221 |
220 if (IdentifierStr == "def") return tok_def; | 222 if (IdentifierStr == "def") |
221 if (IdentifierStr == "extern") return tok_extern; | 223 return tok_def; |
224 if (IdentifierStr == "extern") | |
225 return tok_extern; | |
222 return tok_identifier; | 226 return tok_identifier; |
223 } | 227 } |
224 | 228 |
225 Note that this code sets the '``IdentifierStr``' global whenever it | 229 Note that this code sets the '``IdentifierStr``' global whenever it |
226 lexes an identifier. Also, since language keywords are matched by the | 230 lexes an identifier. Also, since language keywords are matched by the |
248 | 252 |
249 .. code-block:: c++ | 253 .. code-block:: c++ |
250 | 254 |
251 if (LastChar == '#') { | 255 if (LastChar == '#') { |
252 // Comment until end of line. | 256 // Comment until end of line. |
253 do LastChar = getchar(); | 257 do |
258 LastChar = getchar(); | |
254 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); | 259 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r'); |
255 | 260 |
256 if (LastChar != EOF) | 261 if (LastChar != EOF) |
257 return gettok(); | 262 return gettok(); |
258 } | 263 } |