why Unix?
Jarrett Billingsley
jarrett.billingsley at gmail.com
Tue Apr 7 15:17:12 PDT 2009
On Tue, Apr 7, 2009 at 3:30 PM, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
>> And regarding using it as an user shell: there's an interactive command
>> line interpreter.
>
> if you type ls, does it work, or do you have to type __run__("ls") or
> whatever?
I think it's things like this that are the reason a lot of attempts at
turning general-purpose scripting languages into shells has failed.
Despite the horrid mess that is bash's syntax, the syntax for simple
commands is.. simple. Elegant, to a point.
So what I've been considering is creating a modified version of MiniD.
All that would be required is some modifications to the syntactic
analyzer and a small library of functionality for very common shell
tasks. The important part is the modifications to the syntactic
analyzer.
So suppose you started up this minishell or mdsh or whatever it's called.
$ mdsh
> _
OK, go ahead and type some stuff.
> ls
foo.d bar.d baz.d
> ls -a
. .. foo.d bar.d baz.d
> grep "hello" | sort
a: hello!
b: hello!
> _
Uh, ok, so it looks just like bash. What's happening under the hood
is that when in interactive mode, the compiler is in "bash mode." It
parses each line as a bash statement. But it's not actually bash: all
it's doing is parsing the line and turning it into some kind of MiniD
code under the hood. so `grep "hello" | sort` becomes instead
"shell.exec("grep", "hello").pipe("sort")", or something like that.
Then that is compiled into MiniD code and executed. The shell.exec
function just spawns a new process and returns some process object
which can be piped to another one.
Then you have a simple "escaping" mechanism to switch between MiniD
mode and bash mode. Something like %.
> %foreach(file; shell.ls())
.. {
.. writeln("processing file ", file)
.. %something $file
.. }
processing file foo.d
processing file bar.d
processing file baz.d
> _
There. Now you have sane syntax for complex language constructs, but
it's still simple to escape into bash mode to do shell-y stuff.
Notice the "$file", which is a signal to the compiler to convert
'file' as a variable reference rather than as a string argument to the
command.
(It also follows that if you wrote a full shellscript, in a .mdsh file
or something, the compiler would, when compiling it, start in MiniD
mode by default so you wouldn't have to escape every statement with a
%.)
I know Alex wants to see this happen :P
More information about the Digitalmars-d
mailing list