3 exec ${GUILE-guile} -L $(pwd)/module -e '(nyacc)' -s "$0" "$@"
6 ;;; Mes --- The Maxwell Equations of Software
7 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
9 ;;; This file is part of GNU Guix.
11 ;;; Mes is free software; you can redistribute it and/or modify it
12 ;;; under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 3 of the License, or (at
14 ;;; your option) any later version.
16 ;;; Mes is distributed in the hope that it will be useful, but
17 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
24 ;; The Maxwell Equations of Software -- John McCarthy page 13
25 ;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
29 GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/nyacc.scm
32 ;; Tcalc.scm - calculator
34 ;; Copyright (C) 2015 Matthew R. Wette
36 ;; Copying and distribution of this file, with or without modification,
37 ;; are permitted in any medium without royalty provided the copyright
38 ;; notice and this notice are preserved. This file is offered as-is,
39 ;; without any warranty.
41 (define-module (nyacc)
42 #:use-module (ice-9 syncase) ;; guile-1.8
43 #:use-module (ice-9 optargs) ;; guile-1.8
44 #:use-module (nyacc lalr)
45 #:use-module (nyacc lex)
46 #:use-module (nyacc parse)
47 #:use-module (ice-9 rdelim)
48 #:use-module (ice-9 pretty-print)
53 (prec< (left "+" "-") (left "*" "/"))
57 (expr "+" expr ($$ (+ $1 $3)))
58 (expr "-" expr ($$ (- $1 $3)))
59 (expr "*" expr ($$ (* $1 $3)))
60 (expr "/" expr ($$ (/ $1 $3)))
62 ($fixed ($$ (string->number $1)))
63 ($float ($$ (string->number $1)))
64 ("(" expr ")" ($$ $2))))))
66 (define simple-mach (make-lalr-machine simple-spec))
68 ;; (use-modules (nyacc bison))
69 ;; (define simple-mach (make-lalr-machine/bison simple-spec))
71 (define match-table (assq-ref simple-mach 'mtab))
73 (define gen-lexer (make-lexer-generator match-table))
75 (define parse (make-lalr-parser simple-mach))
77 (define demo-string "2 + 2")
79 (define (main arguments)
82 (display (with-input-from-string demo-string
83 (lambda () (parse (gen-lexer)))))