diff options
| author | Tamas Hadhazy <helsgithub15@gmail.com> | 2024-09-14 21:49:25 +0100 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-09-14 15:25:02 -0700 |
| commit | b4f3ba2bf3565eebeee3190e827d5402d8f153fd (patch) | |
| tree | 1e5c6681028dc9756b18207156619505662a4e55 /chapter03/README.md | |
| parent | 4be43794169610d45e47468702c63cdfbc5deb5a (diff) | |
| download | Simple-b4f3ba2bf3565eebeee3190e827d5402d8f153fd.tar.gz Simple-b4f3ba2bf3565eebeee3190e827d5402d8f153fd.zip | |
extend explanation on scopes
Diffstat (limited to 'chapter03/README.md')
| -rw-r--r-- | chapter03/README.md | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/chapter03/README.md b/chapter03/README.md index cfb75d58..4f46f50e 100644 --- a/chapter03/README.md +++ b/chapter03/README.md @@ -127,6 +127,40 @@ Each table is annotated by its scope level, and shows the variables defined in t As we see, only 1 symbol table is present at this point. +We can also have scopes at the same level during the execution of the program +but only one will be alive at a time as the symbol table only capable of holding one symbol table for one scope level. + +For example: +``` +01 int a=1; +02 int b=2; +03 int c=0; +04 { +05 int b=3; +06 c=a+b; +08 } +09 { +10 int e = 3; +11 c = a+e; +13 } +14 return c; +``` +Here both scopes starting from `(04-08)` and `(09-13)` are at the same level: +```java +public final Stack<HashMap<String, Integer>> _scopes; +``` +The `_scopes` stack holds nested scopes, with index 0 representing the highest (global) +scope and deeper indices representing more nested scopes. +Popping returns the current scope and moves to the outer one. +There is a direct one-to-one mapping between each index and its corresponding scope. + +The first nested scope(04-08) + + +The second nested scope(09-13) + + + - In both cases the index which represents the deepness of the scope is 1. ## Enabling peephole optimizations Once we enable peephole optimizations, the parser is able to reduce all the expressions down to a |
