Idorobots / spartan

A small Lisp dialect that serves me as a test bed for programming language features.
MIT License
13 stars 3 forks source link

CPC pass produces invalid code when encountering rebound variables. #143

Closed Idorobots closed 2 years ago

Idorobots commented 3 years ago

Compiling and running the following code yields 1474560 instead of 3628800. Changing the let bound n to n1 makes it compile properly.

(letrec ((fact (lambda (n)
                 (if (< n 2)
                     n
                     (* n
                        (let ((n (- n 1)))
                          (if (< n 2)
                              n
                              (* n (fact (- n 1))))))))))
  (if (< 10 2)
      10
      (* 10 (fact 9))))
Idorobots commented 3 years ago

Minimized reproduction:

(let ((n 23))
  (+ n (let ((n (- n 1)))
         n)))

Returns 44 instead of 45 due to invalid symbol propagation accross scopes:

(let ((n '23))
   (-
    n
    '1
    (lambda (value770)
      (let ((n value770)) (+ n n (lambda (value769) value769))))))
Idorobots commented 3 years ago

This is solvable with #48