Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification on how eval is implemented #410

Open
jpellegrini opened this issue Jul 2, 2022 · 0 comments
Open

Clarification on how eval is implemented #410

jpellegrini opened this issue Jul 2, 2022 · 0 comments

Comments

@jpellegrini
Copy link
Contributor

jpellegrini commented Jul 2, 2022

@egallesio ...

I was reading STklos' eval:

(define (eval e :optional env)

  (define (parse-expression e)
    (compile e #f e #f)
    (emit 'END-OF-CODE)
    (assemble (reverse! *code-instr*)))

  (fluid-let ((*code-instr*     '())
              (*code-constants* '()))
    (let ((code (parse-expression e)))
      ;;(disassemble-code code (current-error-port))
      (%execute code (list->vector *code-constants*) (or env (current-module))))))

The line (compile e #f e #f) in the internal parse-expression means that when we eval some code, it is compiled without a module, and only executed in the given module?

So if I try, for example, to create a macro define or set! in a module in order to shadow the original define or the original set!, then if I eval some code in the module, it won't see my new definition, right?

stklos> (define-module x (define-macro (set! . args) (error "no set!")))
;; x
stklos> (select-module x)
x> (set! a 1)
**** Error:
error: no set!
stklos> (eval '(set! a 1) (find-module 'x))
**** Error:
%execute: variable `a' unbound
stklos> (eval '(set! car print) (find-module 'x))
stklos> (select-module x)
x> car
#[closure print]
x> (select-module stklos)
stklos> car
#[closure print]

Ok - and if I import SCHEME into the module x I get an error when I try to set car, because SCHEME is immutable.

I tried to change eval (naively -- I'm not sure what I'm doing) and it didn't work:

  (define (parse-expression e)
    (compile e (or env (current-module)) e #f)
    (emit 'END-OF-CODE)
    (assemble (reverse! *code-instr*)))

But this won't compile. (I also tried (or env #f)...)

I was looking into this because I have implemented SRFI-172 (which restricts the use of eval, define and some other things), and it seems to work fine if I enter the module and use it on the repl (my macros correctly shadow define, set!, %%set and so on)...

But if I use eval, then the STklos bindings are usable again, probably because the code is compiled in an empty environment, so my macros aren't visible and won't shadow define, set!, etc.

Although I ws looking into this because of SRFI 172, maybe it has to do with #368 also (by setting the value of an automatically imported symbol from the STklos module, I actually change its value in the STklos module!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant