All posts

Adding symbol table

Posted On 05.20.2022

Not much for today, just adding some unit tests and implement a symbol table during the compilation process. The way it works is pretty much like the Constant Table that was mentioned yesterday.

If we have a let statement like in the following example, after the compilation process, the STORE_NAME opcode will be generated with the address of the a symbol.

(let a 10)
 
LOAD_CONST 1  ; Constant 10
STORE_NAME 1  ; Symbol a

The Constants table would look like this:

value index
50 1

And the Symbols table would look like this:

value index
a 1

For now, I’m reusing the ConstantTable class from yesterday, renamed it to LookupTable, so both Constants and Symbols table shared the same behavior: If a value is looked up and not found, it will automatically insert to the table and return the address. But maybe I’ll need to revise this, as it may not make any sense to load a symbol before it’s being created, like in this following code:

(let a (* 10 x))
 
LOAD_CONST 1  ; Constant 10
LOAD_NAME  1  ; Symbol x !! use before create
STORE_NAME 2  ; Symbol a

Still trying to find what is the best way to generate function calls. Once I implemented a proper function call, I can extend the language much further, for example, I’ve been thinking of adding string interpolation (or string template, in JS). But I’ll need to change both the parser and the compiler for this job. With function, we can do it easily with:

(print
    (concat "there are" count "bugs"))