Skip to content

Latest commit

 

History

History
250 lines (231 loc) · 10.5 KB

memo.md

File metadata and controls

250 lines (231 loc) · 10.5 KB

env structure

env preserves global symbols and their bindings in Lisp world from GC.

      +--------+
      |        |
 next V        |
+----------+   |            +---------+
| Env *env |---+            |         |
+----------+            cdr V         |
      |               +-----------+   |
      +-------------->| TCELL:Obj |---+
                 vars +-----------+
                            |
                        car V (var)
                      +-----------+
            +---------| TCELL:Obj |-------+
            |         +-----------+       |
            |                             |
        car V (name)                  cdr V (val)
     +-------------+                 +---------+
     | TSYMBOL:Obj |                 |  *:Obj  |
     +-------------+                 +---------+

Like (defvar *env* (list (cons 'foo 1) (cons 'bar #\2) (cons 'baz "3"))) in Lisp.

root structure

root explicitly represents the stack-frame to preserve unbound intermediate objects in C world from GC.

  1. at the top of main()

           +--------------------+
           |       main()       |
           +--------------------+
           | [0] NULL           |
           | [1] &main          |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **sexp     ]
           | [3] (Obj *) 0      |<----[ Obj **expanded ]
           |- - - - - - - - - - |
           | [4] (Obj *) -1     |
           +--------------------+
    
  2. at the top of add_primitive() called from define_primitives()

           +--------------------+
     +---->|       main()       |
     |     +--------------------+
     |     | [0] NULL           |
     |     | [1] &main          |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sexp     ]
     |     | [3] (Obj *) 0      |<----[ Obj **expanded ]
     |     |- - - - - - - - - - |
     |     | [4] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |   add_primitive()  |
     |     +--------------------+
     +-----| [0] (Obj **) ?     |
           | [1] &add_primitive |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **prim     ]
           | [3] (Obj *) 0      |--+
           | [4] (Obj *) 0      |  | useless?
           | [5] (Obj *) 0      |--+
           |- - - - - - - - - - |
           | [6] (Obj *) -1     |
           +--------------------+
    
  3. in add_var() called from add_primitive()

           +--------------------+
     +---->|       main()       |
     |     +--------------------+
     |     | [0] NULL           |
     |     | [1] &main          |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sexp     ]
     |     | [3] (Obj *) 0      |<----[ Obj **expanded ]
     |     |- - - - - - - - - - |
     |     | [4] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |   add_primitive()  |<--------------------------+
     |     +--------------------+                           |
     +-----| [0] (Obj **) ?     |                           |
           | [1] &add_primitive |                           |
           |- - - - - - - - - - |                           |
           | [2] (Obj *) 0      |<----[ Obj **prim     ]    |
           | [3] (Obj *) 0      |--+                        |
           | [4] (Obj *) 0      |  | useless?               |
           | [5] (Obj *) 0      |--+                        |
           |- - - - - - - - - - |                           |
           | [6] (Obj *) -1     |                           |
           +--------------------+                           |
                                                            |
           +--------------------+                           |
           |      add_var()     |                           |
           +--------------------+                           |
           | [0] (Obj **) ?     |---------------------------+
           | [1] &add_var       |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **sym      ]
           | [3] (Obj *) 0      |<----[ Obj **cell     ]
           | [4] (Obj *) 0      |<----[ Obj **tmp      ] useless?
           |- - - - - - - - - - |
           | [5] (Obj *) -1     |
           +--------------------+
    
  4. in add_var_int() called from add_var()

           +--------------------+
     +---->|       main()       |
     |     +--------------------+
     |     | [0] NULL           |
     |     | [1] &main          |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sexp     ]
     |     | [3] (Obj *) 0      |<----[ Obj **expanded ]
     |     |- - - - - - - - - - |
     |     | [4] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |   add_primitive()  |<--------------------------+
     |     +--------------------+                           |
     +-----| [0] (Obj **) ?     |                           |
           | [1] &add_primitive |                           |
           |- - - - - - - - - - |                           |
           | [2] (Obj *) 0      |<----[ Obj **prim     ]    |
           | [3] (Obj *) 0      |--+                        |
           | [4] (Obj *) 0      |  | useless?               |
           | [5] (Obj *) 0      |--+                        |
           |- - - - - - - - - - |                           |
           | [6] (Obj *) -1     |                           |
           +--------------------+                           |
                                                            |
           +--------------------+                           |
     +---->|      add_var()     |                           |
     |     +--------------------+                           |
     |     | [0] (Obj **) ?     |---------------------------+
     |     | [1] &add_var       |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sym      ]
     |     | [3] (Obj *) 0      |<----[ Obj **cell     ]
     |     | [4] (Obj *) 0      |<----[ Obj **tmp      ] useless?
     |     |- - - - - - - - - - |
     |     | [5] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |    add_var_int()   |
     |     +--------------------+
     +-----| [0] (Obj **) ?     |
           | [1] &add_var_init  |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **cell     ]
           | [3] (Obj *) 0      |<----[ Obj **tmp      ]
           |- - - - - - - - - - |
           | [4] (Obj *) -1     |
           +--------------------+
    
  5. in add_var() after returning from add_var_int() call

           +--------------------+
     +---->|       main()       |
     |     +--------------------+
     |     | [0] NULL           |
     |     | [1] &main          |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sexp     ]
     |     | [3] (Obj *) 0      |<----[ Obj **expanded ]
     |     |- - - - - - - - - - |
     |     | [4] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |   add_primitive()  |<--------------------------+
     |     +--------------------+                           |
     +-----| [0] (Obj **) ?     |                           |
           | [1] &add_primitive |                           |
           |- - - - - - - - - - |                           |
           | [2] (Obj *) 0      |<----[ Obj **prim     ]    |
           | [3] (Obj *) 0      |--+                        |
           | [4] (Obj *) 0      |  | useless?               |
           | [5] (Obj *) 0      |--+                        |
           |- - - - - - - - - - |                           |
           | [6] (Obj *) -1     |                           |
           +--------------------+                           |
                                                            |
           +--------------------+                           |
           |      add_var()     |                           |
           +--------------------+                           |
           | [0] (Obj **) ?     |---------------------------+
           | [1] &add_var       |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **sym      ]
           | [3] (Obj *) 0      |<----[ Obj **cell     ]
           | [4] (Obj *) 0      |<----[ Obj **tmp      ] useless?
           |- - - - - - - - - - |
           | [5] (Obj *) -1     |
           +--------------------+
    
  6. in add_primitive() after returning from add_var() call

           +--------------------+
     +---->|       main()       |
     |     +--------------------+
     |     | [0] NULL           |
     |     | [1] &main          |
     |     |- - - - - - - - - - |
     |     | [2] (Obj *) 0      |<----[ Obj **sexp     ]
     |     | [3] (Obj *) 0      |<----[ Obj **expanded ]
     |     |- - - - - - - - - - |
     |     | [4] (Obj *) -1     |
     |     +--------------------+
     |
     |     +--------------------+
     |     |   add_primitive()  |
     |     +--------------------+
     +-----| [0] (Obj **) ?     |
           | [1] &add_primitive |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **prim     ]
           | [3] (Obj *) 0      |--+
           | [4] (Obj *) 0      |  | useless?
           | [5] (Obj *) 0      |--+
           |- - - - - - - - - - |
           | [6] (Obj *) -1     |
           +--------------------+
    
  7. in define_primitives() after returning from each add_primitive() call

           +--------------------+
           |       main()       |
           +--------------------+
           | [0] NULL           |
           | [1] &main          |
           |- - - - - - - - - - |
           | [2] (Obj *) 0      |<----[ Obj **sexp     ]
           | [3] (Obj *) 0      |<----[ Obj **expanded ]
           |- - - - - - - - - - |
           | [4] (Obj *) -1     |
           +--------------------+