630718f134
* module/mes/boot-0.scm (tty?): Guile-like command-line interface. * scripts/repl.mes: Remove. * scripts/mescc.mes: Update. * tests/*.test: Update scripts. * module/mes/repl.mes (repl): Add ,quit.
161 lines
6.1 KiB
Scheme
Executable file
161 lines
6.1 KiB
Scheme
Executable file
#! /bin/sh
|
|
# -*-scheme-*-
|
|
MES=${MES-$(dirname $0)/mes}
|
|
PREFIX=${PREFIX-@PREFIX@}
|
|
MES_PREFIX=${MES_PREFIX-$PREFIX}
|
|
if [ "$MES_PREFIX" = @PREFIX""@ ]
|
|
then
|
|
MES_PREFIX=$(cd $(dirname $0)/.. && pwd)
|
|
export MES_PREFIX
|
|
fi
|
|
MES_MODULEDIR=${MES_MODULEDIR-$MES_PREFIX/"module"}
|
|
export MES_MODULEDIR
|
|
$MES -s $0 "$@"
|
|
exit $?
|
|
!#
|
|
|
|
;;; Mes --- Maxwell Equations of Software
|
|
;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
;;;
|
|
;;; This file is part of Mes.
|
|
;;;
|
|
;;; Mes 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.
|
|
;;;
|
|
;;; Mes 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 Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;;; mescc.mes is a proof-of-concept simplistic C compiler and linker
|
|
|
|
;;; Code:
|
|
|
|
;;LALR
|
|
;;(mes-use-module (language c compiler))
|
|
;;Nyacc
|
|
|
|
(mes-use-module (mes guile))
|
|
(mes-use-module (mes getopt-long))
|
|
(mes-use-module (mes pretty-print))
|
|
(mes-use-module (language c99 info))
|
|
(mes-use-module (language c99 compiler))
|
|
(mes-use-module (mes display))
|
|
(mes-use-module (mes elf))
|
|
(mes-use-module (mes M1))
|
|
(mes-use-module (srfi srfi-1))
|
|
(mes-use-module (srfi srfi-26))
|
|
|
|
(format (current-error-port) "mescc.mes...\n")
|
|
|
|
(define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "MES_PREFIX") "") "@PREFIX@"))
|
|
|
|
(define (parse-opts args)
|
|
(let* ((option-spec
|
|
'((c (single-char #\c))
|
|
(define (single-char #\D) (value #t))
|
|
(E (single-char #\E))
|
|
(g (single-char #\g))
|
|
(help (single-char #\h))
|
|
(include (single-char #\I) (value #t))
|
|
(o (single-char #\o) (value #t))
|
|
(version (single-char #\V))))
|
|
(options (getopt-long args option-spec))
|
|
(help? (option-ref options 'help #f))
|
|
(files (option-ref options '() '()))
|
|
(usage? (and (not help?) (null? files)))
|
|
(version? (option-ref options 'version #f)))
|
|
(or
|
|
(and version?
|
|
(format (current-output-port) "mescc.scm (mes) ~a\n" %version))
|
|
(and (or help? usage?)
|
|
(format (or (and usage? (current-error-port)) (current-output-port)) "\
|
|
Usage: mescc.mes [OPTION]... FILE...
|
|
-c compile and assemble, but do not link
|
|
-D DEFINE define DEFINE
|
|
-E preprocess only; do not compile, assemble or link
|
|
-g add debug info [GDB, objdump] TODO: hex2 footer
|
|
-h, --help display this help and exit
|
|
-I DIR append DIR to include path
|
|
-o FILE write output to FILE
|
|
-v, --version display version and exit
|
|
")
|
|
(exit (or (and usage? 2) 0)))
|
|
options)))
|
|
|
|
(define (read-object file)
|
|
(let ((char (with-input-from-file file read-char)))
|
|
(if (eq? char #\#) (error "hex2 format not supported:" file)))
|
|
(with-input-from-file file read))
|
|
|
|
(define (main:ast->info file)
|
|
(let ((ast (with-input-from-file file read)))
|
|
(c99-ast->info ast)))
|
|
|
|
(define (source->ast defines includes)
|
|
(lambda (file)
|
|
(with-input-from-file file
|
|
(lambda ()
|
|
(write (c99-input->ast #:defines defines #:includes includes))))))
|
|
|
|
(define (source->info defines includes)
|
|
(lambda (file)
|
|
(with-input-from-file file
|
|
(lambda ()
|
|
((c99-input->info #:defines defines #:includes includes))))))
|
|
|
|
(define (ast? o)
|
|
(or (string-suffix? ".E" o)
|
|
(string-suffix? ".mes-E" o)))
|
|
|
|
(define (object? o)
|
|
(or (string-suffix? ".o" o)
|
|
(string-suffix? ".mes-o" o)))
|
|
|
|
(define (main args)
|
|
(let* ((options (parse-opts args))
|
|
(files (option-ref options '() '()))
|
|
(file (car files))
|
|
(preprocess? (option-ref options 'E #f))
|
|
(compile? (option-ref options 'c #f))
|
|
(debug-info? (option-ref options 'g #f))
|
|
(asts (filter ast? files))
|
|
(objects (filter object? files))
|
|
(sources (filter (cut string-suffix? ".c" <>) files))
|
|
(base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
|
|
(out (option-ref options 'o (cond (compile? (string-append base ".o"))
|
|
(preprocess? (string-append base ".E"))
|
|
(else "a.out"))))
|
|
(multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
|
|
(defines (reverse (filter-map (multi-opt 'define) options)))
|
|
(includes (reverse (filter-map (multi-opt 'include) options))))
|
|
(setenv "NYACC_TRACE" "yes")
|
|
(when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
|
|
(format (current-error-port) "output: ~a\n" out))
|
|
(if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
|
|
(with-output-to-port (open-output-file out (if (and (not compile?)
|
|
(not preprocess?)) S_IRWXU))
|
|
(lambda ()
|
|
(cond ((pair? objects) (let ((objects (map read-object objects)))
|
|
(if compile? (objects->M1 objects)
|
|
(objects->elf objects))))
|
|
((pair? asts) (let* ((infos (map main:ast->info asts))
|
|
(objects (map info->object infos)))
|
|
(if compile? (objects->M1 objects)
|
|
(objects->elf objects))))
|
|
((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
|
|
(let* ((infos (map (source->info defines includes) sources))
|
|
(objects (map info->object infos)))
|
|
(if compile? (objects->M1 objects)
|
|
(objects->elf objects))))))))))
|
|
|
|
(main (command-line))
|
|
'done
|