This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Shell-style programming in Kawa
- From: Per Bothner <per at bothner dot com>
- To: Jamison Hope <jrh at theptrgroup dot com>, "kawa at sourceware dot org list" <kawa at sourceware dot org>
- Date: Fri, 07 Feb 2014 10:38:12 -0800
- Subject: Re: Shell-style programming in Kawa
- Authentication-results: sourceware.org; auth=none
- References: <52C4B518 dot 5080000 at bothner dot com> <A72FB562-9459-4C69-B346-A6C94DBD22C4 at theptrgroup dot com> <52E3772D dot 50804 at bothner dot com> <4A37A2F8-76B0-421D-AFCB-62AA1EE78468 at theptrgroup dot com> <9CF037C1-15CA-48B9-80E5-83EDF306017F at theptrgroup dot com>
This message is about setting/getting the current path.
On 01/27/2014 05:06 PM, Jamison Hope wrote:
Similarly, a Scheme interface to the Path.currentPath stuff, like
(chdir "..") => Path.setCurrentPath(Path.currentPath().resolve("..").getCanonical());
(cwd) => Path.currentPath();
could be nice. Scsh has them, along with a function version of with-cwd that
takes a thunk as its second argument.
I implemented (current-path) asa parameter.
Thus instead of cwd you have currentpath; instead of (chdir "..")
you can use a setter: (set! (current-path) "..") or
(current-path "..").
(define-syntax with-cwd
(syntax-rules ()
((with-cwd wd e ...)
(let ((old-path (cwd)))
(try-finally
(begin (chdir wd) e ...)
(chdir old-path))))))
(define (with-cwd* wd thunk)
(let ((old-path (cwd)))
(try-finally
(begin (chdir wd) (thunk))
(chdir old-path))))
We could add these as syntactic sugar, but since
parameterize now works I think that is good enough:
(parameterize ((current-path (path "..")))
&sh{echo pwd is `pwd`})
I'm still pondering whether the initial (current-path) - i.e. the
value of Path.useDirPath should be "." or the expanded user.dir.
Certainly it seems weird and uninformative for (current-path) to
just print ".". But perhaps that would be ok if we added a
(path-absolute PATH) function.
The resolve-uri call in chdir depends upon the patch to Path.java I just sent.
Without that, this works instead: (resolve-uri (path (->String dir)) (cwd)).
I tried just capitalizing the s in string, not wrapping it in (path ...),
but that gives me a spurious warning:
warning - no known slot 'getCanonical' in java.lang.Object
It seems like the ->string version ends up calling Path#resolve(Path),
while the ->String version (without the wrap) calls Path#resolve(String),
which is abstract. Does the compiler ignore the declared return type
of an abstract method when doing its type inferencing?
I believe so. Perhaps we need to do more "target typing" in the Java 8
world.
[Would the compiler be able to inline away the lambda if with-cwd were defined
as
(define-syntax with-cwd
(syntax-rules ()
((with-cwd wd e ...) (with-cwd* wd (lambda () e ...))))))
or does that require a corresponding Procedure.validateApplyKey property on
with-cwd*?
The latter. There is currently no mechanism to automatically inline
previously-compiled functions. It would certainly be handy.
Can those be implemented in/for functions written in Scheme?]
Yes. See with-exception-handler for an example where the hook is
done in Scheme. Though the actual validateApplyWithExceptionHandler
method is written in Java, I can't think of a reason it couldn't
be written in Scheme. It would help to have a small library of
helper functions - and macros - for munging Expression trees.
--
--Per Bothner
per@bothner.com http://per.bothner.com/