From: Duncan Mak <duncanmak@gmail.com>
To: kawa mailing list <kawa@sourceware.org>
Subject: Re: Analyzing Scheme source code
Date: Tue, 8 Dec 2020 21:32:00 -0500 [thread overview]
Message-ID: <CABgWrqqVRmv9Wq4FNDo6aUareX0-M_wLFPPJYNyazWYjRWMmxQ@mail.gmail.com> (raw)
In-Reply-To: <CABgWrqreJDWt1w3=RDUgw_Zadi9xYQJma+6fgDKVnCk65hgLzQ@mail.gmail.com>
I played around some more and this now prints out all the declarations in a
file:
(import (class gnu.expr Declaration Language ModuleExp ModuleManager
NameLookup)
(class gnu.kawa.io 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 <duncanmak@gmail.com> 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.
> https://www.gnu.org/software/kawa/internals/semantic-analysis.html
>
> 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 gnu.kawa.io 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.
>
--
Duncan.
next prev parent 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:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CABgWrqqVRmv9Wq4FNDo6aUareX0-M_wLFPPJYNyazWYjRWMmxQ@mail.gmail.com \
--to=duncanmak@gmail.com \
--cc=kawa@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* 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).