|
1 | 1 | We've already seen how variable definitions are parsed in our interpreter. Now
|
2 |
| -it's time for function definitions. (note it is definition, not declaration, |
3 |
| -thus our interpreter don't support recursion across functions.) |
| 2 | +it's time for function definitions (note it is definition, not declaration, |
| 3 | +thus our interpreter doesn't support recursion across functions). |
4 | 4 |
|
5 | 5 | ## EBNF Grammar
|
6 | 6 |
|
7 |
| -Let's start be refreshing our memories about EBNF grammar introduced in last |
| 7 | +Let's start by refreshing our memory of the EBNF grammar introduced in last |
8 | 8 | chapter, we've already implement `program`, `global_declaration` and
|
9 | 9 | `enum_decl`. We'll deal with part of `variable_decl`, `function_decl`,
|
10 |
| -`parameter_decl` and `body_decl`. The rest will be covered in next chapter. |
| 10 | +`parameter_decl` and `body_decl`. The rest will be covered in the next chapter. |
11 | 11 |
|
12 | 12 | ```
|
13 | 13 | variable_decl ::= type {'*'} id { ',' {'*'} id } ';'
|
@@ -43,7 +43,7 @@ if (token == '(') {
|
43 | 43 | ...
|
44 | 44 | ```
|
45 | 45 |
|
46 |
| -The type for current identifier (i.e. function name) had already been set |
| 46 | +The type for the current identifier (i.e. function name) had already been set |
47 | 47 | correctly. The above chunk of code set the type (i.e. `Fun`) and the
|
48 | 48 | address in `text segment` for the function. Here comes `parameter_decl` and
|
49 | 49 | `body_decl`.
|
@@ -84,10 +84,10 @@ following (please refer to the VM of chapter 2):
|
84 | 84 | ```
|
85 | 85 |
|
86 | 86 | The key point here is no matter if it is a parameter (e.g. `param_a`) or local
|
87 |
| -variable (e.g. `local_1`), they are all store on **stack**. Thus they are |
88 |
| -referred to by pointer `new_bp` and relative shfitment, while global variables |
| 87 | +variable (e.g. `local_1`), they are all stored on the **stack**. Thus they are |
| 88 | +referred to by the pointer `new_bp` and relative offsets, while global variables |
89 | 89 | which are stored in `text segment` are refered to by direct address. So we
|
90 |
| -need to know the number of parameters and the shiftment of each. |
| 90 | +need to know the number of parameters and the offset of each. |
91 | 91 |
|
92 | 92 | ## Skeleton for Parsing Function
|
93 | 93 |
|
@@ -118,7 +118,7 @@ void function_declaration() {
|
118 | 118 | ```
|
119 | 119 |
|
120 | 120 | Note that we are supposed to consume the last `}` character in ①. But we don't
|
121 |
| -because `variable_decl` and `function_decl` are parsed together(because of the |
| 121 | +because `variable_decl` and `function_decl` are parsed together (because of the |
122 | 122 | same prefix in EBNF grammar) inside `global_declaration`. `variable_decl` ends
|
123 | 123 | with `;` while `function_decl` ends with `}`. If `}` is consumed, the `while`
|
124 | 124 | loop in `global_declaration` won't be able to know that a `function_decl`
|
@@ -296,7 +296,7 @@ The code of this chapter isn't long, most of the part are used to parse
|
296 | 296 | variables and much of them are duplicated. The parsing for parameter and local
|
297 | 297 | variables are almost the same, but the stored information are different.
|
298 | 298 |
|
299 |
| -Of course, you may want to review the VM chapter(chapter 2) to get better |
| 299 | +Of course, you may want to review the VM chapter (chapter 2) to get better |
300 | 300 | understanding of the expected output for function, so as to understand why
|
301 | 301 | would we want to gather such information. This is what we called
|
302 | 302 | "domain knowledge".
|
|
0 commit comments