carboncomputed
carboncomputed

Reputation: 1620

How to add to embedded list in scheme?

I'm trying to generate a symbol table in scheme and I'm stuck on the set-symbol function. The number corresponds to the block level of the code or "scope".

First symbol it reads in
((c class 0))
Next symbols 
(((c class 0) (a int 0) (b float 0)))
We read a bracket and read the next variables to a new scope.
(((a char 1) (d int 1)) ((c class 0) (a int 0) (b float 0)))
We leave that scope and "pop the stack".
(((c class 0) (a int 0) (b float 0)))

How do I always add to deepest list of the first list in scope?

Upvotes: 0

Views: 118

Answers (1)

GoZoner
GoZoner

Reputation: 70155

I suspect you'd be better off using a different representation. One of many, many would be:

(define (make-symbol-table parent alist)
  `(SYMBOL-TABLE ,parent ,@alist))
(define symbol-table-parent cadr)
(define symbol-table-alist  cddr)

(define (symbol-table-depth table)
  (let ((parent (symbol-table-parent table)))
    (if (not parent)
        1
        (+ 1 (symbol-table-depth parent))))

(define (symbol-table-lookup table name)
  (cond ((assoc name (symbol-table-alist table)) => cdr)
        (else (let ((parent (symbol-table-parent table)))
                (and parent (symbol-table-lookup parent name))))))

Upvotes: 1

Related Questions