From b4f3ba2bf3565eebeee3190e827d5402d8f153fd Mon Sep 17 00:00:00 2001 From: Tamas Hadhazy Date: Sat, 14 Sep 2024 21:49:25 +0100 Subject: extend explanation on scopes --- chapter03/README.md | 34 ++++++++++++ chapter03/docs/03-graph4.svg | 127 +++++++++++++++++++++++++++++++++++++++++++ chapter03/docs/03-graph5.svg | 127 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 chapter03/docs/03-graph4.svg create mode 100644 chapter03/docs/03-graph5.svg 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> _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) +![Graph4](./docs/03-graph4.svg) + +The second nested scope(09-13) +![Graph5](./docs/03-graph5.svg) + + - 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 diff --git a/chapter03/docs/03-graph4.svg b/chapter03/docs/03-graph4.svg new file mode 100644 index 00000000..23b48069 --- /dev/null +++ b/chapter03/docs/03-graph4.svg @@ -0,0 +1,127 @@ + + + + + + +chapter03 + + +cluster_Nodes + + + +cluster_Scope1_0 + + + +cluster_Scope1_1 + + + + +Start2 + +Start + + + +Con_3 + +#1 + + + +Con_3->Start2 + + +0 + + + +Con_4 + +#2 + + + +Con_4->Start2 + + +0 + + + +Con_6 + +#3 + + + +Con_6->Start2 + + +0 + + + +Con_8 + +#4 + + + +Con_8->Start2 + + +0 + + + +Scope1_0 + + +0 + +a + +b + +c + + + +Scope1_0:Scope1_0_a->Con_3 + + + + + +Scope1_0:Scope1_0_b->Con_4 + + + + + +Scope1_0:Scope1_0_c->Con_8 + + + + + +Scope1_1 + + +1 + +b + + + +Scope1_1:Scope1_1_b->Con_6 + + + + + \ No newline at end of file diff --git a/chapter03/docs/03-graph5.svg b/chapter03/docs/03-graph5.svg new file mode 100644 index 00000000..346b08f2 --- /dev/null +++ b/chapter03/docs/03-graph5.svg @@ -0,0 +1,127 @@ + + + + + + +chapter03 + + +cluster_Nodes + + + +cluster_Scope1_0 + + + +cluster_Scope1_1 + + + + +Start2 + +Start + + + +Con_3 + +#1 + + + +Con_3->Start2 + + +0 + + + +Con_4 + +#2 + + + +Con_4->Start2 + + +0 + + + +Con_9 + +#3 + + + +Con_9->Start2 + + +0 + + + +Con_11 + +#4 + + + +Con_11->Start2 + + +0 + + + +Scope1_0 + + +0 + +a + +b + +c + + + +Scope1_0:Scope1_0_a->Con_3 + + + + + +Scope1_0:Scope1_0_b->Con_4 + + + + + +Scope1_0:Scope1_0_c->Con_11 + + + + + +Scope1_1 + + +1 + +e + + + +Scope1_1:Scope1_1_e->Con_9 + + + + + \ No newline at end of file -- cgit v1.2.3