Add the dot and eval built-ins

* geesh/built-ins/utils.scm (get-evaluator): New public function.
* geesh/built-ins/dot.scm: New file.
* geesh/built-ins/eval.scm: New file.
* geesh/Makefile.am: Add them.
* geesh/built-ins.scm (*special-built-ins*): Add dot and eval.
This commit is contained in:
Timothy Sample 2018-11-25 22:42:05 -05:00
parent 6a68c73a7b
commit c1f8a870ed
5 changed files with 103 additions and 3 deletions

View File

@ -45,7 +45,9 @@ MODULES = \
geesh/built-ins/cd.scm \
geesh/built-ins/colon.scm \
geesh/built-ins/continue.scm \
geesh/built-ins/dot.scm \
geesh/built-ins/echo.scm \
geesh/built-ins/eval.scm \
geesh/built-ins/export.scm \
geesh/built-ins/false.scm \
geesh/built-ins/pwd.scm \

View File

@ -32,11 +32,11 @@
;; Special built-ins take precedence over any other command.
(define *special-built-ins*
`(("." . ,undefined)
`(("." . ,(@@ (geesh built-ins dot) main))
(":" . ,(@@ (geesh built-ins colon) main))
("break" . ,(@@ (geesh built-ins break) main))
("continue" . ,(@@ (geesh built-ins continue) main))
("eval" . ,undefined)
("eval" . ,(@@ (geesh built-ins eval) main))
("exec" . ,undefined)
("exit" . ,undefined)
("export" . ,(@@ (geesh built-ins export) main))

53
geesh/built-ins/dot.scm Normal file
View File

@ -0,0 +1,53 @@
;;; The Geesh Shell Interpreter
;;; Copyright 2018 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Geesh.
;;;
;;; Geesh is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Geesh is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Geesh. If not, see <http://www.gnu.org/licenses/>.
(define-module (geesh built-ins dot)
#:use-module (geesh built-ins utils)
#:use-module (geesh environment)
#:use-module (geesh parser)
#:use-module (ice-9 match))
;;; Commentary:
;;;
;;; The 'dot' utility.
;;;
;;; Code:
(define (main . args)
(match args
((file)
(catch 'system-error
(lambda ()
(call-with-input-file file
(lambda (port)
(set-status! 0)
(let loop ()
(match (read-sh port)
((? eof-object?) (get-status))
(exp ((get-evaluator) exp)
(loop)))))))
(lambda args
(format (current-error-port)
"~a: .: ~a: ~a.~%"
(car (program-arguments)) file
(strerror (system-error-errno args)))
EXIT_FAILURE)))
(_ (format (current-error-port)
"~a: .: Invalid options ~s.~%"
(car (program-arguments)) args)
EXIT_FAILURE)))

38
geesh/built-ins/eval.scm Normal file
View File

@ -0,0 +1,38 @@
;;; The Geesh Shell Interpreter
;;; Copyright 2018 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Geesh.
;;;
;;; Geesh is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Geesh is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Geesh. If not, see <http://www.gnu.org/licenses/>.
(define-module (geesh built-ins eval)
#:use-module (geesh built-ins utils)
#:use-module (geesh environment)
#:use-module (geesh parser)
#:use-module (ice-9 match))
;;; Commentary:
;;;
;;; The 'eval' utility.
;;;
;;; Code:
(define (main . args)
(match (string-trim-both (string-join args " ") #\space)
("" 0)
(code
(call-with-input-string code
(lambda (port)
((get-evaluator) `(<sh-begin> ,@(read-sh-all port)))))
(get-status))))

View File

@ -18,7 +18,8 @@
(define-module (geesh built-ins utils)
#:use-module (ice-9 match)
#:export (split-assignment))
#:export (get-evaluator
split-assignment))
;;; Commentary:
;;;
@ -26,6 +27,12 @@
;;;
;;; Code:
;; We need to be able to run Shell code from the 'dot' and 'eval'
;; built-ins. This is a bit of trickery to avoid a module dependency
;; loop.
(define (get-evaluator)
(module-ref (resolve-interface '(geesh eval)) 'eval-sh))
(define (split-assignment assignment)
(match (string-index assignment #\=)
(#f (values assignment #f))