• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. inScheme : get the variables defined a loaded file

Stats

  • Locked Locked
  • Replies 8
  • Subscribers 142
  • Views 2465
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

inScheme : get the variables defined a loaded file

kkdesbois
kkdesbois 7 months ago

Hello,

I read much the doc and the posts concenring the dynamic and lexical scoping but I probably missed something...

In Scheme I want to define a global function which loads a file in which several variables are defined and then used these variables in the function

Ex.

I've got a file myfile.txt :
A=1
B="toto"
C='(1 2 "test")

(inSkill
  (defun myfunSk ()
    (let ( A B C (myfile "./myfile.txt") )
      (load myfile)
      (printf "inSkill : \n")
      (println A)
      (println B)
      (println C)
    )
  )
(myfunSk)
)

(inScheme
  (defglobalfun myfunSch ()
    (let ( A B C (myfile "./myfile.txt") )
      (load myfile)
      (printf "inScheme : \n")
      (println A)
      (println B)
      (println C)
    )
  )
(myfunSch)
)
myfunSk returns
1
"toto"
(1 2 "test")
whereas myfunSch returns
nil
nil
nil
In a post, Aurel Buch wrote that the load function works in the dynamic scoping even if it's called from lexical scoping.
So I fought with inSKill, dynamicLet and other functions but didn't find a solution without passing the variables through a symbol.
I guess that there must be a simpler solution but I'm lost.

Laurent.

  • Cancel
  • Andrew Beckett
    Andrew Beckett 7 months ago

    Laurent,

    The documentation for load makes this clear:

    Opens a file, repeatedly calls lineread to read in the file,
    immediately evaluating each form after it is read in. Uses the
    file extension to determine the language mode (.il/.ile for SKILL
    and .ils/.ilse for SKILL++) for processing the language expressions
    contained in the file. By default, the loaded code is evaluated
    in dynamic scoping. However, if the extension is .ils/.ilse,
    lexical scoping is used. For a SKILL++ file, the loaded code
    is always evaluated in the top level environment.

    Since your extension is .txt, it will be loaded using SKILL semantics (dynamic scoping)

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett 7 months ago in reply to Andrew Beckett

    Trying the suggested workaround code instead:

    (inScheme
      (defglobalfun myfunSch2 ()
        (let ( A B C (myfile "./myfile.txt") port data)
          (setq port (infile myfile))
          (while (setq data (read port))
    	     (eval data (theEnvironment)))
          (close port)
          (printf "inScheme : \n")
          (println A)
          (println B)
          (println C)
        )
      )
    (myfunSch2)
    )
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett 7 months ago in reply to Andrew Beckett

    I was struggling to get it to post (I kept getting an error). Anyway, the workaround code uses read to read each form from the file, then eval with the lexical environment passed as the second (optional) argument. That way the evaluation is done in the lexical environment and the output from myfunSch2 is:

    inScheme :
    1
    "toto"
    (1 2 "test")

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • kkdesbois
    kkdesbois 7 months ago in reply to Andrew Beckett

    Hello Andrew,

    Indeed the documentation is clear. But as my file has not an .ils extension, I was stuck in the dynamic scoping and didn't find the way to retrieve my data in the lexical scoping.

    Thank you for the answer. It helps much.

    Laurent.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett 7 months ago in reply to kkdesbois

    Laurent,

    I also meant to point out that even if you had the .ils suffix, it still wouldn't have done as you expect - because the contents of the file being loaded would be treated as their own lexical scope (or rather evaluated in the top level lexical scope).

    Anyway, glad to have cleared things up.

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • kkdesbois
    kkdesbois 7 months ago in reply to Andrew Beckett

    Thanks for the information complement.

    Finally, I updated my code like that

    (inScheme
    (defun readinSkillFile (file @optional (env (theEnvironment)))
      "Retrieve the data from a file in the scoping defined in env"
      (let ( (port (infile file)) data)
        (while (setq data (read port)) (eval data env))
    ))

    (defglobalfun myfunSch ()
      (let ( A B C (myfile "./myfile.txt") )
        (readinSkillFile myfile (theEnvironment))
        (printf "inScheme : \n")
        (println A)
        (println B)
        (println C)
    ) )
    (myfunSch)
    )

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett 7 months ago in reply to kkdesbois

    Note that in this example, there's no difference between using defun and defglobalfun - both functions are in the top-level scope and so are global. This would only make a difference if they were in a lexical scope (e.g. both within the same let().

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • kkdesbois
    kkdesbois 7 months ago in reply to Andrew Beckett

    Thanks for the remark.

    In my "real" code, the two functions are in the same let.

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information