shriram / smol

The SMoL (Standard Model of Languages) Family of Languages
15 stars 3 forks source link

Change #lang dyn-scope-is-bad to match 2021Fall textbook #14

Closed LuKuangChen closed 2 years ago

LuKuangChen commented 2 years ago

Fix #12

LuKuangChen commented 2 years ago

BTW, I spent 2h trying to implement this with (current-namespace) and eval. Eventually, I realized

sorawee commented 2 years ago

In Emacs Lisp:

(let ((x 1))
  (+ (let ((f (lambda () x)))
       (let ((x 2) (y 3))
         (funcall f)))))

evals to 2. #lang's equivalent program:

(let ((x 1))
  (+ (let ((f (lambda () x)))
       (let ((x 2) (y 3))
         (f)))))

also evaluates to 2.

However, in Emacs Lisp:

(let ((x 1))
  (+ (let ((f (lambda () x)))
       (let ((x 2) (y 3))
         (funcall f)))
     y))

fails with the error unbound id y.

With this branch, I think the following program will return 5?

(let ((x 1))
  (+ (let ((f (lambda () x)))
       (let ((x 2) (y 3))
         (f)))
     y))
LuKuangChen commented 2 years ago

You are right! Thanks for catching that. The new commit re-implements let with left-left-lambda and should fix the problem.

sorawee commented 2 years ago

If I understand correctly, the problem is not even about the error type, but about when the error occurs: a syntax error occurs at compile-time, but test/exn is meant to catch run-time error.

If that is truly the case, you could use convert-compile-time-error or convert-syntax-error to turn a compile-time error into a run-time error.

LuKuangChen commented 2 years ago

Although the error being raised is a syntax error, I think the error is raised at runtime:

#lang racket
(require "semantics.rkt")

(deffun (f)
  an-unbound-id)

;; You don't see an error unless you uncomment the application.
;(f)

Besides, the documentation of test/exn says that it only catches errors raised by PLAI's error function.

sorawee commented 2 years ago

Ah, right!

LuKuangChen commented 2 years ago

Thanks for asking!

LuKuangChen commented 2 years ago

This PR is now ready to merge. It makes the following changes in addition to what #12 requires: