Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

05-09-2010 /prog/ Challenge

Name: Anonymous 2010-09-06 0:06

Write a procedure find-variable that takes as arguments a variable and a compile-time environment and returns the lexical address of the variable with mad respect to that environment.

Name: Anonymous 2010-09-06 1:06

The problem with this challenge is that the environment's structure is different for each implementation, on CLISP it's a dumb (recurive) vector(for easier acess from C), on SBCL it's a fancy structure, on CCL it's a structure as well (however, structures there are most certainly always implemented with vectors), and so on. Since I don't know the exact meaning of "lexical address of the variable with mad respect to that environment", I'll just show how I located a variable in 3 minutes within the environment object without much prior knowledge of the compiler internals, and instead just using my instincts and general lisp knowledge to inspect compiler(SBCL) internals:

CL-USER> (defmacro get-environment (&environment env) env)
GET-ENVIRONMENT
CL-USER> (get-environment)
#<NULL-LEXENV>
CL-USER> (let ((x 1)) (get-environment))
; in: LAMBDA NIL
;     (LET ((X 1))
;       (GET-ENVIRONMENT))
;
; caught STYLE-WARNING:
;   The variable X is defined but never used.
;
; compilation unit finished
;   caught 1 STYLE-WARNING condition
#S(SB-KERNEL:LEXENV
   :FUNS NIL
   :VARS ((X . #<SB-C::LAMBDA-VAR :%SOURCE-NAME X {24626819}>))
   :BLOCKS NIL
   :TAGS NIL
   :TYPE-RESTRICTIONS NIL
   :LAMBDA #<SB-C::CLAMBDA
             :%SOURCE-NAME SB-C::.ANONYMOUS.
             :%DEBUG-NAME (LET ((X 1))
                            )
             :KIND :ZOMBIE
             :TYPE #<SB-KERNEL:BUILT-IN-CLASSOID FUNCTION (read-only)>
             :WHERE-FROM :DEFINED
             :VARS (X) {24626949}>
   :CLEANUP NIL
   :HANDLED-CONDITIONS ((COMPILER-NOTE . MUFFLE-WARNING))
   :DISABLED-PACKAGE-LOCKS NIL
   :%POLICY ((COMPILATION-SPEED . 1) (DEBUG . 1) (INHIBIT-WARNINGS . 1)
             (SAFETY . 1) (SPACE . 1) (SPEED . 1))
   :USER-DATA NIL)
CL-USER> (sb-c::lexenv-vars *)
((X . #<SB-C::LAMBDA-VAR :%SOURCE-NAME X {24626819}>))
CL-USER> (assoc 'x *)
(X . #<SB-C::LAMBDA-VAR :%SOURCE-NAME X {24626819}>)
CL-USER> (cdr *)
#<SB-C::LAMBDA-VAR :%SOURCE-NAME X {24626819}>
CL-USER> (describe *)
#<SB-C::LAMBDA-VAR :%SOURCE-NAME X {24626819}>
  [structure-object]

Slots with :INSTANCE allocation:
  NUMBER             = 4
  NUMBER             = 4
  %SOURCE-NAME       = X
  TYPE               = #<SB-KERNEL:NAMED-TYPE T>
  DEFINED-TYPE       = #<SB-KERNEL:NAMED-TYPE T>
  WHERE-FROM         = :ASSUMED
  REFS               = NIL
  EVER-USED          = T
  DYNAMIC-EXTENT     = NIL
  INFO               = NIL
  SETS               = NIL
  FLAGS              = 0
  HOME               = #<SB-C::CLAMBDA..
  ARG-INFO           = NIL
  SPECVAR            = NIL
  CONSTRAINTS        = NIL
  LAST-INITIAL-TYPE  = #<SB-KERNEL:NAMED-TYPE T>
  FOP-VALUE          = NIL
; No value
CL-USER>

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List