public inbox for
 help / color / mirror / Atom feed
From: Duncan Mak <>
To: kawa mailing list <>
Subject: Re: Analyzing Scheme source code
Date: Tue, 8 Dec 2020 21:32:00 -0500	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

I played around some more and this now prints out all the declarations in a

(import (class gnu.expr Declaration Language ModuleExp ModuleManager
        (class InPort)
        (class gnu.text Lexer SourceMessages)
        (class kawa.lang Translator))

(define (print-decls filename)
  (let* ((language   (Language:getDefaultLanguage))
         (port       (InPort:openFile filename))
         (lexer      (language:getLexer port (SourceMessages)))
         (manager    (ModuleManager:getInstance))
         (minfo      (manager:findWithSourcePath port:name))
         (translator (language:parse lexer Language:PARSE_IMMEDIATE minfo))
         (module ::ModuleExp (translator:currentModule)))
    (let loop ((decl ::Declaration (module:firstDecl)))
      (unless (eq? #!null decl)
        (format #t "~A~%" decl)
        (loop (decl:nextDecl))))))

On Tue, Dec 8, 2020 at 6:49 PM Duncan Mak <> wrote:

> Hello all,
> I'm interested in running an analysis of some Scheme source code,
> specifically, I'm looking to find what's defined in each file and what
> references each file takes on.
> I started writing my own analyzer with the match macro, and it looks
> something like this:
> (define (process-form form)
>   (match form
>     (['define [name @args] @body]
>      (cons name (map process-form body)))
>     (['define name value]
>      value)
>     (['let [[foo bar] ...] @body]
>      (map process-form body))
>     (['if test then else]
>      (list (process-form test)
>            (process-form then)
>            (process-form else)))
>     ([procedure @args]
>      (cons procedure (map process-form args)))))
> Thinking a bit more, rather than doing it myself, I thought maybe I could
> reuse the existing machinery that's in Kawa already, i.e.
> I think the trick is to get an instance of a Translator for a particular
> file, and then call `rewrite` and possibly inspect the resulting Expression
> (which ought to be an instance of a ModuleExp?)
> (import (class gnu.expr Language NameLookup)
>         (class InPort)
>         (class gnu.text Lexer)
>         (class kawa.lang Translator))
> (define (process-file filename)
>   (let ((lang (Language:getDefaultLanguage))
>         (lexer (language:getLexer (InPort:openFile filename)
> (SourceMessages))))
>     (Translator lang lexer:messages  (NameLookup lang))))
> What I have above seems to only result in an empty Translator.
> What's the right way to set up the environment?
> --
> Duncan.


  reply	other threads:[~2020-12-09  2:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-08 23:49 Duncan Mak
2020-12-09  2:32 ` Duncan Mak [this message]
2020-12-09  3:00   ` Duncan Mak
2020-12-09  3:19   ` Per Bothner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).