From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 8CC803835098 for ; Wed, 9 Dec 2020 03:00:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8CC803835098 Received: by mail-lj1-x22d.google.com with SMTP id f11so29251ljm.8 for ; Tue, 08 Dec 2020 19:00:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=DuodZkKY5ZIrjFCsFsathm4H4WKWFrsnrL3crkfSF28=; b=SpNNtStvqbrEGTGtrwNXXfnPkoAWOQx9hiCaNCnsczPyfKmy/L9JaC8vVXp+fzVxl2 DSHu+KmeSfF7nG3lXYTFATZFHIRkTWDrhyBng9cTWI8jVoVqJSfY9mlTdf/BY7OS9wSJ F5qoZTJHTYY7YLLJlVhIdMh9xD5578a4FTOVR7JfFzzxbLK5LDGoi3qNgLWX5KjsIi4+ 9BfsratXeFTrPC0tMbWYfCPXu6iVLXg++9fFqvlMk7pIPnUCJnILanD/kNB5hv6UsRPx nWgDBuCjkGpqhkqOeSDLIj9SiQE9bYcLoBkC4qgnzqJySzyTLz+CPlQyzXYFV3rLrQBY unOw== X-Gm-Message-State: AOAM531Cdksrqxnq8HED98NeYsSdjksNQCgN4eNucNMLxMXPxs04PBhc 3JT2zWUf3KV4ad4r7axlWNifxqypnseIPYCOTxy9azUJGFU= X-Google-Smtp-Source: ABdhPJx5t11Ol+WBpullKHTsvqr/faRFhezopyUagpFGHi4gm1G4G/nFFW5TnEGLpVKW4NSc69iozcYmDKU+2sj590k= X-Received: by 2002:a2e:b1c8:: with SMTP id e8mr122545lja.198.1607482837697; Tue, 08 Dec 2020 19:00:37 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Duncan Mak Date: Tue, 8 Dec 2020 22:00:01 -0500 Message-ID: Subject: Re: Analyzing Scheme source code To: kawa mailing list X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: kawa@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Kawa mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Dec 2020 03:00:41 -0000 Using Declarations got me somewhere, but really I'm interested in all the top-level forms, and not just (define x ...). For example, it'd be useful to see (define-record-type ...) forms too, right now, I don't know how to get at them. Another interesting question is macros that I define myself, I guess somehow I'll have to tell the Language instance about my macros. Also, I can't quite figure out how to open up the inside of a Declaration either -- I'm looking for a getBody() method that might give me an Expression[], but I haven't been able to find any methods of that sort. Duncan. On Tue, Dec 8, 2020 at 9:32 PM Duncan Mak wrote: > 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 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. > -- Duncan.