mescc: Use Nyacc frontend.

* module/language/c99/compiler.mes: New file.
* module/language/c99/compiler.scm: Include it.
* module/mes/elf.mes: Move (mes-use-module) into cond-expand.
* module/mes/elf.scm: New file.
* module/mes/libc-i386.scm: New file.
* module/nyacc/lang/c99/parser.mes: Add missing module includes.
* module/nyacc/lang/util.mes: Add missing module include.
* scripts/mescc.mes: Use Nyacc.
* guile/mescc.scm: New file.
* GNUmakefile (guile-mescc): Run it.
This commit is contained in:
Jan Nieuwenhuizen 2016-12-31 09:03:07 +01:00
parent cd44770258
commit 7ec42c3cc7
20 changed files with 403 additions and 22 deletions

View file

@ -96,17 +96,14 @@ guile-check:
guile/nyacc-calc.scm
MAIN_C:=doc/examples/main.c
mescc: all
mescc: all $(MAIN_C)
rm -f a.out
scripts/mescc.mes $(MAIN_C)
scripts/mescc.mes $(MAIN_C) > a.out
./a.out; r=$$?; [ $$r = 42 ]
mescc.cat: all $(MES-0) module/rnrs/bytevectors.mes module/mes/elf.mes module/mes/libc-i386.mes module/language/c/lexer.mes module/language/c/parser.mes module/language/c/compiler.mes
echo '(compile)' | cat $(filter %.scm %.mes, $^) - > $@
guile-mescc: mescc.cat
guile-mescc: $(MAIN_C)
rm -f a.out
cat $(MAIN_C) | $(GUILE) -s $^ > a.out
guile/mescc.scm $(MAIN_C) > a.out
chmod +x a.out
./a.out; r=$$?; [ $$r = 42 ]

View file

@ -1,8 +1,8 @@
int main ()
int
main ()
{
int i; // = 0;
puts ("Hi Mes!\n");
for (i = 0; i < 4; ++i)
for (int i = 0; i < 4; ++i)
puts (" Hello, world!\n");
return 42;
}

1
guile/language Symbolic link
View file

@ -0,0 +1 @@
../module/language

1
guile/mes Symbolic link
View file

@ -0,0 +1 @@
../module/mes

View file

@ -1 +0,0 @@
../../module/nyacc

44
guile/mescc.scm Executable file
View file

@ -0,0 +1,44 @@
#! /bin/sh
# -*-scheme-*-
export GUILE_AUTO_COMPILE=0
exec ${GUILE-guile} -L $(pwd)/guile -e '(mescc)' -s "$0" "$@"
!#
;;; Mes --- The Maxwell Equations of Software
;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; 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/>.
;; The Maxwell Equations of Software -- John McCarthy page 13
;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
#!
Run with Guile-1.8:
GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
!#
(define-module (mescc)
#:use-module (language c99 compiler)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 pretty-print)
#:export (main))
(define (main arguments)
(let* ((files (cdr arguments))
(file (if (null? files) "doc/examples/main.c"
(car files))))
(with-input-from-file file
compile)))

1
guile/nyacc Symbolic link
View file

@ -0,0 +1 @@
../module/nyacc

View file

@ -1,6 +1,7 @@
#! /bin/sh
# -*-scheme-*-
exec ${GUILE-guile} -L $(pwd)/guile/mes -e '(nyacc)' -s "$0" "$@"
export GUILE_AUTO_COMPILE=0
exec ${GUILE-guile} -L $(pwd)/guile -e '(nyacc)' -s "$0" "$@"
!#
;;; Mes --- The Maxwell Equations of Software
@ -38,9 +39,13 @@ GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/nyacc.scm
;; notice and this notice are preserved. This file is offered as-is,
;; without any warranty.
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase))
(use-modules (ice-9 optargs))))
(define-module (nyacc)
#:use-module (ice-9 syncase) ;; guile-1.8
#:use-module (ice-9 optargs) ;; guile-1.8
#:use-module (nyacc lalr)
#:use-module (nyacc lex)
#:use-module (nyacc parse)

View file

@ -1,6 +1,7 @@
#! /bin/sh
# -*-scheme-*-
exec ${GUILE-guile} -L $(pwd)/guile/mes -e '(nyacc)' -s "$0" "$@"
export GUILE_AUTO_COMPILE=0
exec ${GUILE-guile} -L $(pwd)/guile -e '(nyacc)' -s "$0" "$@"
!#
;;; Mes --- The Maxwell Equations of Software
@ -29,6 +30,12 @@ Run with Guile-1.8:
GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/nyacc.scm
!#
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase))
(use-modules (ice-9 optargs))))
(define-module (nyacc)
#:use-module (nyacc lang c99 parser)
#:use-module (ice-9 rdelim)

View file

@ -0,0 +1,145 @@
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan 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:
;;; compiler.mes produces an i386 binary from the C produced by
;;; Nyacc c99.
;;; Code:
(cond-expand
(guile
(set-port-encoding! (current-output-port) "ISO-8859-1"))
(mes
(mes-use-module (nyacc lang c99 parser))
(mes-use-module (mes pmatch))
(mes-use-module (mes elf))
(mes-use-module (mes libc-i386))))
(define (mescc)
(parse-c99 #:inc-dirs '()))
(define (write-any x)
(write-char (if (char? x) x (integer->char (if (>= x 0) x (+ x 256))))))
(define (ast:function? o)
(and (pair? o) (eq? (car o) 'fctn-defn)))
(define (.name o)
(pmatch o
((fctn-defn _ (ftn-declr (ident ,name) _) _) name)))
(define (.statements o)
(pmatch o
((fctn-defn _ (ftn-declr (ident ,name) _) (compd-stmt (block-item-list . ,statements))) statements)))
(define (statement->data o)
(pmatch o
((expr-stmt (fctn-call (p-expr (ident ,name))
(expr-list (p-expr (string ,string)))))
(string->list string))
((for (decl (decl-spec-list (type-spec (fixed-type ,type)))
(init-declr-list (init-declr (ident ,identifier)
(initzer (p-expr (fixed ,start))))))
(lt (p-expr (ident _)) (p-expr (fixed ,test)))
,step ;;(pre-inc (p-expr (ident i)))
,statement)
(statement->data statement))
(_ '())))
(define (statement->text data o)
(let ((offset (length data)))
(pmatch o
((expr-stmt (fctn-call (p-expr (ident ,name))
(expr-list (p-expr (string ,string)))))
(list (lambda (data) (i386:puts (+ data offset) (string-length string)))))
((for (decl (decl-spec-list (type-spec (fixed-type ,type)))
(init-declr-list (init-declr (ident ,identifier)
(initzer (p-expr (fixed ,start))))))
(lt (p-expr (ident _)) (p-expr (fixed ,test)))
,step ;;(pre-inc (p-expr (ident i)))
,statement)
(display "start:" (current-error-port))
(display start (current-error-port))
(newline (current-error-port))
(display "test:" (current-error-port))
(display test (current-error-port))
(newline (current-error-port))
;; (display "step:" (current-error-port))
;; (display step (current-error-port))
;; (newline (current-error-port))
;;
(display "for-statement:" (current-error-port))
(display statement (current-error-port))
(newline (current-error-port))
(let ((start (string->number start))
(test (string->number test))
(step 1)
(statement (car (statement->text data statement))))
(display "2start:" (current-error-port))
(display start (current-error-port))
(newline (current-error-port))
(display "2for-statement:" (current-error-port))
(display statement (current-error-port))
(newline (current-error-port))
(list (lambda (d) (i386:for start test step (statement d))))))
((return (p-expr (fixed ,value)))
(let ((value (string->number value)))
(list (lambda (data) (i386:exit value)))))
(_ '()))))
(define (function->text+data o)
(let loop ((statements (.statements o)) (text '()) (data '()))
(display "text:" (current-error-port))
(display text (current-error-port))
(newline (current-error-port))
(if (null? statements) (values text data)
(let* ((statement (car statements)))
(display "statement:" (current-error-port))
(display statement (current-error-port))
(newline (current-error-port))
(loop (cdr statements)
(append text (statement->text data statement))
(append data (statement->data statement)))))))
(define (text+data->exe text data)
(display "dumping to a.out:\n" (current-error-port))
(map write-any (make-elf (lambda (data)
(append-map (lambda (f) (f data)) text)) data)))
(define (compile)
(let* ((ast (mescc))
(functions (filter ast:function? (cdr ast)))
(main (find (lambda (x) (equal? (.name x) "main")) functions)))
(display "AST" (current-error-port))
(pretty-print ast (current-error-port))
(format (current-error-port) "functions~a\n" functions)
(format (current-error-port) "main~a\n" main)
(call-with-values
(lambda () (function->text+data main))
text+data->exe)))

View file

@ -0,0 +1,40 @@
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan 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:
;;; Code:
(define-module (language c99 compiler)
#:use-module (srfi srfi-1)
#:use-module (system base pmatch)
#:use-module (ice-9 pretty-print)
#:use-module (mes elf)
#:use-module (mes libc-i386)
#:use-module (nyacc lang c99 parser)
#:export (compile))
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase)))
(mes))
(include-from-path "language/c99/compiler.mes")

View file

@ -0,0 +1,36 @@
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan 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:
;;; Code:
(define-module (mes bytevectors)
#:export (bytevector-u32-native-set!
bytevector-u16-native-set!
make-bytevector))
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase)))
(mes))
(include-from-path "mes/bytevectors.mes")

View file

@ -24,7 +24,10 @@
;;; Code:
(mes-use-module (rnrs bytevectors))
(cond-expand
(guile)
(mes
(mes-use-module (mes bytevectors))))
(define (int->bv32 value)
(let ((bv (make-bytevector 4)))

37
module/mes/elf.scm Normal file
View file

@ -0,0 +1,37 @@
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan 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:
;;; Code:
(define-module (mes elf)
#:use-module (mes bytevectors)
#:export (int->bv16
int->bv32
make-elf))
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase)))
(mes))
(include-from-path "mes/elf.mes")

39
module/mes/libc-i386.scm Normal file
View file

@ -0,0 +1,39 @@
;;; -*-scheme-*-
;;; Mes --- Maxwell Equations of Software
;;; Copyright © 2016 Jan 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:
;;; libc-i386.mes defines C library routines
;;; Code:
(define-module (mes libc-i386)
#:use-module (mes elf)
#:export (i386:exit
i386:for
i386:puts))
(cond-expand
(guile-2)
(guile
(use-modules (ice-9 syncase)))
(mes))
(include-from-path "mes/libc-i386.mes")

View file

@ -34,6 +34,20 @@
;;; Code:
;; (pmatch exp <clause> ...[<else-clause>])
;; <clause> ::= (<pattern> <guard> exp ...)
;; <else-clause> ::= (else exp ...)
;; <guard> ::= boolean exp | ()
;; <pattern> :: =
;; ,var -- matches always and binds the var
;; pattern must be linear! No check is done
;; _ -- matches always
;; 'exp -- comparison with exp (using equal?) REMOVED (August 8, 2012)
;; exp -- comparison with exp (using equal?)
;; (<pattern1> <pattern2> ...) -- matches the list of patterns
;; (<pattern1> . <pattern2>) -- ditto
;; () -- matches the empty list
(define-module (system base pmatch)
#:export-syntax (pmatch))

View file

@ -23,6 +23,8 @@
;;; Code:
(mes-use-module (mes guile))
(mes-use-module (mes catch))
(mes-use-module (mes fluids))
(mes-use-module (mes pretty-print))
(mes-use-module (mes optargs))
(mes-use-module (srfi srfi-9-psyntax))

View file

@ -23,6 +23,7 @@
;;; Code:
(mes-use-module (mes guile))
(mes-use-module (mes fluids))
(mes-use-module (mes optargs))
(mes-use-module (srfi srfi-1))
(include-from-path "nyacc/lang/util.scm")

View file

@ -1,7 +1,8 @@
#! /bin/sh
# -*-scheme-*-
prefix=module/
cat ${1-$(dirname $(dirname $0))/share/doc/mes/examples/main.c} | cat $prefix/mes/base-0.mes $0 /dev/stdin | $(dirname $0)/mes $MES_FLAGS "$@" > a.out
echo '()' | cat $prefix/mes/base-0.mes $0 /dev/stdin | $(dirname $0)/mes $MES_FLAGS "$@"
#paredit:||
chmod +x a.out
exit $?
!#
@ -27,13 +28,21 @@ exit $?
;;; Commentary:
;;; mescc.mes is a proof-of-concept simplistic C compiler and linker
;;;
;;; Run with Guile:
;;; make guile-mescc
;;; Code:
(mes-use-module (language c compiler))
;;LALR
;;(mes-use-module (language c compiler))
;;Nyacc
(mes-use-module (mes guile))
(mes-use-module (language c99 compiler))
(compile)
(define (main arguments)
(let* ((files (cdr arguments))
(file (if (null? files) "doc/examples/main.c"
(car files))))
(with-input-from-file file
compile)))
(main '("mes"))
()