diff --git a/extra/shell/parser/parser.factor b/extra/shell/parser/parser.factor new file mode 100644 index 0000000000..846ff9cbf1 --- /dev/null +++ b/extra/shell/parser/parser.factor @@ -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 boa ; +: double-quoted-expr boa ; +: back-quoted-expr boa ; +: glob-expr boa ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +EBNF: expr + +space = " " + +tab = "\t" + +white = (space | tab) + +whitespace = (white)* => [[ drop ignore ]] + +squote = "'" + +single-quoted = squote (!(squote) .)* squote => [[ second >string ]] + +dquote = '"' + +double-quoted = dquote (!(dquote) .)* dquote => [[ second >string ]] + +bquote = "`" + +back-quoted = bquote (!(bquote) .)* bquote => [[ second >string ]] + +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 ]] + +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 + diff --git a/extra/shell/shell.factor b/extra/shell/shell.factor new file mode 100644 index 0000000000..25ac09cbba --- /dev/null +++ b/extra/shell/shell.factor @@ -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 -- ) + + 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 \ No newline at end of file