| | 23 | Comments: |
| | 24 | * When multiple translation units are present, an AST is generated for each unit, and then the ASTs are merged (global scope variables are merged to one scope). |
| | 25 | * For any global scope variable with the same name that is declared in multiple files, all but at most one must be extern. |
| | 26 | * No extern variable declarations can have initializers. |
| | 27 | * For scoping issues with blocks, see sec 8.4 of C: A Reference Manual. |
| | 28 | * Rewrite blocks to have variables declared separately. Replace declarations that have initializations with assignment statements. |
| | 29 | Example: |
| | 30 | {{{ |
| | 31 | int x=1; |
| | 32 | { print x; |
| | 33 | int x=2; |
| | 34 | print x; |
| | 35 | } |
| | 36 | }}} |
| | 37 | In the AST, translate this to: |
| | 38 | * Root |
| | 39 | * Globals: x |
| | 40 | * Statements |
| | 41 | * Block |
| | 42 | * Decls |
| | 43 | * x |
| | 44 | * int |
| | 45 | * Stmt |
| | 46 | * print x |
| | 47 | * x = 2 |
| | 48 | * print x |