Interactive Executive
parent
ec9954a00a
commit
f516958dcf
|
@ -0,0 +1,73 @@
|
|||
|
||||
USING: kernel arrays strings sequences sequences.deep peg peg.ebnf ;
|
||||
|
||||
IN: shell.parser
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
TUPLE: incantation command stdin stdout background ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
TUPLE: single-quoted-expr expr ;
|
||||
TUPLE: double-quoted-expr expr ;
|
||||
TUPLE: back-quoted-expr expr ;
|
||||
TUPLE: glob-expr expr ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: <single-quoted-expr> single-quoted-expr boa ;
|
||||
: <double-quoted-expr> double-quoted-expr boa ;
|
||||
: <back-quoted-expr> back-quoted-expr boa ;
|
||||
: <glob-expr> glob-expr boa ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
EBNF: expr
|
||||
|
||||
space = " "
|
||||
|
||||
tab = "\t"
|
||||
|
||||
white = (space | tab)
|
||||
|
||||
whitespace = (white)* => [[ drop ignore ]]
|
||||
|
||||
squote = "'"
|
||||
|
||||
single-quoted = squote (!(squote) .)* squote => [[ second >string <single-quoted-expr> ]]
|
||||
|
||||
dquote = '"'
|
||||
|
||||
double-quoted = dquote (!(dquote) .)* dquote => [[ second >string <double-quoted-expr> ]]
|
||||
|
||||
bquote = "`"
|
||||
|
||||
back-quoted = bquote (!(bquote) .)* bquote => [[ second >string <back-quoted-expr> ]]
|
||||
|
||||
glob-char = ("*" | "?")
|
||||
|
||||
non-glob-char = !(glob-char | white) .
|
||||
|
||||
glob-beginning-string = (non-glob-char)* [[ >string ]]
|
||||
|
||||
glob-rest-string = (non-glob-char)+ [[ >string ]]
|
||||
|
||||
glob = glob-beginning-string glob-char (glob-rest-string | glob-char)* => [[ flatten concat <glob-expr> ]]
|
||||
|
||||
other = (!(white | "&" | ">" | ">>" | "<") .)+ => [[ >string ]]
|
||||
|
||||
element = (single-quoted | double-quoted | back-quoted | glob | other)
|
||||
|
||||
to-file = ">" whitespace other => [[ second ]]
|
||||
|
||||
in-file = "<" whitespace other => [[ second ]]
|
||||
|
||||
ap-file = ">>" whitespace other => [[ second ]]
|
||||
|
||||
redirection = (in-file)? whitespace (to-file | ap-file)?
|
||||
|
||||
line = (element whitespace)+ (in-file)? whitespace (to-file | ap-file)? whitespace ("&")? => [[ first4 incantation boa ]]
|
||||
|
||||
;EBNF
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
|
||||
USING: kernel words continuations namespaces debugger sequences combinators
|
||||
io io.files io.launcher
|
||||
accessors multi-methods newfx shell.parser ;
|
||||
|
||||
IN: shell
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: cd ( args -- )
|
||||
dup empty?
|
||||
[ drop home set-current-directory ]
|
||||
[ first set-current-directory ]
|
||||
if ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: pwd ( args -- )
|
||||
drop
|
||||
current-directory get
|
||||
print ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: swords ( -- seq ) { "cd" "pwd" } ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
GENERIC: expand ( expr -- expr )
|
||||
|
||||
METHOD: expand { single-quoted-expr } expr>> ;
|
||||
|
||||
METHOD: expand { double-quoted-expr } expr>> ;
|
||||
|
||||
METHOD: expand { object } ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: expansion ( command -- command ) [ expand ] map ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: run-incantation ( incantation -- )
|
||||
<process>
|
||||
over command>> expansion >>command
|
||||
over stdin>> >>stdin
|
||||
over stdout>> >>stdout
|
||||
swap background>>
|
||||
[ run-detached drop ]
|
||||
[ [ try-process ] [ print-error drop ] recover ]
|
||||
if ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: chant ( incantation -- )
|
||||
dup command>> first swords member-of?
|
||||
[ command>> unclip "shell" lookup execute ]
|
||||
[ run-incantation ]
|
||||
if ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: prompt ( -- )
|
||||
current-directory get write
|
||||
" $ " write
|
||||
flush ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: shell ( -- )
|
||||
prompt
|
||||
readln
|
||||
{
|
||||
{ [ dup f = ] [ drop ] }
|
||||
{ [ dup "exit" = ] [ drop ] }
|
||||
{ [ dup "" = ] [ drop shell ] }
|
||||
{ [ dup expr ] [ expr ast>> chant shell ] }
|
||||
{ [ t ] [ drop shell ] }
|
||||
}
|
||||
cond ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
: ix ( -- ) shell ;
|
||||
|
||||
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
MAIN: ix
|
Loading…
Reference in New Issue